EP4 Docker容器的基础使用
启动Nginx容器
从仓库拉取nginx镜像
% docker pull nginx:1.22.0
1.22.0: Pulling from library/nginx
214ca5fb9032: Pull complete
43cbb497c326: Pull complete
51c4910349a0: Pull complete
f88e73a4c2d1: Pull complete
6a3fe55fabfa: Pull complete
d766a4b2ae83: Pull complete
Digest: sha256:976d3bee73b8111e9e8035ccecaf2fc935c9b7941a04788c1634e8a3b290c497
Status: Downloaded newer image for nginx:1.22.0
docker.io/library/nginx:1.22.0
% docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.22.0 ae3149f43818 33 hours ago 142MB
运行并启动nginx容器
% docker run --name my-nginx -p 8080:80 -d nginx:1.22.0
92f2acf8de66d039c3e520fb42978c92065fdb9f61dac45cddff8aa72f4dfd6c
# --name my-nginx : 容器名称
# -p 8080:80 : 暴露端口 宿主机端口:容器端口
# -d : 后台运行
# nginx:1.22.0 : 镜像名称以及tag
启动后在宿主机通过浏览器访问http://localhost:8080/
出现上图页面证明nginx启动成功
日志查看
% docker logs -f --tail=30 my-nginx
172.17.0.1 - - [14/Jul/2022:06:21:32 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36" "-"
2022/07/14 06:24:24 [notice] 1#1: signal 15 (SIGTERM) received, exiting
2022/07/14 06:24:24 [notice] 25#25: signal 15 (SIGTERM) received, exiting
2022/07/14 06:24:24 [notice] 25#25: exiting
2022/07/14 06:24:24 [notice] 25#25: exit
2022/07/14 06:24:24 [notice] 26#26: signal 15 (SIGTERM) received, exiting
2022/07/14 06:24:24 [notice] 26#26: exiting
2022/07/14 06:24:24 [notice] 26#26: exit
2022/07/14 06:24:25 [notice] 1#1: signal 17 (SIGCHLD) received from 26
2022/07/14 06:24:25 [notice] 1#1: worker process 26 exited with code 0
2022/07/14 06:24:25 [notice] 1#1: signal 29 (SIGIO) received
2022/07/14 06:24:25 [notice] 1#1: signal 17 (SIGCHLD) received from 25
2022/07/14 06:24:25 [notice] 1#1: worker process 25 exited with code 0
2022/07/14 06:24:25 [notice] 1#1: exit
/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: IPv6 listen already enabled
/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
2022/07/14 07:37:09 [notice] 1#1: using the "epoll" event method
2022/07/14 07:37:09 [notice] 1#1: nginx/1.22.0
2022/07/14 07:37:09 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/07/14 07:37:09 [notice] 1#1: OS: Linux 5.10.104-linuxkit
2022/07/14 07:37:09 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/07/14 07:37:09 [notice] 1#1: start worker processes
2022/07/14 07:37:09 [notice] 1#1: start worker process 25
2022/07/14 07:37:09 [notice] 1#1: start worker process 26
172.17.0.1 - - [14/Jul/2022:07:38:28 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36" "-"
元数据
% docker inspect my-nginx
[
{
"Id": "92f2acf8de66d039c3e520fb42978c92065fdb9f61dac45cddff8aa72f4dfd6c",
"Created": "2022-05-27T03:35:11.039699Z",
"Path": "/docker-entrypoint.sh",
"Args": [
"nginx",
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 2183,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-07-14T07:37:09.351601178Z",
"FinishedAt": "2022-07-14T06:31:55.644950081Z"
},
"Image": "sha256:ae3149f43818854f4b96a1274eabb218129b4a6f336d582faa2e27631ad5b786",
"ResolvConfPath": "/var/lib/docker/containers/92f2acf8de66d039c3e520fb42978c92065fdb9f61dac45cddff8aa72f4dfd6c/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/92f2acf8de66d039c3e520fb42978c92065fdb9f61dac45cddff8aa72f4dfd6c/hostname",
"HostsPath": "/var/lib/docker/containers/92f2acf8de66d039c3e520fb42978c92065fdb9f61dac45cddff8aa72f4dfd6c/hosts",
"LogPath": "/var/lib/docker/containers/92f2acf8de66d039c3e520fb42978c92065fdb9f61dac45cddff8aa72f4dfd6c/92f2acf8de66d039c3e520fb42978c92065fdb9f61dac45cddff8aa72f4dfd6c-json.log",
"Name": "/my-nginx",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"80/tcp": [
{
"HostIp": "",
"HostPort": "8080"
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
...more
查看容器进程
% docker top my-nginx
UID PID PPID C STIME TTY TIME CMD
root 2183 2156 0 07:37 ? 00:00:00 nginx: master process nginx -g daemon off;
uuidd 2229 2183 0 07:37 ? 00:00:00 nginx: worker process
uuidd 2230 2183 0 07:37 ? 00:00:00 nginx: worker process
性能监控
% docker stats my-nginx
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
92f2acf8de66 my-nginx 0.00% 6.824MiB / 3.843GiB 0.17% 3.24kB / 850B 10.8MB / 4.1kB 3
进入/退出容器
exec:通过启动终端进入容器
进入容器
使用exec命令进入容器
% docker exec -it my-nginx bash
root@92f2acf8de66:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
*有的容器不支持bash命令,可以尝试使用sh命令
退出容器
使用exit命令退出即可
[root@localhost ~]# docker exec -it docker-nginx bash
root@ffc704495173:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
root@ffc704495173:/# exit
exit
[root@localhost ~]#
attach:连接到正在运行的容器终端上
进入容器
[root@localhost ~]# docker attach --sig-proxy=false docker-nginx
192.168.1.125 - - [11/Aug/2022:07:15:06 +0000] "GET / HTTP/1.1" 400 339 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
192.168.1.125 - - [11/Aug/2022:07:15:06 +0000] "GET /favicon.ico HTTP/1.1" 400 350 "http://docker.uctimes.cn/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
^C
[root@localhost ~]#
传输复制文件
将文件从容器复制到本地
# 进入容器
% docker exec -it my-nginx bash
root@92f2acf8de66:/# cd /home
root@92f2acf8de66:/home# ls
root@92f2acf8de66:/home# echo "teststestsetsetsetsetsetseat" >> test.txt
root@92f2acf8de66:/home# cat test.txt
teststestsetsetsetsetsetseat
root@92f2acf8de66:/home# ls
test.txt
# 在宿主机执行复制命令
% docker cp my-nginx:/home/test.txt ~/
% ls | grep test
test.txt
% echo "test1test1" >> test1.txt
% ls |grep test1
test1.txt
% docker cp ~/test1.txt my-nginx:/home/
root@92f2acf8de66:/home# ls
test.txt test1.txt
查看容器
% docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
92f2acf8de66 nginx:1.22.0 "/docker-entrypoint.…" 2 months ago Up 2 seconds 0.0.0.0:8080->80/tcp my-nginx
停止容器
% docker stop my-nginx
my-nginx
删除容器
% docker rm my-nginx
my-nginx
% docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
04594873b7de nacos/nacos-server:v2.0.4 "bin/docker-startup.…" 3 months ago Exited (143) 2 minutes ago nacos-standalone-mysql
b246e0b1616b nacos/nacos-mysql:5.7 "docker-entrypoint.s…" 4 months ago Exited (0) 3 months ago mysql