Docker Faqs

Within this article I have collected several useful Docker Faqs I’ve found across the Web. They can help you to understand better what is Docker for and some common best practices.

What is Docker?
Docker is an open-source program that lets a Linux application and its dependencies to be packaged as a container. Container-based virtualization isolates applications from each other on a shared operating system. This approach regulates application program delivery, allowing apps to run in any Linux context, whether physical or virtual. Because they share the same operating system, containers are portable among different Linux distributions and are significantly smaller than virtual machine images.

How many processes you should run in a Docker container ?
The suggested practice is to run only a single application in a Docker container. Splitting applications into multiple containers makes it easier to scale horizontally and reuse your containers. You can use docker-compose or container links if a service depends on another service.

Do you mean I should use Docker for every process ?
Probably the best approach would be to create role based Docker images. Using it for every single process would be overwhelming, however it would be easier to manage and conceptually more correct to manage Docker if you use it for roles like application server, database, volumes rather than individual processes (sshd, nginx, etc).
Also, the Docker images are likely to match with the OS roles created for your applications (e.g. jboss/oracle) making the Dockerization process much simpler.
In terms of scaling, having one role per server also means one Docker container per server. One container per server simplifies networking greatly and you don’t have to worry about port conflicts etc.

How to assign custom ports to Docker containers ?

Docker will assign random ports to access services on your containers unless you specify them explicitly. However, you can assign explicit port numbers and not have to mess with the complexity of trying to communicate random port numbers to other servers that need to access them.

for example:

docker run -p 80:80 -t -i image

How do you assign a custom IP Address to Docker ?
First, create a new IP and assign it to your host’s interface (we assume your interface is called eth0.

$ ip addr add 10.10.10.99/8 dev eth0

Now, when you fire up the container, specify that address and link it to your docker container:

$ docker run -i -t --rm -p 10.10.10.99:80:8080 base

The -p argument will make docker create an iptables NAT rule which will nat all the traffic matching the destination 10.0.0.99:80 to your docker container on port 8080.

Should I store data in Docker images ?
Storing Data in your container is not considered a best practice, unless you really know what you’re doing. If you’re not careful and stop a running container, that data may be lost forever. It’s safer and easier to manage your data if you store it directly on the host with a shared directory.
The approach that seems to work best for production is to use a data only container which is run on a barebone image and actually does nothing except exposing a data volume. Then you can run any other container to have access to the data container volumes:
For example:

docker run --volumes-from data-container some-other-container command-to-execute

Can you assign a PID namespace to your Docker images ?
Like most Docker isolation features, you can optionally create containers without their own PID namespace. You can try this yourself by setting the –pid flag on docker create or docker run and setting the value to host. Try it yourself with a container running BusyBox Linux and the ps Linux command:

docker run --pid host busybox:latest ps 

How to handle restart of Docker container ?
A Docker container can be in any of the following states
– Running
– Paused
– Restarting
– Exited (also used if the container has never been started)
A basic strategy for recovering from temporary failures is automatically restarting a process when it exits or fails. Docker has restart policies such as docker run –restart=always that will handle this. This is also available in the compose.yml config file as restart: always.

That’s all for now. I will keep adding some more Docker Faqs in the future!