Tailing logs
Sometimes when we want to access docker’s logs quickly, we can use the following command:
1 2 | $ docker logs -t --tail 1000 [container_name or container_id] 2>&1 | grep -i error |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | version: '3' networks: test_net: services: webserver: image: nginx:alpine command: sh -c 'sleep 10 && nginx -g "daemon off;"' networks: - test_net test: image: alpine command: sh -c 'while ! nc webserver 80; do echo '.' && sleep 1; done && echo "Webserver up and running, bye bye..!"' networks: - test_net |
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
1 2 3 4 5 6 7 8 | version: '3' services: cron: image: alpine:3.6 command: crond -f -l 8 volumes: - ./cron_tasks_folder:/etc/periodic/hourly/:ro |
Volumes in docker-compose
Host mounted volumes
1 2 | Syntax: /host/path:/container/path |
/ 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
1 2 | Syntax: named_volume_name:/container/path |
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:
1 2 | $ docker volume create my_volume |
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:
1 2 3 4 5 | $ cat .dockerignore logs/ MY_SECRET_PASSWORDS.txt .dockerignore |
Listing 2 below excludes all files and directories except the src directory:
1 2 3 4 | $ cat .dockerignore ** !src |
Pass environment variables to docker
There are 2 options -e and –env.file (when you want to transfer many environmental variables)
1 2 3 4 | $ docker run -it -e TEST=1234 --env TEST1=3456 --rm alpine /bin/ash - write in terminal echo $TEST - you should have result '1234' |
See more here
Backup of container
Sometimes we need to backup a running container and bring it to another computer to run
1 2 3 4 5 | $ dokcer ps -a // sau đó lấy container_id hoặc container_name $ docker commit -p <container_id> <your_backup_name> $ docker images // xuất hiện image mới tên là <your_backup_name> $ docker save -o <container_file>.tar <your_backup_name> |
After copying the above .tar file to another device, you can restore it
1 2 | $ docker load -i <container_file>.tar |
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
1 2 3 4 5 6 | FROM scratch ..... or FROM alpine .... |
Run multiple containers
Think docker-compose right away.
View docker internal files
First, get docker container_id via the following command
1 2 | $ docker ps -a |
Then follow the steps below
1 2 3 4 5 6 | $ sudo su $ cd /var/lib/docker $ ls -list // show tất cả các container $ cd /containers/<CONTAINER_ID>+<OTHER_HASH_NAME> $ ls -list // show tất cả file của container |
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.
1 2 | docker images --format '{{.Size}}t{{.Repository}}:{{.Tag}}t{{.ID}}' | sort -h | column -t |