Docker Cheatsheet

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

  1. docker push <username>/<image_name>
  2. docker images
  3. docker pull | inspect | rmi ubuntu:20.04
    • normally, the image name is composed by registry/username/repository:tag, if there is no username, the default is library, which is the official repository. if there is no registry, the default is docker.io, which is the official registry.
  4. docker create -it ubuntu:20.04 create a container by the image
  5. docker tag image_name:tag new_image_name:new_tag
  6. docker export/import and docker save/load
  • export/import will discard history and metadata information, only saving the snapshot state of the container at the time
    • docker export -o xxx.tar CONTAINER
    • docker import xxx.tar image_name:tag
  • save/load will save the complete record, with a larger volume
    • docker save -o xxx.tar image_name:tag
    • docker load -i xxx.tar

docker hub

  1. docker login -u <username>
  2. docker search <image_name>
  3. docker push <username>/<image_name>
  4. 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 the docker images command.

containers

  • docker ps -a list all containers
  • docker ps list running containers
  • docker 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
  • 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 time
  • docker 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 is sshd(which is not accept input) not the bash, 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.
  • docker update CONTAINER --memory 500MB
  • docker container prune remove all stopped containers
  • docker 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 volume
    • docker run --mount source=VOLUME,target=/webapp option to specify the volume
  • docker volume ls list all volumes
  • docker volume rm VOLUME remove the volume
  • docker volume prune remove all unused volumes
  • docker volume inspect VOLUME inspect the volume
  • docker 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.

相关推荐
To_OC25 分钟前
跑了3个Docker容器后,我终于搞懂镜像和容器到底啥关系
后端·docker·容器
tryCbest4 小时前
Docker 从零到精通:知识与跨平台使用指南
docker·容器
开始学AI7 小时前
Codex2API Docker 使用宿主机代理:OAuth Token 兑换 403 问题排查与解决方案
运维·docker·容器
做前端的娜娜子7 小时前
Docker 常用命令全梳理:从镜像拉取到容器编排
docker·容器·掘金·金石计划
m0_579146659 小时前
关于Docker Desktop导致C盘空间异常占用的排查与根治方案
运维·docker·容器
郝开12 小时前
Docker Compose 本地环境搭建:kkFileView
运维·docker·容器
jufeng130714 小时前
【系列:TDengine 工业物联网实战:从零搭起可运行系统 · 第 10 篇】
docker·时序数据库·tdengine·高可用
国际云,接待16 小时前
Docker 容器一直显示 unhealthy:从 Healthcheck 输出到依赖故障的完整排查
运维·docker·容器
IT大白鼠16 小时前
容器安全入门:K8s常见基础安全风险通俗解读
安全·容器·kubernetes
伞伞悦读16 小时前
Docker 安装 MinIO 完整教程(新手友好版)
docker·容器·eureka