一、帮助启动类命令
1、启动docker
[root@localhost ~]# systemctl start docker
2、停止docker
[root@localhost ~]# systemctl stop docker
3、重启docker
[root@localhost ~]# systemctl stop docker
4、查看docker状态
[root@localhost ~]# systemctl status docker
5、开机自启动
[root@localhost ~]# systemctl enable docker
6、查看docker 概要信息
[root@localhost ~]# docker info
7、查看docker总体帮助文档
[root@localhost ~]# docker --help
8、查看docker命令帮助文档
docker 具体命令 --help
[root@localhost ~]# docker image --help
二、镜像命令
查看帮助文档:
Commands:attach Attach to a running container # 当前 shell 下 attach 连接指定运行镜像build Build an image from a Dockerfile # 通过 Dockerfile 定制镜像commit Create a new image from a container's changes # 提交当前容器为新的镜像cp Copy files/folders from the containers filesystem to the host path # 从容器中拷贝指定文件或者目录到宿主机中create Create a new container # 创建一个新的容器,同 run,但不启动容器diff Inspect changes on a container's filesystem # 查看 docker 容器变化events Get real time events from the server # 从 docker 服务获取容器实时事件exec Run a command in an existing container # 在已存在的容器上运行命令export Stream the contents of a container as a tar archive # 导出容器的内容流作为一个 tar 归档文件[对应 import ]history Show the history of an image # 展示一个镜像形成历史images List images # 列出系统当前镜像import Create a new filesystem image from the contents of a tarball # 从tar包中的内容创建一个新的文件系统映像[对应 export]info Display system-wide information # 显示系统相关信息inspect Return low-level information on a container # 查看容器详细信息kill Kill a running container # kill 指定 docker 容器load Load an image from a tar archive # 从一个 tar 包中加载一个镜像[对应 save]login Register or Login to the docker registry server # 注册或者登陆一个 docker 源服务器logout Log out from a Docker registry server # 从当前 Docker registry 退出logs Fetch the logs of a container # 输出当前容器日志信息port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT # 查看映射端口对应的容器内部源端口pause Pause all processes within a container # 暂停容器ps List containers # 列出容器列表pull Pull an image or a repository from the docker registry server # 从docker镜像源服务器拉取指定镜像或者库镜像push Push an image or a repository to the docker registry server # 推送指定镜像或者库镜像至docker源服务器restart Restart a running container # 重启运行的容器rm Remove one or more containers # 移除一个或者多个容器rmi Remove one or more images# 移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]run Run a command in a new container # 创建一个新的容器并运行一个命令save Save an image to a tar archive # 保存一个镜像为一个 tar 包[对应 load]search Search for an image on the Docker Hub # 在 docker hub 中搜索镜像start Start a stopped containers # 启动容器stop Stop a running containers # 停止容器tag Tag an image into a repository # 给源中镜像打标签top Lookup the running processes of a container # 查看容器中运行的进程信息unpause Unpause a paused container # 取消暂停容器version Show the docker version information # 查看 docker 版本号wait Block until a container stops, then print its exit code# 截取容器停止时的退出状态值
Run 'docker COMMAND --help' for more information on a command.示例:
1、列出本地的镜像
语法:docker images [OPTIONS] [REPOSITORY[:TAG]]
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE各个选项说明:REPOSITORY:表示镜像的仓库源TAG:镜像的标签版本号IMAGE ID
:镜像IDCREATED:镜像创建时间SIZE:镜像大小OPTIONS 选项说明:-a:列出本地所有镜像(含历史镜像层)-q:只显示镜像ID
示例:
[root@localhost lib]# docker images -q
d2c94e258dcb
[root@localhost lib]#
[root@localhost lib]#
[root@localhost lib]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d2c94e258dcb 13 months ago 13.3kB2、查询镜像
语法:docker search [OPTIONS] 镜像名字
[root@localhost ~]# docker search redis
NAME DESCRIPTION STARS OFFICIAL
redis Redis is the world’s fastest data platform f… 12853 [OK]
redislabs/redisearch Redis With the RedisSearch module pre-loaded… 64
redislabs/redisinsight RedisInsight - The GUI for Redis 103
redis/redis-stack-server redis-stack-server installs a Redis server w… 76
有点多不粘贴了只列出前5个
[root@localhost ~]# docker search --limit 5 redis
NAME DESCRIPTION STARS OFFICIAL
redis Redis is the world’s fastest data platform f… 12853 [OK]
redislabs/redisearch Redis With the RedisSearch module pre-loaded… 64
redislabs/redisinsight RedisInsight - The GUI for Redis 103
redis/redis-stack-server redis-stack-server installs a Redis server w… 76
redis/redis-stack redis-stack installs a Redis server with add… 1073、拉取镜像
语法:docker pull [OPTIONS] NAME[:TAG|@DIGEST]
[root@localhost ~]# docker pull ubuntu # 下载官方 ubuntu docker镜像,默认下载最新 ubuntu 官方库镜像
[root@localhost ~]# docker pull ubuntu:14.04 # 下载指定版本 ubuntu 官方镜像4、查看镜像、容器、数据卷所占的空间
[root@localhost ~]# docker system df
TYPE(类型) TOTAL(数量)ACTIVE (运行的) SIZE (大小) RECLAIMABLE(可用的)
Images 1 1 13.26kB 0B (0%)
Containers 1 0 0B 0B
Local Volumes 0 0 0B 0B
Build Cache 0 0 0B 0B5、删除一个或者多个镜像
语法:docker rmi [OPTIONS] IMAGE [IMAGE...][root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d2c94e258dcb 13 months ago 13.3kB
[root@localhost ~]# 6、删除镜像
[root@localhost ~]# docker rmi d2c94e258dcb
Error response from daemon: conflict: unable to delete d2c94e258dcb (must be forced) - image is being used by stopped container 0ac0ad1afbfc
[root@localhost ~]# #删除多个镜像
docker rmi -f 镜像名1:版本号 镜像名2:版本号
docker rmi -f 镜像ID1 镜像ID2
#删除全部
docker rmi -f $(docker images -qa)7、attach
# 当前 shell 下 attach 连接指定运行镜像
[root@localhost ~]# docker attach f6d23f416b00
root@f6d23f416b00:/#
三、容器命令
有镜像才能创建容器,本次演示使用Ubuntu镜像,先拉取镜像
[root@localhost network-scripts]# docker pull ubuntu[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ba6acccedd29 2 years ago 72.8MB
hello-world latest feb5d9fea6a5 2 years ago 13.3kB
1、新建或启动一个容器
查看帮助文档
[root@localhost ~]# docker run --help语法:docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
常用选项:注意 有些是一个减号,有些是两个减号
-c:设置 cpu 使用权重
-d:后台运行容器,并返回容器ID
-h:# 设置容器主机名
-i:以交互模式运行容器,通常与 -t 同时使用
-t:为容器重新分配一个伪输入终端
-m: 内存限制
--name="容器新名字" :为容器指定一个名称-P:# 自动映射容器对外提供服务的端口-p:# 指定端口映射示例:# 创建一个 cpu 优先级为 100,内存限制 512MB,主机名为 test1,名为 docker_test1 后台运行 bash 的容器[root@localhost ~]# docker run -t -i -c 100 -m 512MB -h test1 -d --name="docker_test1" ubuntu /bin/bash
1551e23a57a11d72f1089ee61672a8063c82529ab2b07ac2009cd4e96c3fe3a4
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1551e23a57a1 ubuntu "/bin/bash" 28 seconds ago Up 28 seconds docker_test1
[root@localhost ~]#
[root@localhost ~]# docker attach docker_test1
root@test1:/#
root@test1:/#
root@test1:/# exit
exit
[root@localhost ~]#
2、查看当前运行的容器
语法: docker ps [OPTIONS]
选项:-a # 列出当前系统正在运行的容器,+历史运行过的-l # 显示最近创建的容器-n # 显示最近n个创建的容器-q # 静默模式,只显示容器编号示例:
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ed59b9bdb95 ubuntu "/bin/bash" About a minute ago Up About a minute docker_test2[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ed59b9bdb95 ubuntu "/bin/bash" 2 minutes ago Up 2 minutes docker_test2
5cd794497a4d ubuntu "/bin/bash" 3 minutes ago Exited (0) 3 minutes ago docker_test1[root@localhost ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ed59b9bdb95 ubuntu "/bin/bash" 9 minutes ago Up 9 minutes docker_test2
[root@localhost ~]#
[root@localhost ~]# docker ps -n 1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ed59b9bdb95 ubuntu "/bin/bash" 10 minutes ago Up 10 minutes docker_test2
[root@localhost ~]# docker ps -n 2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ed59b9bdb95 ubuntu "/bin/bash" 10 minutes ago Up 10 minutes docker_test2
5cd794497a4d ubuntu "/bin/bash" 11 minutes ago Exited (0) 11 minutes ago docker_test1
[root@localhost ~]#
[root@localhost ~]# docker ps -q
1ed59b9bdb95
3、退出容器
退出容器方式有两种,
(1)、exit: run 进去容器,exit退出,容器停止
(2)、ctrl+p+q:run进去容器,Ctrl+p+q退出,容器不停止示例1:[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f6d23f416b00 ubuntu "/bin/bash" 5 seconds ago Up 4 seconds kind_pike
1ed59b9bdb95 ubuntu "/bin/bash" 3 hours ago Up 3 hours docker_test2
[root@localhost ~]#
[root@localhost ~]# docker attach f6d23f416b00root@f6d23f416b00:/#
root@f6d23f416b00:/# ls
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
root@f6d23f416b00:/#
root@f6d23f416b00:/# exit
exit
[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ed59b9bdb95 ubuntu "/bin/bash" 3 hours ago Up 3 hours docker_test2
可以看到刚才进去的容器已经消失了。
示例2:
进入第二个容器,然后Ctrl+p+q退出,然后容器还在运行中
[root@localhost ~]# docker attach 1ed59b9bdb95
root@test2:/#
root@test2:/#
root@test2:/# read escape sequence
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ed59b9bdb95 ubuntu "/bin/bash" 3 hours ago Up 3 hours docker_test2
4、启动已经停止运行的容器
docker start 容器ID或者容器名
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f6d23f416b00 ubuntu "/bin/bash" 7 minutes ago Exited (0) 4 minutes ago kind_pike
1ed59b9bdb95 ubuntu "/bin/bash" 3 hours ago Up 3 hours docker_test2[root@localhost ~]#
[root@localhost ~]# docker start f6d23f416b00
f6d23f416b00
[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f6d23f416b00 ubuntu "/bin/bash" 8 minutes ago Up 4 seconds kind_pike
1ed59b9bdb95 ubuntu "/bin/bash" 3 hours ago Up 3 hours docker_test2
5、重启、停止、强制停止容器
重启容器
docker restart 容器ID或者容器名
停止容器
docker stop 容器ID或者容器名
强制停止容器
docker kill 容器ID或者容器名
示例:
[root@localhost ~]# docker restart f6d23f416b00
f6d23f416b00
[root@localhost ~]#
[root@localhost ~]# docker stop f6d23f416b00
f6d23f416b00
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ed59b9bdb95 ubuntu "/bin/bash" 3 hours ago Up 3 hours docker_test2
[root@localhost ~]#
[root@localhost ~]# docker start f6d23f416b00
f6d23f416b00
[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f6d23f416b00 ubuntu "/bin/bash" 11 minutes ago Up 2 seconds kind_pike
1ed59b9bdb95 ubuntu "/bin/bash" 3 hours ago Up 3 hours docker_test2
[root@localhost ~]#
[root@localhost ~]# docker kill f6d23f416b00
f6d23f416b00
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ed59b9bdb95 ubuntu "/bin/bash" 3 hours ago Up 3 hours docker_test2
6、删除容器 docker rm 容器ID或者名字,如果正在运行是不能删除的,必须先停止容器。
[root@localhost ~]# docker rm 0ac0ad1afbfc
0ac0ad1afbfc
[root@localhost ~]#
如果非要删除,可不是不可以 ,加上-f 选项 强制删除
[root@localhost ~]# docker rm -f 0ac0ad1afbfc
0ac0ad1afbfc
[root@localhost ~]#
四、重点
1、启动守护式容器(后台启动),也就是使用 -d 选项
命令:docker run -d ubuntu
[root@localhost ~]# docker run -d redis
b7c4927819e6c41373a056ead4efb448e2225ca61492ec9ad5ded72ad80dad3f
[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b7c4927819e6 redis "docker-entrypoint.s…" 2 seconds ago Up 2 seconds 6379/tcp interesting_snyder
2、查看日志
命令:docker logs 容器ID
选项:--details 显示更多的信息-f, --follow 跟踪实时日志--since string 显示自某个timestamp之后的日志,或相对时间,如42m(即42分钟)--tail string 从日志末尾显示多少行日志, 默认是all-t, --timestamps 显示时间戳--until string 显示自某个timestamp之前的日志,或相对时间,如42m(即42分钟)示例:
[root@localhost ~]# docker logs b7c4927819e6
查看实时日志
[root@localhost ~]# docker logs -f b7c4927819e6
查看指定行数的日志
[root@localhost ~]# docker logs -f --tail=200 b7c4927819e6日志过滤:
1、关键字过滤
docker logs <容器id> | grep “关键词”
2、时间范围的过滤:使用–since和–until参数,可以查看指定时间段内的日志
docker logs --since "2024-06-17T00:00:00" --until "2024-06-17T23:59:59" <容器id>
3、日志输出
docker logs <容器id> > container_logs.txt
3、查看容器内运行的进程
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b7c4927819e6 redis "docker-entrypoint.s…" 41 minutes ago Up 41 minutes 6379/tcp interesting_snyder
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# docker top b7c4927819e6
UID PID PPID C STIME TTY TIME CMD
polkitd 21682 21661 0 14:48 ? 00:00:05 redis-server *:6379
4、查看容器或者镜像的详细信息
docker inspect 容器ID
[root@localhost ~]# docker inspect b7c4927819e6
5、进入正在运行的容器并以命令行交互
1、docker exec -it 容器ID / bin/bash
2、docker attach -it 容器ID /bin/bash
以上两个都可以。但是他们有什么区别呢
(1):attach 直接进入容器启动命令的终端,不会启动新的进程,用exit退出,会导致容器停止
(2):exec 是在容器中打开新的终端,并且可以启动新的进程,用exit退出,不会导致容器停止示例
[root@localhost ~]# docker run -it -d ubuntu /bin/bash
8bb2bfc0fb83b11eb4951d26af49221e6a8d9ae426f6ab47fee6773ca6b13952
[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8bb2bfc0fb83 ubuntu "/bin/bash" 3 seconds ago Up 2 seconds exciting_grothendieck
b7c4927819e6 redis "docker-entrypoint.s…" About an hour ago Up About an hour 6379/tcp interesting_snyder
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# docker attach 8bb2bfc0fb83
root@8bb2bfc0fb83:/#
root@8bb2bfc0fb83:/# exit
exit
[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b7c4927819e6 redis "docker-entrypoint.s…" About an hour ago Up About an hour 6379/tcp interesting_snyder
[root@localhost ~]#
[root@localhost ~]# docker run -it -d ubuntu /bin/bash
17d361f6c531847d1713a8ff403aefe96c504927888f1528f7228309f8adb3d8
[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
17d361f6c531 ubuntu "/bin/bash" 3 seconds ago Up 2 seconds busy_kapitsa
b7c4927819e6 redis "docker-entrypoint.s…" About an hour ago Up About an hour 6379/tcp interesting_snyder
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# docker exec -it 17d361f6c531 /bin/bash
root@17d361f6c531:/#
root@17d361f6c531:/# exit
exit
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
17d361f6c531 ubuntu "/bin/bash" 2 minutes ago Up 2 minutes busy_kapitsa
b7c4927819e6 redis "docker-entrypoint.s…" About an hour ago Up About an hour 6379/tcp interesting_snyder
6、从容器内拷贝文件到主机上
进入容器
命令:docker cp 容器ID:容器内路径 目的主机路径
[root@localhost ~]# docker exec -it 17d361f6c531 /bin/bash
root@17d361f6c531:/#
root@17d361f6c531:/# cd /tmp/
root@17d361f6c531:/tmp#
root@17d361f6c531:/tmp# touch a.txt
root@17d361f6c531:/tmp#
root@17d361f6c531:/tmp# ll
total 0
drwxrwxrwt. 1 root root 19 Jun 17 08:26 ./
drwxr-xr-x. 1 root root 29 Jun 17 07:50 ../
-rw-r--r--. 1 root root 0 Jun 17 08:26 a.txt
[root@localhost ~]# docker cp 17d361f6c531:/tmp/a.txt /tmp/
Successfully copied 1.54kB to /tmp/
[root@localhost ~]#
7、导入和导出容器
export 导出容器的内容留做为一个tar归档文件【对应import命令】
import从tar包中的内容创建一个新的文件系统在导入为镜像【对应export】
示例:
[root@localhost tmp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
17d361f6c531 ubuntu "/bin/bash" 21 hours ago Up 3 hours busy_kapitsa
b7c4927819e6 redis "docker-entrypoint.s…" 22 hours ago Up 3 hours 6379/tcp interesting_snyder
[root@localhost tmp]#
[root@localhost tmp]# docker export 17d361f6c531 > /tmp/ubuntu.tar
[root@localhost tmp]#导入
cat 包名.tar | docker import - 镜像用户/镜像名:镜像版本号
[root@localhost tmp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
17d361f6c531 ubuntu "/bin/bash" 21 hours ago Up 3 hours busy_kapitsa
b7c4927819e6 redis "docker-entrypoint.s…" 22 hours ago Up 3 hours 6379/tcp interesting_snyder
[root@localhost tmp]#
[root@localhost tmp]#
[root@localhost tmp]# docker rm -f 17d361f6c531
17d361f6c531
[root@localhost tmp]#
[root@localhost tmp]#
[root@localhost tmp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b7c4927819e6 redis "docker-entrypoint.s…" 22 hours ago Up 3 hours 6379/tcp interesting_snyder
[root@localhost tmp]#
[root@localhost tmp]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 7614ae9453d1 2 years ago 113MB
ubuntu latest ba6acccedd29 2 years ago 72.8MB
hello-world latest feb5d9fea6a5 2 years ago 13.3kB
[root@localhost tmp]#
[root@localhost tmp]# cat ubuntu.tar | docker import - wu/ubuntu:latest
sha256:9fd000d64b52e57a2c9111f2ffd9ea61f53a934af606327408a7c33e4c2ed0d3
[root@localhost tmp]#
[root@localhost tmp]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wu/ubuntu latest 9fd000d64b52 Less than a second ago 72.8MB
redis latest 7614ae9453d1 2 years ago 113MB
ubuntu latest ba6acccedd29 2 years ago 72.8MB
hello-world latest feb5d9fea6a5 2 years ago 13.3kB
[root@localhost tmp]#
[root@localhost tmp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b7c4927819e6 redis "docker-entrypoint.s…" 22 hours ago Up 3 hours 6379/tcp interesting_snyder
[root@localhost tmp]#
[root@localhost tmp]# docker run -it -d 9fd000d64b52 /bin/bash
3fd8b4c60b6cb60f9548fb8ff204752a001bdc0705c3206ff9b969bd37ddd952
[root@localhost tmp]#
[root@localhost tmp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3fd8b4c60b6c 9fd000d64b52 "/bin/bash" Less than a second ago Up Less than a second relaxed_liskov
b7c4927819e6 redis "docker-entrypoint.s…" 22 hours ago Up 3 hours 6379/tcp interesting_snyder[root@localhost tmp]#
[root@localhost tmp]# docker exec -it 3fd8b4c60b6c /bin/bash
root@3fd8b4c60b6c:/# cd /tmp/
root@3fd8b4c60b6c:/tmp# ll
total 0
drwxrwxrwt. 2 root root 19 Jun 17 08:26 ./
drwxr-xr-x. 1 root root 18 Jun 18 04:34 ../
-rw-r--r--. 1 root root 0 Jun 17 08:26 a.txt
root@3fd8b4c60b6c:/tmp#