Essence! A complete list of common Docker commands
This article includes container life cycle management commands, container operation commands, container rootfs command, image warehouse command, local image management command and basic version information command.
Container life cycle management commands
run
Create a new container.
# 使用docker镜像nginx:latest以后台模式启动一个容器,# 并将容器命名为mynginx。 docker run --name mynginx -d nginx:latest # 使用镜像 nginx:latest,以后台模式启动一个容器,# 将容器的 80 端口映射到主机的 80 端口,# 主机的目录 /data 映射到容器的 /data。 docker run -p 80:80 -v /data:/data -d nginx:latest # 使用镜像nginx:latest以交互模式启动一个容器,# 在容器内执行/bin/bash命令。 docker run -it nginx:latest /bin/bash
start/stop/restart
##docker start : Start one or more stopped containers. docker stop : Stop a running container. docker restart : Restart the container.
# 启动已被停止的容器mynginx docker start mynginx # 停止运行中的容器mynginx docker stop mynginx # 重启容器mynginx docker restart mynginx
kill
Kill a running container. Optional parameters:
-s: What signal to send to the container, default KILL
# 根据容器名字杀掉容器 docker kill tomcat7 # 根据容器ID杀掉容器 docker kill 65d4a94f7a39
rm
Delete one or more containers.
# 强制删除容器 db01、db02: docker rm -f db01 db02 # 删除容器 nginx01, 并删除容器挂载的数据卷: docker rm -v nginx01 # 删除所有已经停止的容器: docker rm $(docker ps -a -q)
create
Creates a new container without starting it.
# 使用docker镜像nginx:latest创建一个容器,并将容器命名为mynginx docker create --name mynginx nginx:latest
exec
Execute commands in a running container. Optional parameters:
-d : 分离模式: 在后台运行 -i : 即使没有附加也保持STDIN 打开 -t : 分配一个伪终端
# 在容器 mynginx 中以交互模式执行容器内 /root/nginx.sh 脚本 docker exec -it mynginx /bin/sh /root/nginx.sh # 在容器 mynginx 中开启一个交互模式的终端 docker exec -i -t mynginx /bin/bash # 也可以通过 docker ps -a 命令查看已经在运行的容器,然后使用容器 ID 进入容器。 docker ps -a docker exec -it 9df70f9a0714 /bin/bash
pause/unpause
docker pause :暂停容器中所有的进程。 docker unpause :恢复容器中所有的进程。
# 暂停数据库容器db01提供服务。 docker pause db01 # 恢复数据库容器 db01 提供服务 docker unpause db0
容器操作命令
ps
列出容器。可选参数:
-a : 显示所有的容器,包括未运行的。 -f : 根据条件过滤显示的内容。 –format : 指定返回值的模板文件。 -l : 显示最近创建的容器。 -n : 列出最近创建的n个容器。 –no-trunc : 不截断输出。 -q : 静默模式,只显示容器编号。 -s : 显示总的文件大小。
# 列出所有在运行的容器信息。 docker ps # 列出最近创建的5个容器信息。 docker ps -n 5 # 列出所有创建的容器ID。 docker ps -a -q
补充说明:
容器的7种状态:created(已创建)、restarting(重启中)、running(运行中)、removing(迁移中)、paused(暂停)、exited(停止)、dead(死亡)。
inspect
获取容器/镜像的元数据。可选参数:
-f : 指定返回值的模板文件。 -s : 显示总的文件大小。 –type : 为指定类型返回JSON。
# 获取镜像mysql:5.7的元信息。 docker inspect mysql:5.7 # 获取正在运行的容器mymysql的 IP。 docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mymysql
top
查看容器中运行的进程信息,支持 ps 命令参数。
# 查看容器mymysql的进程信息。 docker top mymysql # 查看所有运行容器的进程信息。 for i in `docker ps |grep Up|awk '{print $1}'`;do echo \ &&docker top $i; done
events
获取实时事件。参数说明:
-f : 根据条件过滤事件; –since : 从指定的时间戳后显示所有事件; –until : 流水时间显示到指定的时间为止;
# 显示docker 2016年7月1日后的所有事件。 docker events --since="1467302400" # 显示docker 镜像为mysql:5.6 2016年7月1日后的相关事件。 docker events -f "image"="mysql:5.6" --since="1467302400"
说明:如果指定的时间是到秒级的,需要将时间转成时间戳。如果时间为日期的话,可以直接使用,如–since=“2016-07-01”。
logs
获取容器的日志。参数说明:
-f : 跟踪日志输出 –since : 显示某个开始时间的所有日志 -t : 显示时间戳 –tail : 仅列出最新N条容器日志
# 跟踪查看容器mynginx的日志输出。 docker logs -f mynginx # 查看容器mynginx从2016年7月1日后的最新10条日志。 docker logs --since="2016-07-01" --tail=10 mynginx
export
将文件系统作为一个tar归档文件导出到STDOUT。参数说明:
牛逼啊!接私活必备的 N 个开源项目!赶快收藏
-o : 将输入内容写到文件。
# 将id为a404c6c174a2的容器按日期保存为tar文件。 docker export -o mysql-`date +%Y%m%d`.tar a404c6c174a2 ls mysql-`date +%Y%m%d`.tar
port
列出指定的容器的端口映射。
# 查看容器mynginx的端口映射情况。 docker port mymysql
容器rootfs命令
commit
从容器创建一个新的镜像。参数说明:
-a : 提交的镜像作者; -c : 使用Dockerfile指令来创建镜像; -m : 提交时的说明文字; -p : 在commit时,将容器暂停。
# 将容器a404c6c174a2 保存为新的镜像,# 并添加提交人信息和说明信息。 docker commit -a "guodong" -m "my db" a404c6c174a2 mymysql:v1
cp
用于容器与主机之间的数据拷贝。参数说明:
-L : 保持源目标中的链接
# 将主机/www/runoob目录拷贝到容器96f7f14e99ab的/www目录下。 docker cp /www/runoob 96f7f14e99ab:/www/ # 将主机/www/runoob目录拷贝到容器96f7f14e99ab中,目录重命名为www。 docker cp /www/runoob 96f7f14e99ab:/www # 将容器96f7f14e99ab的/www目录拷贝到主机的/tmp目录中。 docker cp 96f7f14e99ab:/www /tmp/
diff
检查容器里文件结构的更改。
# 查看容器mymysql的文件结构更改。 docker diff mymysql
镜像仓库命令
login/logout
docker login : 登陆到一个Docker镜像仓库,如果未指定镜像仓库地址,默认为官方仓库 Docker Hubdocker logout :登出一个Docker镜像仓库,如果未指定镜像仓库地址,默认为官方仓库 Docker Hub参数说明:
-u : 登陆的用户名 -p : 登陆的密码
# 登陆到Docker Hub docker login -u 用户名 -p 密码 # 登出Docker Hub docker logout
pull
从镜像仓库中拉取或者更新指定镜像。参数说明:
-a : 拉取所有 tagged 镜像 –disable-content-trust : 忽略镜像的校验,默认开启
# 从Docker Hub下载java最新版镜像。 docker pull java # 从Docker Hub下载REPOSITORY为java的所有镜像。 docker pull -a java
push
将本地的镜像上传到镜像仓库,要先登陆到镜像仓库。参数说明:
–disable-content-trust : 忽略镜像的校验,默认开启
# 上传本地镜像myapache:v1到镜像仓库中。 docker push myapache:v1
search
从Docker Hub查找镜像。参数说明:
–automated : 只列出 automated build类型的镜像; –no-trunc : 显示完整的镜像描述; -f \<过滤条件>: 列出指定条件的镜像。
# 从 Docker Hub 查找所有镜像名包含 java,并且收藏数大于 10 的镜像 docker search -f stars=10 java NAME DESCRIPTION STARS OFFICIAL AUTOMATED java Java is a concurrent, class-based... 1037 [OK] anapsix/alpine-java Oracle Java 8 (and 7) with GLIBC ... 115 [OK] develar/java 46 [OK]
每列参数说明:
NAME: 镜像仓库源的名称 DESCRIPTION: 镜像的描述 OFFICIAL: 是否 docker 官方发布 stars: 类似 Github 里面的 star,表示点赞、喜欢的意思 另外搜索公众号GitHub猿后台回复“赚钱”,获取一份惊喜礼包。 AUTOMATED: 自动构建
本地镜像管理命令
images
列出本地镜像。参数说明:
-a : 列出本地所有的镜像(含中间映像层,默认情况下,过滤掉中间映像层); –digests : 显示镜像的摘要信息; -f : 显示满足条件的镜像; –format : 指定返回值的模板文件; –no-trunc : 显示完整的镜像信息; -q : 只显示镜像ID。
# 查看本地镜像列表。 docker images # 列出本地镜像中REPOSITORY为ubuntu的镜像列表。 docker images ubuntu
rmi
删除本地一个或多个镜像。参数说明:
-f : 强制删除; –no-prune : 不移除该镜像的过程镜像,默认移除; 另外,搜索公众号Linux就该这样学后台回复“Linux”,获取一份惊喜礼包。
# 强制删除本地镜像 guodong/ubuntu:v4。 docker rmi -f guodong/ubuntu:v4
tag
标记本地镜像,将其归入某一仓库。
# 将镜像ubuntu:15.10标记为 runoob/ubuntu:v3 镜像。 docker tag ubuntu:15.10 runoob/ubuntu:v3
build
用于使用 Dockerfile 创建镜像。参数说明:
–build-arg=[] : Set the variables when creating the image; –cpu-shares: Set cpu usage weight; –cpu-period: Limit CPU CFS period ; ##–cpu-quota: Limit CPU CFS quota; -
–cpuset-cpus : Specify the CPU id used; –cpuset-mems : Specify Memory id used; ##–disable-content-trust: Ignore verification, enabled by default; -f : Specify the Dockerfile path to be used; –force -rm: Delete the intermediate container during the mirror setting process; -isolation: Use container isolation technology; –label=[] : Set the metadata used by the image; -m : Set the maximum memory value; –memory-swap : Set the maximum value of Swap to Memory swap, "-1" means no swap limit; - ##–no-cache:
The process of creating the image does not use cache; –pull : 尝试去更新镜像的新版本; –quiet, -q : 安静模式,成功后只输出镜像 ID; –rm : 设置镜像成功后删除中间容器; –shm-size : 设置/dev/shm的大小,默认值是64M; –ulimit : Ulimit配置。 –squash : 将 Dockerfile 中所有的操作压缩为一层。 –tag, -t: 镜像的名字及标签,通常 name:tag 或者 name 格式;可以在一次构建中为一个镜像设置多个标签。 –network: 默认 default。在构建期间设置RUN指令的网络模式
# 使用当前目录的 Dockerfile 创建镜像,标签为 runoob/ubuntu:v1 docker build -t runoob/ubuntu:v1 . # 使用URL github.com/creack/docker-firefox 的 Dockerfile 创建镜像 docker build github.com/creack/docker-firefox # 通过 -f Dockerfile文件的位置 创建镜像 docker build -f /path/to/a/Dockerfile .
history
查看指定镜像的创建历史。参数说明:
-H : 以可读的格式打印镜像大小和日期,默认为true; –no-trunc : 显示完整的提交记录; -q : 仅列出提交记录ID。
# 查看本地镜像 guodong/ubuntu:v3 的创建历史。 docker history guodong/ubuntu:v3
save
将指定镜像保存成 tar 归档文件。参数说明:
-o : 输出到的文件。
# 将镜像 runoob/ubuntu:v3 生成 my_ubuntu_v3.tar 文档 docker save -o my_ubuntu_v3.tar runoob/ubuntu:v3
load
导入使用 <span style="outline: 0px;font-size: 16px;">docker save</span>
命令导出的镜像。参数说明:
–input , -i : 指定导入的文件,代替 STDIN。 –quiet , -q : 精简输出信息。
# 导入镜像 docker load --input fedora.tar
import
从归档文件中创建镜像。参数说明:
-c : 应用docker 指令创建镜像; -m : 提交时的说明文字;
# 从镜像归档文件my_ubuntu_v3.tar创建镜像,命名为runoob/ubuntu:v4 docker import my_ubuntu_v3.tar runoob/ubuntu:v4
基础版本信息命令
info
显示 Docker 系统信息,包括镜像和容器数。
# 查看docker系统信息。 docker info
version
显示 Docker 版本信息。
docker version
The above is the detailed content of Essence! A complete list of common Docker commands. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

To save the image in Docker, you can use the docker commit command to create a new image, containing the current state of the specified container, syntax: docker commit [Options] Container ID Image name. To save the image to the repository, you can use the docker push command, syntax: docker push image name [: tag]. To import saved images, you can use the docker pull command, syntax: docker pull image name [: tag].

Troubleshooting steps for failed Docker image build: Check Dockerfile syntax and dependency version. Check if the build context contains the required source code and dependencies. View the build log for error details. Use the --target option to build a hierarchical phase to identify failure points. Make sure to use the latest version of Docker engine. Build the image with --t [image-name]:debug mode to debug the problem. Check disk space and make sure it is sufficient. Disable SELinux to prevent interference with the build process. Ask community platforms for help, provide Dockerfiles and build log descriptions for more specific suggestions.

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).
