Docker Within Docker

In case you ever need to run docker within docker, here is an example Dockerfile that does just that.

FROM ubuntu:16.04

RUN apt-get update
RUN apt-get install -y \
apt-transport-https ca-certificates

RUN apt-key adv \
--keyserver hkp:// \
--recv-keys 58118E89F3A912897C070ADBF76221572C52609D

RUN echo "deb ubuntu-xenial main" \
| tee /etc/apt/sources.list.d/docker.list

# Update package index using new source
RUN apt-get update

# Install the docker engine
RUN apt-get install docker-engine -y

# Start the docker engine immediately in bg
RUN nohup docker daemon -H tcp:// -H unix:///var/run/docker.sock &

# Install the cron service
# and use it to tie up the fg proc
RUN apt-get install cron -y

CMD ["/usr/sbin/cron", "-f"]

Just save that content to a file called Dockerfile within an empty directory, navigate to that directory and run

docker build .

Then when the image is built, all you have to do is make sure to run it with the --priviledged flag. E.g.

# Run in the background
docker run -d --privileged [image ID]

# run in interactive mode
docker run -it --privileged [image ID] /bin/bash



Stuart is a software developer with a passion for Linux and open source projects.

comments powered by Disqus