Skip to content

July 13, 2021––– views

Docker Linux commands

Omar Alsoudani - Linux Docker

Overview

This page covers Docker specific commands on Linux. You can use it as a reference in case you want to do some task quickly, or you might find something new and beneficial when using Docker.

Docker compose

This builds the images and run their containers assuming the default compose file name is docker-compose.yml, or by specifying the file name.

Default file name

docker-compose up  --build -d

Different file name

docker-compose  -f docker-compose.development.yml up  --build -d

More than one file. This gives you the ability to add more than one file, and docker will combine them into and single configuration. Docker will build the configuration by the order of the files supplied, so subsequent files will add configuration or override (in case the configuration directive was in two files) to their predecessors

docker-compose -f docker-compose.base.yml -f docker-compose.proxy.yml -f docker-compose.migration.yml up --build -d

Build images and run their containers while ignoring the current existing images cache (useful for multi-stage builds, when you just want to re-build everything with fresh layers)

docker-compose build  --no-cache

The same as above but this time also attempt to pull the latest version of the images. The pull argument might be useless for you if your images are pinned to an exact version. For example this NodeJS image node:14.16.1-alpine specifies the major, minor and patch with the tag alpine, so the pull argument will do nothing. Meanwhile this NodeJS image node:alpine will attempt to pull the latest non-breaking changes LTS version, basically it will update the minor and patch versions.

docker-compose build  --no-cache --pull

Although I said non-breaking, you never know with docker. So I always prefer the pinning to exact version and update the image myself manually. Unless ofcourse I'm too lazy to do that, then I use the second method. Which is the case in this site!

exec command

This executes a command from a container

docker exec -it node_app npm install

Exporting MySQL database

Export data from a Docker MySQL container to a directory using mysqldump and execute command

  • Full database structure and data

    docker exec -i app_mysql-server-57_1 mysqldump \
    -uroot -proot --databases mydb_name --skip-comments > /Documents/db.sql
    
  • Specified table structure and data

    docker exec -i app_mysql-server-57_1 mysqldump \
    -uroot -proot mydb_name mytable_name > /Documents/db.sql
    
  • Specific table structure and data that matches a where clause

    docker exec app_mysql mysqldump \
    -uroot -proot mydb_name --tables mytable_name --where="id>5 limit 5000000" > mytable_name.sql
    

SSH into a container

  • For alpine images, only sh is available

    docker exec -it app_server sh
    
  • For other images, bash is available

    docker exec -it app_server bash
    

Testing config

Preview and validate a docker compose file configuration found in the specified path

docker-compose -f docker-compose.development.yml config

Deleting data

Delete all stopped containers and unused images/networks/volumes and non persistent volumes and their networks

WARNING: Beware that all stopped containers, and unused images/networks/volumes will be deleted, so if you have something stopped but want to use it later, or you want the image cache layers, then don't do this.

Everything
docker system prune -a
Networks
docker network prune
Volumes
docker volume ls -qf dangling=true | xargs -r docker volume rm

Note: If you are using volumes managed by docker instead of bind volumes by declaring them in a docker-compose file. Beware that you might accidentally wipe the whole data by issuing the following

docker-compose down -v

down tells docker to remove all containers and images from the running compose file. -v tells docker to also wipe any managed volumes

Tailing logs

This gets you the latest 100 line from a container

docker logs --tail 100 -f app_server

This finds lines that contains a phrase and print the containing line, 3 lines before it and 3 lines after it

docker logs -f --tail 10000  app_server | grep -B3 -A3 "api/users"

System wide info

docker system info

Disk usage

Detailed
docker system df -v
Overview
docker system df

All topics

Omar Alsoudani

Modified July 13, 2021

Continue reading

Linux commands for me