How to restart containers automatically in docker

If we are using docker containers for personal use we can restart it manually using docker restart. The docker restart command allows us to restart containers with some time limit. But, in production, it is difficult to restart by manual. To restart containers automatically, we can use restart policies for docker containers.

Restart policies for docker containers

Restart polies allows the container to restart automatically in required situations. The situations may be when a failure occurs, when daemon starts, when a container stopped.
The --restart flag is used with docker run command when starting a container. There are four restart policies available.

noDo not automatically restart containers anyway
alwaysAlways restart the container even though the container stopped manually. Until the daemon stops or the restart policy is changed, it will restart it in loop.
on-failureRestart the container if it exits due to an error, which manifests as a non-zero exit code. If any container is stopped due to any fault, it will automatically restart those containers with this policy.
unless-stoppedSimilar to always, except that when the container is stopped (manually or otherwise), it is not restarted even after Docker daemon restarts.

To run a container with restart policies, try with following code pattern

docker run --restart no hello-world

The above command run the hello-world image with restart policy set as no. It will not restart the containers automatically.

docker update --restart always hello-world

Here I use update command to update the restart policy of the hello-world container with always policy. This will restart the container always even though it is stopped manually or start when the daemon starts.

To view the events that occurs during restart we can use event command.

docker event

Open the docker event in one shell and run the container with always policy with another shell. The event shows the hello-world container will restart automatically every times it stopped. The image shows that every time the hello-world container stopped, it restart it.

docker restart containers(1)

We can also restart the containers using process managers such as upstart, systemd, supervisor. I will be posting an article on using process managers for docker in later.

For any reference to docker -> go.

For my other docker archives -> go.

If you like the article feel free to share.

 1,054 total views,  1 views today

What is docker networking and bridge networking?

In docker we can connect two or more containers or non-docker system using networking. Whether it is a windows or Linux containers, docker network helps to connect them in an easy way. This may helpful for beginners who interested in docker.

There are different network drivers available in docker networking

  • bridge:
    The default network driver if we don’t mention a network driver when running a container. It is created automatically when a container is created. It is best for standalone containers
  • host:
    If you use the host network mode for a container, that container’s network stack is not isolated from the Docker host, and the container does not get its own IP-address allocated.
    For instance, if you run a container which binds to port 80 and you use host networking, the container’s application is available on port 80 on the host’s IP address.
  • overlay:
    Overlay is mainly used when we want to connect multiple containers to create a swarm service to communicate between them.
  • macvlan:
    Macvlan networks allow you to assign a MAC address to a container, making it appear as a physical device on your network. The Docker daemon routes traffic to containers by their MAC addresses.
  • The other driver is called none, which is used when we use third party plugins from docker hub.

Bridge networking tutorial

This tutorial explain how a two standalone containers can be connected by bridge network. Here we create two alpine linux containers namely alpine1 and alphine2, then connect them using bridge.
We can view the network list using the command

docker network ls

You may see different network listed as bridge, host, none as following image. Any newly created network also listed here.
Next create two alpine containers as alpine 1 and alpine 2 with following command

docker run -dit --name alpine1 alpine 
docker run -dit --name alpine2 alpine

The two container created as follows
Then inspect the bridge network to view details of the containers connected to it with the following command

docker network inspect bridge

This will display a JSON format of bridge network details. We could see that by default alpine1 and alpine2 has bridge networks with different IP address as and respectively .
Now connect to the alpine1 container using attach command

docker attach alpine1

Now attach to the container and ping a website say -c 2 make 2 limit of ping.

Then try to ping the alpine2 container using the IP and check they are connected. If it ping correctly then successfully two containers are connected using bridge.

ping -c 2

For other docker archive -> go.
For network reference of docker -> go.

 1,177 total views

The difference between ADD and COPY in dockerfile.

When I was seeing some examples for docker file image building, I came across two things of same functionalities. They are COPY and ADD instructions. This article gives the difference between ADD and COPY in dockerfile. Next it explains how similar they are, then the best practice for using the RUN instead of ADD instruction.

ADD instruction

ADD instruction is an older one, which job is to copy the file or directory from a source to destination.
The ADD instruction can also do operations such as extraction of compressed files or download from an URL path.
Here the source may be a local compressed tar file or the URL path.
If it is a tar file, it extracts the contents to the destination else if it is an URL, then it download the file and extract to the destination in a similar way.
If authentication needs for URL file we can use RUN with curl or wget to download the files.
Since, ADD degrades in performance of the docker containers, COPY is been introduced for simple job.
Syntax for ADD:

ADD <src> <dest>

A simple example to ADD used for local tar file called source.tar.xz to the destination folder /dest.

ADD sourcefile.tar.xz /dest

This syntax example gives how the URL path called file is downloaded and copied to the /dest.

ADD /dest

COPY instruction

COPY instruction copies the file or directory from a source path to the destination.
Its job is simple as it duplicates the source file or directory to the destination.
It doesn’t include the operation of extraction or downloading files as ADD instruction.
Syntax for COPY:

COPY <src> <dest>

An example to COPY a file called source.txt to the destination folder /dest.

COPY sourcefile.txt /dest

What to use either COPY or ADD?

In dockerfile we use COPY many time because it only copies the file from a source to destination.ADD used when there is a purpose such as local tar files or URL file source.
For best docker practice, we can use RUN instruction instead of creating an extra layer.
RUN instruction with curl or wget used to get the file direct into the destination.
The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.

ADD /usr/src/things/
RUN tar -xJf /usr/src/things/source.tar.xz -C /usr/src/things

For instance take the above sentence, where a file called source.tar.gz is downloaded using ADD and extracted using tar with RUN instruction.
This makes two layers that worse the docker performance.
It simply done using RUN with curl tool as follows within a single layer.
If, there is direct copy operation needed than COPY instruction is recommended.
The below dockerfile only takes single layer of operation of file download using curl and then extraction of file using tar with RUN instruction.

RUN mkdir -p /usr/src/things \
    && curl -SL \
    | tar -xJC /usr/src/things 

Reference to the Best docker practices -> go.
For my docker archives click here -> go.
If any queries please comment it.

 1,119 total views