The article reviews essential Docker CLI commands and illustrates them with real-world examples, including commands related to the Docker system information, file system, containers, and images.
Briefly About Docker
Docker is a free and open-source framework for packaging an application or database into a lightweight, portable container. It simplifies the process of installing an application on a server. And equally, to the same degree, it simplifies the process of installing a mobile app on a tablet or smartphone.
You can edit the Docker CLI configuration file, which is stored in the folder ~/.docker/
To examine the provided examples, ensure you have Docker Desktop on your computer. Otherwise, for installation instructions, please refer to the link.
Essential Docker CLI Commands
Informational Commands
To see a list of available commands, execute either the command docker without parameters or docker help:
docker --help
To get help on a particular command (e.g., on the command ps
):
docker ps --help
Display Docker’s system information:
docker info
Use the command version
to get installed Docker version:
docker version
Get a list of running containers:
docker ps
To show all the containers, execute the following command:
docker ps -a
Or you can get the same list, but with only the specified columns:
docker ps --format='table {{.ID}}\t{{.Image}}\t{{.Names}}'
To get list of the images on Docker, run the following command:
docker image ls
File System Commands
To take a snapshot (image) from existing container run the command commit
:
docker commit 77bf33c5d015 mysqlsnapshot
where 77bf33c5d015 is ID of the MySQL container.
After that, you can enter the container’s file system and explore it:
docker run -t -i mysqlsnapshot /bin/bash
To copy files/folders between a container and the local filesystem, use the cp
command:
In order to copy a specified file from the container to a local folder (container ID= 77bf33c5d015):
docker cp 77bf33c5d015:/var/log/mysqld.log C:\User_Projects\mysqld.log
And also to copy a local file to the container:
docker cp C:\User_Projects\mysqld2.log 77bf33c5d015:/var/log/mysqld2.log
To be sure if the file has been copied, select the appropriate folder in Visual Studio Code and check the files contained in it:
Finally, after inspecting the container’s file system, you can remove the created snapshot:
docker rmi mysqlsnapshot
Container and Image Commands
Run a container using a predefined image pulled from Docker’s repository with bundled ports:
docker pull gcr.io/cloud-spanner-emulator/emulator
docker run -p 9010:9010 -p 9020:9020 gcr.io/cloud-spanner-emulator/emulator
Run one-time PHP Container with Script
docker run --rm -v "$pwd":/usr/src/myapp -w /usr/src/myapp php:7.1-cli php script.php
Where:
- –rm – remove container after finish
- -v – volume list (in this example: “$pwd”:/usr/src/myapp) (for the volume details, follow the link)
- -w – working directory (in this example: /usr/src/myapp)
To get a list of built images that we have on the PC, execute the following command:
docker images
Run a container of the image provided, and if the image does not exist locally, it will be pulled:
docker run hello-world
Execute a command against a running container with both flags remaining interactive (flag: -i)
and opening a terminal (flag: -t)
:
docker exec -it pg_container bash
Archive container in the local file:
docker container export 8ef902c1ef23 > postgresql_container.tar
To import archived image, run the command load
:
docker load < postgresql_container.tar
Stop a running container:
docker stop pg_container
The container still exist in inactive mode. You can execute it again or remove.
Remove the container from Docker (be aware that you can lose all the created data):
docker rm pg_container
Name non-named image:
docker tag 577410342f45 postgres_57
Remove a image from Docker (577410342f45 is image ID):
docker rmi 577410342f45
Was this helpful?
1 / 0