Credit: https://github.com/jpetazzo/dind
Although running Docker inside Docker (DinD) or Docker outside of Docker (DooD) is generally not recommended, there are some legitimate use cases, such as development of Docker itself or for local CI testing. And in this blog post, I’m going to show you how to use DinD or DooD for local CI testing as it’s a very typical use case for local DevOps.
The differences between DinD and DooD?
DinD is the opposite of DooD.
DinD includes a whole Docker installation inside of it.
DooD uses its underlying host’s Docker installation by bind-mounting the Docker socket.
Obviously, DooD should be faster because we can leverage its caching mechanism, and DinD should be cleaner. DinD should support parallel running but DooD does not, or at least, not very reliable with my observation. If you want to conduct the clean testing, use DinD. If you want to conduct the faster testing, use DooD.
DooD is simpler to use than DinD.
And if you want to test with different versions of docker
, docker-compose
, use DinD and DooD.
Local CI testing with DinD
You can use https://hub.docker.com/r/library/docker/ for local testing, however, it’s alpine
image
which is not very suitable for local CI testing since it is not the default travis-ci environment.
We should use Ubuntu for executing CI scripts on all the CI systems (gitlab, drone, etc.) because we
can port the CI scripts easily between these CI systems.
That is the reason why we build teracy/ubuntu
Docker images to be used with DinD, you can check out
the project here: https://github.com/teracyhq/docker-files/tree/master/ubuntu and the built Docker
images here: https://hub.docker.com/r/teracy/ubuntu/tags/
We also have docker-compose
installed to the teracy/ubuntu
Docker images for faster testing
with it.
Let’s see how it works:
The commands from the above video:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Local CI testing with DooD
Let’s see how it works:
The commands from the above video:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Volume mounting
Volume mounting is a bit tricky, you must understand the underlying mechanism of its to get it work. Basically, you need to make sure the mounting path from the running containers must be the same as the DinD containers or DooD containers.
Let’s see how volume mounting works with DinD:
The commands from the above video:
1 2 3 4 5 6 7 8 9 |
|
Let’s see how volume mounting works with DooD:
The commands from the above video:
1 2 3 4 5 6 7 |
|
Local CI Testing with a real project
Let’s see how we can conduct a local CI testing with the https://github.com/teracyhq/docker-files project.
Let’s dive into the .travis.yml file https://github.com/teracyhq/docker-files/blob/master/.travis.yml to test the following scripts:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Let’s see how to do local CI testing in action:
This is just the very first step for basic testing.
Later, we should convert this .travis.yml
file into a build.sh
one to execute, this is the right way
for local CI testing travis-ci and other similar CI systems.
To do that, please follow https://github.com/teracyhq/docker-files/issues/42 and I’ll update this section more when it’s ready.
Too many virtualization layers?
At Teracy, the teracy-dev
VM is virtualized on a host machine (and the host machine could be virtualized
from another virtualized machine and so on).
Within the teracy-dev
VM, we use DinD. And within a Docker container, we can use Docker to run other
Docker containers and so on.
Yeah, welcome to the world of “Inception”, let’s figure out where you are now :–)?
Happy hacking!