Programster's Blog

Tutorials focusing on Linux, programming, and open-source

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://p80.pool.sks-keyservers.net:80 \
--recv-keys 58118E89F3A912897C070ADBF76221572C52609D

RUN echo "deb https://apt.dockerproject.org/repo 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://0.0.0.0:2375 -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
Last updated: 8th August 2020
First published: 16th August 2018