文章目录
- 一、流转图
- 二、基本操作
- 2.1 查看本地容器进程
- 2.2 启动容器
- 2.2.1 交互式启动容器
- 2.2.2 后台启动容器
- 2.3 进入容器
- 2.4 停止启动重启容器
- 2.5 退出容器
- 2.6 删除容器
- 2.7 提交容器(打包成镜像)
- 2.8 拷贝文件
- 2.8.1 拷贝容器内文件到宿主机
- 2.8.2 拷贝宿主机文件到容器内
- 2.9 容器日志
- 2.10 容器进程信息
- 2.11 容器元数据
- 2.12 查看容器状态
- 2.13 其他
- 三、高级操作
- 3.1 映射端口
- 3.2 挂载数据卷
- 3.2.1 自定义数据卷目录
- 3.2.2 自动数据卷目录
- 3.3 传递环境变量
- 3.4 容器内安装软件
一、流转图
二、基本操作
2.1 查看本地容器进程
#docker-ps
# -a 所有,包括运行的和已退出的
[root@server ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
02b2d2343a23 hello-world "/hello" 8 days ago Exited (0) 8 days ago festive_meninsky[root@server ~]# docker ps -n 2 # 显示正在运行的容器中最新的2个
[root@server ~]# docker ps # 显示当前正在运行的容器
[root@server ~]# docker ps -q # 显示正在运行的容器ID
2.2 启动容器
#docker-run
2.2.1 交互式启动容器
-i
表示交互,-t
表示(后跟)指定伪终端
[root@server ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest f8c20f8bbcb6 5 weeks ago 7.38MB
hello-world latest d2c94e258dcb 8 months ago 13.3kB[root@server ~]# docker run -it alpine /bin/sh
/ # ls
bin etc lib mnt proc run srv tmp var
dev home media opt root sbin sys usr
/ #
此时可看到有容器进程:
[root@server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
908b5182365c alpine "/bin/sh" 7 seconds ago Up 5 seconds interesting_bhabha
[root@server ~]#
若退出容器,则容器进程也会结束,因为容器内没有进程在运行着,且没有指定后台运行容器。
2.2.2 后台启动容器
-d
表示后台运行
[root@server ~]# docker run -td --name myalpine alpine:latest
9ce9f829f6e66b354a2b806e07d2148754fe7bb710f2e1993d46b2661833a39f
[root@server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ce9f829f6e6 alpine "/bin/sh" 25 seconds ago Up 24 seconds myalpine
[root@server ~]#
2.3 进入容器
#docker-exec
进入容器时开启一个新的终端:
[root@server ~]# docker exec -it 9ce9f829f6e6 /bin/sh
/ # exit
[root@server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ce9f829f6e6 alpine "/bin/sh" 5 minutes ago Up About a minute myalpine
#docker-attach
进入当前正在运行的容器终端:
[root@server ~]# docker attach 9ce9f829f6e6
2.4 停止启动重启容器
#docker-stop
#docker-start
#docker-restart
[root@server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ce9f829f6e6 alpine "/bin/sh" 2 minutes ago Up 2 minutes myalpine
[root@server ~]# docker stop 9ce9f829f6e6
9ce9f829f6e6
[root@server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@server ~]# docker start 9ce9f829f6e6
9ce9f829f6e6
[root@server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ce9f829f6e6 alpine "/bin/sh" 3 minutes ago Up 1 second myalpine
[root@server ~]# docker restart 9ce9f829f6e6
9ce9f829f6e6
[root@server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ce9f829f6e6 alpine "/bin/sh" 3 minutes ago Up 1 second myalpine
[root@server ~]#
2.5 退出容器
exit # 容器停止并退出(容器内没有进程在运行)
Ctrl + P + Q # 容器不停止退出
2.6 删除容器
#docker-rm
[root@server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ce9f829f6e6 alpine "/bin/sh" 17 minutes ago Up 13 minutes myalpine[root@server ~]# docker rm myalpine
Error response from daemon: You cannot remove a running container 9ce9f829f6e66b354a2b806e07d2148754fe7bb710f2e1993d46b2661833a39f. Stop the container before attempting removal or force remove
[root@server ~]# docker stop myalpine
myalpine
[root@server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@server ~]# docker rm myalpine
myalpine[root@server ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
908b5182365c alpine "/bin/sh" 22 minutes ago Exited (0) 20 minutes ago interesting_bhabha
02b2d2343a23 hello-world "/hello" 8 days ago Exited (0) 8 days ago festive_meninsky
[root@server ~]#
若容器正在运行中仍要被删除,则需使用
docker rm -f
强制删除
删除已停止运行的容器中的第一个容器:
[root@server ~]# for i in `docker ps -a | grep -i exit | sed '1d' | awk '{print $1}'`;do docker rm -f $i;done
02b2d2343a23
[root@server ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
908b5182365c alpine "/bin/sh" 24 minutes ago Exited (0) 23 minutes ago interesting_bhabha
[root@server ~]#
2.7 提交容器(打包成镜像)
#docker-commit
[root@server ~]# docker start 908b5182365c
908b5182365c
[root@server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
908b5182365c alpine "/bin/sh" 36 minutes ago Up 5 minutes interesting_bhabha# 添加作者、描述,指定容器ID,加上镜像名:Tag
[root@server ~]# docker commit -a="auther" -m="this is a comment" 908b5182365c myalpine:1.0.0
sha256:9cc4b0832b11004cbca8b55dbdde69647d0851d002b3e629d9bad149a8ce5424[root@server ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myalpine 1.0.0 9cc4b0832b11 4 seconds ago 7.38MB
alpine latest f8c20f8bbcb6 5 weeks ago 7.38MB
hello-world latest d2c94e258dcb 8 months ago 13.3kB
2.8 拷贝文件
#docker-cp
2.8.1 拷贝容器内文件到宿主机
docker cp 容器id:容器内路径 宿主机目的路径docker cp 55321bcae33d:/test.java / # 拷贝到宿主机 / 根目录下
2.8.2 拷贝宿主机文件到容器内
docker cp 文件or目录名 容器ID:/路径
2.9 容器日志
#docker-logs
[root@server ~]# docker run hello-world 2>&1 >> /dev/null
[root@server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@server ~]# docker ps -a | grep hello
de258d79d2af hello-world "/hello" 18 seconds ago Exited (0) 17 seconds ago elastic_mendel
[root@server ~]# docker logs -f de258d79d2afHello from Docker!
This message shows that your installation appears to be working correctly.To generate this message, Docker took the following steps:1. The Docker client contacted the Docker daemon.2. The Docker daemon pulled the "hello-world" image from the Docker Hub.(amd64)3. The Docker daemon created a new container from that image which runs theexecutable that produces the output you are currently reading.4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.To try something more ambitious, you can run an Ubuntu container with:$ docker run -it ubuntu bashShare images, automate workflows, and more with a free Docker ID:https://hub.docker.com/For more examples and ideas, visit:https://docs.docker.com/get-started/
[root@server ~]# docker logs --helpUsage: docker logs [OPTIONS] CONTAINERFetch the logs of a containerAliases:docker container logs, docker logsOptions:--details Show extra details provided to logs-f, --follow Follow log output--since string Show logs since timestamp (e.g. "2013-01-02T13:23:37Z") or relative (e.g. "42m"for 42 minutes)-n, --tail string Number of lines to show from the end of the logs (default "all")-t, --timestamps Show timestamps--until string Show logs before a timestamp (e.g. "2013-01-02T13:23:37Z") or relative (e.g. "42m"for 42 minutes)
查看倒数5行日志,并打上时间戳(-t
):
[root@server ~]# docker logs --tail 5 de258d79d2afhttps://hub.docker.com/For more examples and ideas, visit:https://docs.docker.com/get-started/[root@server ~]# docker logs --tail 5 -t de258d79d2af
2024-01-20T09:17:28.063983170Z https://hub.docker.com/
2024-01-20T09:17:28.063985026Z
2024-01-20T09:17:28.063986546Z For more examples and ideas, visit:
2024-01-20T09:17:28.063988103Z https://docs.docker.com/get-started/
2024-01-20T09:17:28.063989639Z
-f
跟踪日志:
[root@server ~]# docker run -d centos /bin/sh -c "while true;do echo helloworld;sleep 1;done"
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
d9137e0fd7ea7799f65ba6f470723629610df41fc0735de60ff1f6ebeea2939c
[root@server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d9137e0fd7ea centos "/bin/sh -c 'while t…" 10 seconds ago Up 7 seconds determined_tharp
[root@server ~]# docker logs -tf -n5 d9137e0fd7ea
2024-01-20T09:23:42.471835910Z helloworld
2024-01-20T09:23:43.476544231Z helloworld
2024-01-20T09:23:44.479378544Z helloworld
2024-01-20T09:23:45.488346985Z helloworld
2024-01-20T09:23:46.491736593Z helloworld
2024-01-20T09:23:47.497556758Z helloworld
2024-01-20T09:23:48.507944232Z helloworld
2024-01-20T09:23:49.515828873Z helloworld
2.10 容器进程信息
#docker-top
查看容器中进程信息:
[root@server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d9137e0fd7ea centos "/bin/sh -c 'while t…" About a minute ago Up About a minute determined_tharp
[root@server ~]# docker top d9137e0fd7ea
UID PID PPID C STIME TTY TIME CMD
root 29345 29325 0 17:23 ? 00:00:00 /bin/sh -c while true;do echo helloworld;sleep 1;done
root 29590 29345 0 17:25 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
2.11 容器元数据
#docker-inspect
[root@server ~]# docker inspect d9137e0fd7ea
[{"Id": "d9137e0fd7ea7799f65ba6f470723629610df41fc0735de60ff1f6ebeea2939c","Created": "2024-01-20T09:23:24.883298959Z","Path": "/bin/sh","Args": ["-c","while true;do echo helloworld;sleep 1;done"],"State": {"Status": "running","Running": true,"Paused": false,"Restarting": false,"OOMKilled": false,"Dead": false,"Pid": 29345,"ExitCode": 0,"Error": "","StartedAt": "2024-01-20T09:23:26.389867521Z","FinishedAt": "0001-01-01T00:00:00Z"},"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6","ResolvConfPath": "/var/lib/docker/containers/d9137e0fd7ea7799f65ba6f470723629610df41fc0735de60ff1f6ebeea2939c/resolv.conf","HostnamePath": "/var/lib/docker/containers/d9137e0fd7ea7799f65ba6f470723629610df41fc0735de60ff1f6ebeea2939c/hostname","HostsPath": "/var/lib/docker/containers/d9137e0fd7ea7799f65ba6f470723629610df41fc0735de60ff1f6ebeea2939c/hosts","LogPath": "/var/lib/docker/containers/d9137e0fd7ea7799f65ba6f470723629610df41fc0735de60ff1f6ebeea2939c/d9137e0fd7ea7799f65ba6f470723629610df41fc0735de60ff1f6ebeea2939c-json.log","Name": "/determined_tharp",...
2.12 查看容器状态
#docker-stats
[root@server ~]# docker stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
d9137e0fd7ea determined_tharp 0.15% 3.941MiB / 846.4MiB 0.47% 656B / 0B 11.2MB / 0B 2
2.13 其他
Docker运行容器时,可设置分配CPU权重、绑核等操作。
分配CPU权重 --cpu-shares
taskset 把进程绑定到特定CPU核上
stress 压力测试
三、高级操作
拉取nginx
镜像,做好后续操作的环境准备工作:
[root@server ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
2f44b7a888fa: Pull complete
8b7dd3ed1dc3: Pull complete
35497dd96569: Pull complete
36664b6ce66b: Pull complete
2d455521f76c: Pull complete
dc9c4fdb83d6: Pull complete
8056d2bcf3b6: Pull complete
Digest: sha256:4c0fdaa8b6341bfdeca5f18f7837462c80cff90527ee35ef185571e1c327beac
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
3.1 映射端口
-p
映射端口:容器外端口 -> 容器内端口
--rm
,会在容器退出时,自动清除挂载的卷,以便清除数据
[root@server ~]# docker run --rm --name mynginx -d -p81:80 nginx[root@server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
97665eae03a1 nginx "/docker-entrypoint.…" 9 minutes ago Up 9 minutes 0.0.0.0:81->80/tcp, :::81->80/tcp mynginx
[root@server ~]#
访问Nginx
:
[root@server ~]# curl 127.0.0.1
curl: (7) Failed connect to 127.0.0.1:80; Connection refused
[root@server ~]# curl 127.0.0.1:81
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
3.2 挂载数据卷
3.2.1 自定义数据卷目录
-v
容器外目录:容器内目录
此时容器外目录的内容会直接显示在容器内目录中
容器内目录中若存在文件,则会被清空!
[root@server ~]# mkdir /html
[root@server ~]# touch /html/index.html[root@server ~]# docker run -d --rm --name nginx_baidu -p 81:80 -v /html:/usr/share/nginx/html nginx
3c2492f1d1afee0f5d8d376c3e9991a5615138a78cd53875173d7f01fd3aeb0f[root@server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3c2492f1d1af nginx "/docker-entrypoint.…" 11 seconds ago Up 10 seconds 0.0.0.0:81->80/tcp, :::81->80/tcp nginx_baidu
[root@server ~]# [root@server ~]# docker exec -it nginx_baidu /bin/sh
# ls /usr/share/nginx/html
index.html
3.2.2 自动数据卷目录
[root@server ~]# docker run -d --rm --name nginx_baidu -p 81:80 -v aaa:/usr/share/nginx/html nginx
随意命名宿主机的目录名,此时Docker会在找不到该路径后,自动在/var/lib/docker/volumes/
下创建数据卷,同时将容器内映射目录中的内容全部复制到该自动创建的数据卷目录中;多个容器可同时共享该数据卷。
3.3 传递环境变量
-e XXX=yyy
[root@server ~]# docker run --rm -e OPS=abcdef nginx printenv
HOSTNAME=dfb69fa2b394
HOME=/root
PKG_RELEASE=1~bookworm
NGINX_VERSION=1.25.3
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
NJS_VERSION=0.8.2
OPS=abcdef
PWD=/
3.4 容器内安装软件
[root@server ~]# docker exec -it 3c2492f1d1af /bin/bash
root@3c2492f1d1af:/# apt-get install wget
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package wget # 报错:无法找到wget软件,此时就需要更新updateroot@3c2492f1d1af:/# apt-get update
Get:1 http://deb.debian.org/debian bookworm InRelease [151 kB]
Get:2 http://deb.debian.org/debian bookworm-updates InRelease [52.1 kB]
Get:3 http://deb.debian.org/debian-security bookworm-security InRelease [48.0 kB]
Get:4 http://deb.debian.org/debian bookworm/main amd64 Packages [8787 kB]
Get:5 http://deb.debian.org/debian bookworm-updates/main amd64 Packages [12.7 kB]
Get:6 http://deb.debian.org/debian-security bookworm-security/main amd64 Packages [134 kB]
Fetched 9185 kB in 3s (2632 kB/s)
Reading package lists... Doneroot@3c2492f1d1af:/# apt-get install wget
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:wget
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 984 kB of archives.
After this operation, 3692 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian bookworm/main amd64 wget amd64 1.21.3-1+b2 [984 kB]
Fetched 984 kB in 2s (484 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package wget.
(Reading database ... 7590 files and directories currently installed.)
Preparing to unpack .../wget_1.21.3-1+b2_amd64.deb ...
Unpacking wget (1.21.3-1+b2) ...
Setting up wget (1.21.3-1+b2) ...