Docker is a modern and popular version of chroot. The difference between the two is out of the scope of this article. What will be shown however is how to create a minimal docker image, that contains shell and commands of your choice. Since a script is worth a thousand words, just have a look below:


# This script generates a minimal image for docker, which contains COMMANDS and is based on SHELL. COMMANDS and SHELL can contain full paths.

COMMANDS='ls ps which'

rm -fr root
mkdir root
sh -c "which $SHELL $COMMANDS" | xargs ldd | grep -Po '/[^ :]*' | sort | uniq | xargs -I '{}' cp --parents '{}' root

echo "FROM scratch" > Dockerfile
echo "COPY root /" >> Dockerfile
echo "CMD [\"$(which $SHELL)\"]" >> Dockerfile

docker build -t $IMAGE:$TAG .
docker run --rm=true -t -i $IMAGE:$TAG
docker rmi $IMAGE:$TAG

Example above generates bash based image with ls, ps and which commands. Feel free to edit COMMANDS and SHELL variables according to your preference. Of course, shell and the commands are expected to exist in your host OS. It is also assumed that you have installed Docker in advance.

When you are done with editing, just execute the script. It will spent a few seconds on building and loading the image. Then prompt of your shell should pop up. You can play around in your newly created container. When you are done, just hit CTRL+D. Container will be removed automatically.