A few tip / docker tricks

Tram Ho

Tailing logs

Sometimes when we want to access docker’s logs quickly, we can use the following command:


Oh wait a minute

In docker-compose, sometimes we want to run multiple services but service A depends on service B to run before running. If you run the order, use depend_on, and dependencies are completely run (ie available-available), you will have to use another way.
Here is an example:


Run cron tasks in the docker Alpine container

Docker Alpine is a popular lightweight Docker image that contains a list of basic Linux utilities. Including crond.
To run our tasks periodically, we can mount our scripts based on when we want to execute it. In this example, the scripts placed in cron_tasks_folder will be executed for hours


Volumes in docker-compose

Host mounted volumes

/ host / path: can be relative or absolute path. This type of volume should be used when running infrastructure (containers) in a single machine (using Docker or docker compose).
Named volumes

With these volumes, it can be used on one machine or extended on a cluster (using Docker, docker composer or docker swarm).
Docker volumes can be internal or external. Internal volumes have scope in docker-compose file while external needs to be declared before using:


Use .dockerignore

The concept of .dockerignore is similar to .gitignore but the time it was used to build a better Docker image. With this file, we can avoid uploading unnecessary files that reduce build time and image size.
Create a file named .dockerignore, add the config files, the file we do not want to be added to the image.
Example 1:

Listing 2 below excludes all files and directories except the src directory:


Pass environment variables to docker

There are 2 options -e and –env.file (when you want to transfer many environmental variables)

See more here

Backup of container

Sometimes we need to backup a running container and bring it to another computer to run

After copying the above .tar file to another device, you can restore it

Perform list docker images on the host computer has just copied to see a new image just restored from the other .tar file

Minimal base image as possible

Sometimes it is not necessary to create images from “ubuntu” but you can create images from smaller image bases such as debian, alpine, scratch.
ubuntu -> debian -> alpine -> scratch


Run multiple containers

Think docker-compose right away.

View docker internal files

First, get docker container_id via the following command

Then follow the steps below


Showing information with –format

Sometimes information is cumbersome, we can reconfigure briefly with the –format parameter and sort and print the table format for example.

Share the news now

Source : Viblo