The article will be updated from time to time, feel free to check the latest revision: Docker Cheatsheet
Deamon
docker info
systemctl start | stop | restart | status | enable docker
This command to operate the docker daemon. For more details, you can refer to the systemd.docker system df
query the disk usage of the docker
images
docker push <username>/<image_name>
docker images
docker pull | inspect | rmi ubuntu:20.04
- normally, the image name is composed by
registry/username/repository:tag
, if there is nousername
, the default islibrary
, which is the official repository. if there is noregistry
, the default isdocker.io
, which is the official registry.
- normally, the image name is composed by
docker create -it ubuntu:20.04
create a container by the imagedocker tag image_name:tag new_image_name:new_tag
docker export/import
anddocker save/load
:
export/import
will discard history and metadata information, only saving the snapshot state of the container at the timedocker export -o xxx.tar CONTAINER
docker import xxx.tar image_name:tag
save/load
will save the complete record, with a larger volumedocker save -o xxx.tar image_name:tag
docker load -i xxx.tar
docker hub
docker login -u <username>
docker search <image_name>
docker push <username>/<image_name>
- dangling image: if the image is updated by official, and the tag is allocated to the new image, the old image will be called dangling image. Only display
<none>
in thedocker images
command.
containers
docker ps -a
list all containersdocker ps
list running containersdocker stats
search all the containers resource usage (CPU, memory, storage, network)docker rename CONTAINER1 CONTAINER2
docker start | stop | restart | rm | top | inspect | kill | port | history CONTAINER
docker run -itd ubuntu:20.04
search and run a container (-d means detach) ==pull + create + start
- eg.
docker run -p 20000:22 --name my_docker_server -itd docker_images:1.0
- eg.
docker attach CONTAINER
⌃ + p
and⌃ + q
which can detach the container⌃+d
which can close and exit the container(exit; then the container will be stopped)
docker logs -f CONTAINER
-f means follow, you can see the logs in real timedocker cp xxx CONTAINER:xxx
docker exec CONTAINER COMMAND
- eg
docker exec -it container_name bash
docker exec -it container_name /bin/bash
if your garrison program issshd
(which is not accept input) not thebash
, then you should use this command to substitute it. (Recommend, exit; the container will not be stopped)- You can also use
docker run -it container_name /bin/bash
specific the shell to enter the container.
- eg
docker update CONTAINER --memory 500MB
docker container prune
remove all stopped containersdocker commit container_name image_name:tag
Package the container as an image.(Not recommend use to build image, can use as a snapshot of the container)
Docker volume
The volume will not be deleted when the container is deleted. Volume can be shared between containers.
docker volume create VOLUME
create a volumedocker run --mount source=VOLUME,target=/webapp
option to specify the volume
docker volume ls
list all volumesdocker volume rm VOLUME
remove the volumedocker volume prune
remove all unused volumesdocker volume inspect VOLUME
inspect the volumedocker rm -v CONTAINER
remove the container and the volume
Besides, you can also mount the host directory to the container. The host directory path must be absolute path.
shell
docker run -d -P \
--name web \
--mount type=bind,source=/src/webapp,target=/opt/webapp[,readonly] \
training/webapp \
python app.py
For the detail of build docker image, you can refer to the tips about dockerfile.