The article will be updated from time to time, feel free to check the latest revision: Docker Cheatsheet
Deamon
docker infosystemctl start | stop | restart | status | enable dockerThis command to operate the docker daemon. For more details, you can refer to the systemd.docker system dfquery the disk usage of the docker
images
docker push <username>/<image_name>docker imagesdocker 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.04create a container by the imagedocker tag image_name:tag new_image_name:new_tagdocker export/importanddocker save/load:
export/importwill discard history and metadata information, only saving the snapshot state of the container at the timedocker export -o xxx.tar CONTAINERdocker import xxx.tar image_name:tag
save/loadwill save the complete record, with a larger volumedocker save -o xxx.tar image_name:tagdocker 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 imagescommand.
containers
docker ps -alist all containersdocker pslist running containersdocker statssearch all the containers resource usage (CPU, memory, storage, network)docker rename CONTAINER1 CONTAINER2docker start | stop | restart | rm | top | inspect | kill | port | history CONTAINERdocker run -itd ubuntu:20.04search 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⌃ + pand⌃ + qwhich can detach the container⌃+dwhich 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:xxxdocker exec CONTAINER COMMAND- eg
docker exec -it container_name bash docker exec -it container_name /bin/bashif 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/bashspecific the shell to enter the container.
- eg
docker update CONTAINER --memory 500MBdocker container pruneremove all stopped containersdocker commit container_name image_name:tagPackage 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 VOLUMEcreate a volumedocker run --mount source=VOLUME,target=/webappoption to specify the volume
docker volume lslist all volumesdocker volume rm VOLUMEremove the volumedocker volume pruneremove all unused volumesdocker volume inspect VOLUMEinspect the volumedocker rm -v CONTAINERremove 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.