容器-Docker《三》容器管理
下载镜像只是相当于将软件下载下来安装好,但是并不代表把它运行起来,类似于root@ubuntu2204:~# apt install nginx = docker pull nginx ,然而进行运行起来就变成了容器,镜像只是模板文件,容器复制一份模板文件生成相关进程对外提供服务,随着运行的时间推移容器除了模板文件还会生成别的文件。
那么一个机器里面可以运行两个容器跑两个nginx吗?
root@ubuntu2204:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be01627007fb nginx:latest "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp priceless_nightingale
8dd8f5328dff nginx:latest "/docker-entrypoint.…" 34 minutes ago Up 10 minutes 80/tcp boring_goldstine
root@ubuntu2204:~# ps axu|grep nginx
root 6576 0.0 0.1 8856 5516 ? Ss 12:53 0:00 nginx: master process nginx -g daemon off;
systemd+ 6628 0.0 0.0 9276 2672 ? S 12:53 0:00 nginx: worker process
systemd+ 6629 0.0 0.0 9276 2672 ? S 12:53 0:00 nginx: worker process
root 6655 0.0 1.2 1273784 49348 pts/6 Sl+ 13:02 0:00 docker run nginx:latest
root 6724 0.0 0.1 8856 5508 ? Ss 13:02 0:00 nginx: master process nginx -g daemon off;
systemd+ 6774 0.0 0.0 9276 2588 ? S 13:02 0:00 nginx: worker process
systemd+ 6775 0.0 0.0 9276 2588 ? S 13:02 0:00 nginx: worker process
root 6908 0.0 0.0 6608 2264 pts/8 R+ 13:08 0:00 grep --color=auto nginx
容器是一个在主机上运行的进程 ,主机可以是本地主机,也可以是远程主机。运行的容器进程被隔离,容器有自己的文件系统、自己的网络和自己的 独立于主机的独立进程树。
image:文件模板,仅是文件,只消耗磁盘空间,静态概念;container:复制模板文件生成新的文件和进程,即消耗磁盘空间也消耗内存空间,动态概念,有生命期。
1,容器生命周期
root@ubuntu2204:~# docker container --help
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes
Run 'docker container COMMAND --help' for more information on a command.
root@ubuntu2204:~#
容器启动流程
Docker----Docker容器的启动流程:https://developer.aliyun.com/article/920837
为了后续做实验,将Docker数据根目录更改为/data/docker ,和添加镜像加速器。
root@ubuntu2204:~# docker info
Client:
Context: default
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 20.10.12
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: systemd
Cgroup Version: 2
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runtime.v1.linux runc io.containerd.runc.v2
Default Runtime: runc
Init Binary: docker-init
containerd version:
runc version:
init version:
Security Options:
apparmor
seccomp
Profile: default
cgroupns
Kernel Version: 5.15.0-43-generic
Operating System: Ubuntu 22.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.8GiB
Name: ubuntu2204
ID: KZWM:XV2H:TECS:YALE:RV7H:IMVK:BITA:QTRO:52DQ:POVV:WL5I:YQVO
Docker Root Dir: /var/lib/docker #默认数据根目录
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
#停止Dokcer服务
root@ubuntu2204:~# sudo systemctl stop docker
Warning: Stopping docker.service, but it can still be activated by:
docker.socket
root@ubuntu2204:~# sudo systemctl stop docker.socket
root@ubuntu2204:~# sudo systemctl stop containerd
#转移 root 目录为/data/docker
root@ubuntu2204:~# mkdir -p /data/docker
root@ubuntu2204:~# mv /var/lib/docker/ /data/
#编写 daemon.json,添加如下内容
root@ubuntu2204:~# sudo vim /etc/docker/daemon.json
{
"data-root":"/data/docker/"
}
#重启docker服务
root@ubuntu2204:~# sudo systemctl start docker
root@ubuntu2204:~# docker info
Client:
Context: default
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 20.10.12
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: systemd
Cgroup Version: 2
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version:
runc version:
init version:
Security Options:
apparmor
seccomp
Profile: default
cgroupns
Kernel Version: 5.15.0-43-generic
Operating System: Ubuntu 22.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.8GiB
Name: ubuntu2204
ID: KZWM:XV2H:TECS:YALE:RV7H:IMVK:BITA:QTRO:52DQ:POVV:WL5I:YQVO
Docker Root Dir: /data/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
root@ubuntu2204:~#
添加镜像加速器如果之前有了别的定义,也写在大括号里面用逗号隔开。
root@ubuntu2204:~# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://uietgfqt.mirror.aliyuncs.com"]
}
root@ubuntu2204:~# sudo systemctl daemon-reload
root@ubuntu2204:~# sudo systemctl restart docker
root@ubuntu2204:~# sudo systemctl daemon-reload ; sudo systemctl restart docker
root@ubuntu2204:~# docker info
Client:
Context: default
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 20.10.12
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: systemd
Cgroup Version: 2
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version:
runc version:
init version:
Security Options:
apparmor
seccomp
Profile: default
cgroupns
Kernel Version: 5.15.0-43-generic
Operating System: Ubuntu 22.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.8GiB
Name: ubuntu2204
ID: KZWM:XV2H:TECS:YALE:RV7H:IMVK:BITA:QTRO:52DQ:POVV:WL5I:YQVO
Docker Root Dir: /data/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://uietgfqt.mirror.aliyuncs.com/
Live Restore Enabled: false
root@ubuntu2204:~#
root@ubuntu2204:~# cat /etc/docker/daemon.json
{
"data-root":"/data/docker/",
"registry-mirrors": ["https://uietgfqt.mirror.aliyuncs.com"]
}
root@ubuntu2204:~#
2,容器命令行CLI
2.1 容器创建
docker create
创建容器实际上就是复制image,命令docker create
用法
oot@ubuntu2204:~# docker create --help
Usage: docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
Create a new container
Options:
。。。。
--name string Assign a name to the container #在创建容器时指定容器名称
示例
root@ubuntu2204:~# du -sh /data/docker/
244K /data/docker/
root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
root@ubuntu2204:~# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 12 months ago 141MB
root@ubuntu2204:~# docker create nginx:latest
8dd8f5328dffe3a1173cfe120fae47484ad582e1273a127916fdff8e60ff4bc8
root@ubuntu2204:~# du -sh /data/docker/
151M /data/docker/
root@ubuntu2204:~#
2.2 查看容器
docker -ps
默认显示运行状态的容器,
-a 显示所有状态的容器。
root@ubuntu2204:~# docker ps --help
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states)
(default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display container IDs
-s, --size Display total file sizes
root@ubuntu2204:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8dd8f5328dff nginx:latest "/docker-entrypoint.…" 10 minutes ago Created boring_goldstine
容器本质上进程,创建容器只是复制image,但是还没有运行起来,所以看不到进程。此时,并且数据根目录大小也没变化。
root@ubuntu2204:~# ps aux|grep nginx
root 6484 0.0 0.0 6476 2260 pts/6 S+ 12:44 0:00 grep --color=auto nginx
root@ubuntu2204:~# du -sh /data/docker
151M /data/docker
root@ubuntu2204:~# ll /data/docker/containers/
total 12
drwx--x--- 3 root root 4096 Jan 3 12:29 ./
drwx--x--- 13 root root 4096 Jan 3 12:14 ../
drwx--x--- 3 root root 4096 Jan 3 12:29 8dd8f5328dffe3a1173cfe120fae47484ad582e1273a127916fdff8e60ff4bc8/
root@ubuntu2204:~# tree /data/docker/containers/8dd8f5328dffe3a1173cfe120fae47484ad582e1273a127916fdff8e60ff4bc8/
/data/docker/containers/8dd8f5328dffe3a1173cfe120fae47484ad582e1273a127916fdff8e60ff4bc8/
├── checkpoints
├── config.v2.json
└── hostconfig.json
1 directory, 2 files
root@ubuntu2204:~#
示例:指定名字
可以通过三种方式识别容器:
2.3 启动容器
docker start
docker start 容器ID或名称
root@ubuntu2204:~# docker start --help
Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]
Start one or more stopped containers
Options:
-a, --attach Attach STDOUT/STDERR and forward signals
--detach-keys string Override the key sequence for detaching a container
-i, --interactive Attach container's STDIN
root@ubuntu2204:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8dd8f5328dff nginx:latest "/docker-entrypoint.…" 23 minutes ago Created boring_goldstine
root@ubuntu2204:~# du -sh /data/docker
151M /data/docker
root@ubuntu2204:~# docker start boring_goldstine
boring_goldstine
root@ubuntu2204:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8dd8f5328dff nginx:latest "/docker-entrypoint.…" 23 minutes ago Up 4 seconds 80/tcp boring_goldstine
容器启动之后数据目录变大,也增加了一些文件。
root@ubuntu2204:~# du -sh /data/docker
298M /data/docker
root@ubuntu2204:~# tree /data/docker/containers/8dd8f5328dffe3a1173cfe120fae47484ad582e1273a127916fdff8e60ff4bc8/
/data/docker/containers/8dd8f5328dffe3a1173cfe120fae47484ad582e1273a127916fdff8e60ff4bc8/
├── 8dd8f5328dffe3a1173cfe120fae47484ad582e1273a127916fdff8e60ff4bc8-json.log
├── checkpoints
├── config.v2.json
├── hostconfig.json
├── hostname
├── hosts
├── mounts
├── resolv.conf
└── resolv.conf.hash
2 directories, 7 files
root@ubuntu2204:~#
root@ubuntu2204:~# ls /data/docker/volumes/
backingFsBlockDev metadata.db
root@ubuntu2204:~# ls /data/docker/
buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes
root@ubuntu2204:~# ls /data/docker/overlay2/
274af8f4099d035812b99a2d45652cd4dca273d7db9bb3d9eada764eaefd5b40 73cbb9671db1515f18bdf3e274b9cf836ff3b9c925adb438f79da63e1200c056
36980a6dd0f318d40fa2ceb48b4d27e81833c95ea89f94776c868f32bd1a34a9 8fef2c8d09ae78da14b279bc1ab1a07de713c530dd8ec53c76c5dd9573c29f11
4511584cd776f33c45876483b8cd80962695b3526cf34ffbfef9b524aa73e121 9ba8ba2a6fece861e26c40f2c3ab77806f3d9496eccc208a6b6ef19e1af9506d
4511584cd776f33c45876483b8cd80962695b3526cf34ffbfef9b524aa73e121-init l
657928bd618dd8de2b94bc3915f31eb124dde49b36a5f0c568029ec936dadac3
root@ubuntu2204:~# du -sh /data/docker/overlay2/274af8f4099d035812b99a2d45652cd4dca273d7db9bb3d9eada764eaefd5b40/
28K /data/docker/overlay2/274af8f4099d035812b99a2d45652cd4dca273d7db9bb3d9eada764eaefd5b40/
root@ubuntu2204:~# ls /data/docker/overlay2/ -t
4511584cd776f33c45876483b8cd80962695b3526cf34ffbfef9b524aa73e121 73cbb9671db1515f18bdf3e274b9cf836ff3b9c925adb438f79da63e1200c056
4511584cd776f33c45876483b8cd80962695b3526cf34ffbfef9b524aa73e121-init 8fef2c8d09ae78da14b279bc1ab1a07de713c530dd8ec53c76c5dd9573c29f11
l 657928bd618dd8de2b94bc3915f31eb124dde49b36a5f0c568029ec936dadac3
36980a6dd0f318d40fa2ceb48b4d27e81833c95ea89f94776c868f32bd1a34a9 9ba8ba2a6fece861e26c40f2c3ab77806f3d9496eccc208a6b6ef19e1af9506d
274af8f4099d035812b99a2d45652cd4dca273d7db9bb3d9eada764eaefd5b40
root@ubuntu2204:~# du -sh /data/docker/overlay2/4511584cd776f33c45876483b8cd80962695b3526cf34ffbfef9b524aa73e121
148M /data/docker/overlay2/4511584cd776f33c45876483b8cd80962695b3526cf34ffbfef9b524aa73e121
root@ubuntu2204:~#
用同一个镜像在创建一个容器,也会在复制一份镜像文件。
root@ubuntu2204:~# du -sh /data/docker/
298M /data/docker/
root@ubuntu2204:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8dd8f5328dff nginx:latest "/docker-entrypoint.…" 31 minutes ago Up 7 minutes 80/tcp boring_goldstine
root@ubuntu2204:~# docker run nginx:latest
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/01/03 13:02:34 [notice] 1#1: using the "epoll" event method
2023/01/03 13:02:34 [notice] 1#1: nginx/1.21.5
2023/01/03 13:02:34 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2023/01/03 13:02:34 [notice] 1#1: OS: Linux 5.15.0-43-generic
2023/01/03 13:02:34 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/01/03 13:02:34 [notice] 1#1: start worker processes
2023/01/03 13:02:34 [notice] 1#1: start worker process 31
2023/01/03 13:02:34 [notice] 1#1: start worker process 32 #默认前台运行,-d后台运行
root@ubuntu2204:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be01627007fb nginx:latest "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp priceless_nightingale
8dd8f5328dff nginx:latest "/docker-entrypoint.…" 34 minutes ago Up 10 minutes 80/tcp boring_goldstine
root@ubuntu2204:~# ps axu|grep nginx
root 6576 0.0 0.1 8856 5516 ? Ss 12:53 0:00 nginx: master process nginx -g daemon off;
systemd+ 6628 0.0 0.0 9276 2672 ? S 12:53 0:00 nginx: worker process
systemd+ 6629 0.0 0.0 9276 2672 ? S 12:53 0:00 nginx: worker process
root 6655 0.0 1.2 1273784 49348 pts/6 Sl+ 13:02 0:00 docker run nginx:latest
root 6724 0.0 0.1 8856 5508 ? Ss 13:02 0:00 nginx: master process nginx -g daemon off;
systemd+ 6774 0.0 0.0 9276 2588 ? S 13:02 0:00 nginx: worker process
systemd+ 6775 0.0 0.0 9276 2588 ? S 13:02 0:00 nginx: worker process
root 6908 0.0 0.0 6608 2264 pts/8 R+ 13:08 0:00 grep --color=auto nginx
root@ubuntu2204:~# du -sh /data/docker/
445M /data/docker/
2.4 运行容器
docker run=复制image生成容器docker create+docker start,容器的创建实际上就是复制image文件。
运行容器基本命令采用以下形式:docker run ,容器创建并启动
容器必须要有一个前台运行的进程,才能不退出。
–privileged 向此容器授予扩展权限,让容器可以控制宿主机,这个比较危险,谨慎使用。
docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
#该命令必须指定要生成容器的 IMAGE。
#选项:
-i, --interactive Keep STDIN open even if not attached,通常和-t一起使用
-t, --tty 分配pseudo-TTY,通常和-i一起使用,注意对应的容器必须运行shell才支持进入
-d, --detach Run container in background and print container ID,台后运行,默认前台
--volume,-v 绑定装载卷
--rm 容器退出时自动移除容器
--publish,-p 将容器的端口发布到主机
--publish-all,-P 将所有公开的端口发布到随机端口
--env,-e 设置环境变量
示例
docker run -d -p 80:80 my_image service nginx start
#Ubuntu默认是前台运行,运行就退出,-i交互,-t分配终端
root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 12 months ago 141MB
ubuntu jammy 9d28ccdc1fc7 13 months ago 76.3MB
root@ubuntu2204:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be01627007fb nginx:latest "/docker-entrypoint.…" 2 hours ago Up 9 minutes 80/tcp priceless_nightingale
root@ubuntu2204:~# docker run ubuntu:jammy
root@ubuntu2204:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
feac00c812fa ubuntu:jammy "bash" 3 seconds ago Exited (0) 3 seconds ago eager_shaw
be01627007fb nginx:latest "/docker-entrypoint.…" 2 hours ago Up 10 minutes 80/tcp priceless_nightingale
root@ubuntu2204:~#
#容器Ubuntu交互式
root@ubuntu2204:~# docker run -it ubuntu:jammy
root@52c36c5079d5:/# ls
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
root@52c36c5079d5:/# pwd
/
root@52c36c5079d5:/#
root@52c36c5079d5:/# exit
exit
root@ubuntu2204:~#
#容器Ubuntu后台运行,-d
2.4.1 docker命令后台运行
#docker命令后台运行,-d
root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine 3.16.2 9c6f07244728 4 months ago 5.54MB
nginx latest 605c77e624dd 12 months ago 141MB
ubuntu jammy 9d28ccdc1fc7 13 months ago 76.3MB
root@ubuntu2204:~# docker run -d alpine:3.16.2 sleep 100
b7788f0d576400168d52c2b748ae3aad0b900ac729db4e576a4ad2d21781005f
root@ubuntu2204:~