docker-shell - run a user shell in a new container with pwd bind mounted
Runs the command process in a new container; using the current user, with pwd bind mounted.
When called without arguments, docker-shell defaults to running an interactive shell as current user.
Under the hood, docker-shell builds its docker image using the Dockerfile(5) from the current working directory inside docker-build(1). docker-shell appends a groupadd(8) and a useradd(8) RUN commands to it, to add the $USER/$GROUPS to the container. Once the image is build, the command is run into su(1) using user $USER through docker-run.
Run an interactive shell in the latest Ubuntu container
$ echo FROM ubuntu >Dockerfile
$ cat Dockerfile
FROM ubuntu
$ docker-shell
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu
---> f49eec89601e
Step 2 : RUN groupadd --non-unique --gid 1000 gportay
---> Using cache
---> 572ea4688a13
Step 3 : RUN useradd --non-unique --gid 1000 --uid 1000 --create-home --home-dir /home/gportay --shell /bin/bash gportay
---> Using cache
---> 777c682a9816
Successfully built 777c682a9816
gportay@4c3fb2d195d8:~$
Check for the distribution
gportay@4c3fb2d195d8:~$ cat /etc/os-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.1 LTS"
NAME="Ubuntu"
VERSION="16.04.1 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.1 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
Check for user
gportay@4c3fb2d195d8:~$ whoami
gportay
Exit from container
gportay@4c3fb2d195d8:~$ exit
logout
Run the commands above in the same container as a shell script
$ ./docker-shell "cat /etc/os-release && whoami"
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu
---> f49eec89601e
Step 2 : RUN groupadd --non-unique --gid 1000 gportay
---> Using cache
---> 572ea4688a13
Step 3 : RUN useradd --non-unique --gid 1000 --uid 1000 --create-home --home-dir /home/gportay --shell /bin/bash gportay
---> Using cache
---> 777c682a9816
Successfully built 777c682a9816
NAME="Ubuntu"
VERSION="16.04.1 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.1 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
gportay
logout
Check for PWD bind mountage ; write the distribution name to local file
$ ./docker-shell -c "grep '^NAME=' /etc/os-release >os-release"
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu
---> f49eec89601e
Step 2 : RUN groupadd --non-unique --gid 1000 gportay
---> Using cache
---> 572ea4688a13
Step 3 : RUN useradd --non-unique --gid 1000 --uid 1000 --create-home --home-dir /home/gportay --shell /bin/bash gportay
---> Using cache
---> 777c682a9816
Successfully built 777c682a9816
Read the contents outside the container
$ cat os-release
NAME="Ubuntu"
Report bugs at https://github.com/gazoo74/templates/issues
Written by Gaël PORTAY [email protected]
Copyright (c) 2017 Gaël PORTAY
This program is free software: you can redistribute it and/or modify it under the terms of the MIT License.