Docker学习

本文最后更新于:2 个月前

Docker学习

尚硅谷3小时速通Docker教程,雷神带练docker部署到实战

Docker Docs

Docker - 快速通关 (3h) (yuque.com)

一、基础

1. 认识Docker

Screenshot_2024-06-04-13-22-55-815_tv.danmaku.bili

image-20240604225111233

2. 安装Docker

参考文档:Install Docker Engine on CentOS | Docker Docs

2.1 卸载旧版本

1
2
3
4
5
6
7
8
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

2.2 安装

1
sudo yum install -y yum-utils
1
2
# sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
1
sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

2.3 启动

1
2
3
4
5
6
7
8
sudo systemctl start docker

# 启动docker
systemctl start docker.service
# 开机自启
systemctl enable docker.service
# 重启
systemctl restart docker.service

配置docker镜像加速

1
2
3
4
5
sudo mkdir -p /etc/docker
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}

或者

1
2
3
4
5
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://mirror.ccs.tencentyun.com"]
}
EOF
1
2
sudo systemctl daemon-reload
sudo systemctl restart docker

二、命令

1. 镜像操作

Docker Hub Container Image Library | App Containerization

1.1 下载镜像(image)

镜像名:标签(版本)

  • 检索:docker search 镜像名

image-20240604230048976

  • 下载:docker pull 镜像名

示例:

1
2
3
docker pull nginx
docker pull nginx:latest
docker pull nginx:stable-alpine3.19-perl

image-20240604234354211

  • 列表:docker images / docker image ls
1
2
3
4
5
6
7
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest e784f4560448 4 weeks ago 188MB
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest e784f4560448 4 weeks ago 188MB
[root@iZ2zeb7ybli3zzdll6airpZ ~]#

image-20240604230211334

  • 删除:docker rmi 镜像名

1.2 启动容器(container)

  • 运行:docker run
1
2
3
4
5
# 创建并运行一个容器,处于运行状态。(前台运行)
docker run nginx
# 参数说明
docker run --name [容器名] -p 宿主机端口号:容器内部端口号 -d [镜像名称]
docker run -d --name stock_app -p 80:80 nginx

image-20240605000341887

  • 查看:docker ps
1
2
3
4
5
6
# 查看运行中容器
docker ps
# 查看所有容器
docker ps -a
# 查看所有容器ID(CONTAINER ID)
docker ps -aq
1
2
3
4
5
6
7
8
9
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d76bb85c0461 nginx "/docker-entrypoint.…" 7 hours ago Up 7 hours 0.0.0.0:80->80/tcp, :::80->80/tcp stock_app
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d76bb85c0461 nginx "/docker-entrypoint.…" 7 hours ago Up 7 hours 0.0.0.0:80->80/tcp, :::80->80/tcp stock_app
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker ps -aq
d76bb85c0461
[root@iZ2zeb7ybli3zzdll6airpZ ~]#
  • 停止:docker stop
  • 启动:docker start
  • 重启:docker restart
  • 状态:docker stats
1
2
3
4
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
d76bb85c0461 stock_app 0.00% 5.285MiB / 3.523GiB 0.15% 28.5MB / 28.6MB 3.47MB / 0B 3

  • 日志:docker logs
  • 进入:docker exec
1
2
3
4
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker exec -it stock_app /bin/bash
root@d76bb85c0461:/# ls
bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@d76bb85c0461:/#
1
2
# 简写
docker exec -it stock_app bash
1
2
3
nginx容器内部默认路径:
静态页面路径:/usr/share/nginx/html
配置文件路径:/etc/nginx
  • 删除:docker rm
1
2
3
4
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d76bb85c0461 nginx "/docker-entrypoint.…" 7 hours ago Up 7 hours 0.0.0.0:80->80/tcp, :::80->80/tcp stock_app
[root@iZ2zeb7ybli3zzdll6airpZ ~]#
1
2
3
4
5
6
7
# 删除停止状态的容器:
docker rm d76bb85c0461 / docker rm stock_app
# 强制删除容器(包括运行中的容器):
docker rm -f d76bb85c0461
# 批量删除容器
docker rm $(docker ps -aq)
docker rm -f $(docker ps -aq)

1.3 修改页面

1.4 保存镜像

image-20240605001320049

  • 提交:docker commit
1
2
3
4
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest e784f4560448 4 weeks ago 188MB
[root@iZ2zeb7ybli3zzdll6airpZ ~]#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker commit --help

Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

Aliases:
docker container commit, docker commit

Options:
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
[root@iZ2zeb7ybli3zzdll6airpZ ~]#
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker commit -m "update nginx page" stock_app stock_app:v1.0
  • 保存:docker save
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker save --help

Usage: docker save [OPTIONS] IMAGE [IMAGE...]

Save one or more images to a tar archive (streamed to STDOUT by default)

Aliases:
docker image save, docker save

Options:
-o, --output string Write to a file, instead of STDOUT
[root@iZ2zeb7ybli3zzdll6airpZ ~]#
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker save -o stock_app.tar stock_app:v1.0
  • 加载:docker load
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker load --help

Usage: docker load [OPTIONS]

Load an image from a tar archive or STDIN

Aliases:
docker image load, docker load

Options:
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output
[root@iZ2zeb7ybli3zzdll6airpZ ~]#
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker load -i stock_app.tar

1.5 分享社区

Docker Hub

  • 登录:docker login
  • 命名:docker tag
1
2
3
4
5
6
7
8
9
10
11
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker tag --help

Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Aliases:
docker image tag, docker tag
[root@iZ2zeb7ybli3zzdll6airpZ ~]#
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker tag stock_app:v1.0 xxx/stock_app:v1.0
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker images
  • 推送:docker push
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker push --help

Usage: docker push [OPTIONS] NAME[:TAG]

Upload an image to a registry

Aliases:
docker image push, docker push

Options:
-a, --all-tags Push all tags of an image to the repository
--disable-content-trust Skip image signing (default true)
-q, --quiet Suppress verbose output
[root@iZ2zeb7ybli3zzdll6airpZ ~]#
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker push xxx/stock_app:v1.0
1
2
3
# 生成默认版本
docker tag xxx/stock_app:v1.0 xxx/stock_app:latest
docker push xxx/stock_app:latest

2. 容器操作

  • 运行:docker run
  • 查看:docker ps
  • 停止:docker stop
  • 启动:docker start
  • 重启:docker restart
  • 状态:docker stats
  • 日志:docker logs
  • 进入:docker exec
  • 删除:docker rm

image-20240605003232402

三、存储

1. 目录挂载

1
2
# 目录挂载:
-v /app/nghtml:/usr/share/nginx/html
1
2
3
4
5
6
# 示例 宿主机端口号:容器内部端口号
# -d 保持后台运行
# --name 设置容器名称
# -p 端口映射,宿主机端口:容器内部端口
# -v 目录挂载,宿主机文件夹:容器内部文件夹
docker run -d --name stock_app -p 80:80 -v /home/ruoyi/stock_app/dist:/usr/share/nginx/html nginx

image-20240605004310652

2. 卷挂载

1
2
3
4
5
# 卷挂载
# ngconf 卷名称
-v ngconf:/etc/nginx
# 卷路径:
/var/lib/docker/volumes/<volume-name>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker run -d -p 80:80 -v /home/ruoyi/stock_app/dist:/usr/share/nginx/html -v ngconf:/etc/nginx --name stock_app nginx
aebba039eb32e8c5030ea0e4dc07277ccdfdcc3c92fe8d9376a3c3c35fb89d7a
[root@iZ2zeb7ybli3zzdll6airpZ ~]#
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aebba039eb32 nginx "/docker-entrypoint.…" 25 seconds ago Up 24 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp stock_app
[root@iZ2zeb7ybli3zzdll6airpZ ~]#
[root@iZ2zeb7ybli3zzdll6airpZ ~]# cd /var/lib/docker/volumes/
[root@iZ2zeb7ybli3zzdll6airpZ volumes]# ls
backingFsBlockDev metadata.db ngconf
[root@iZ2zeb7ybli3zzdll6airpZ volumes]#
[root@iZ2zeb7ybli3zzdll6airpZ _data]# pwd
/var/lib/docker/volumes/ngconf/_data
[root@iZ2zeb7ybli3zzdll6airpZ _data]# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
[root@iZ2zeb7ybli3zzdll6airpZ _data]#
[root@iZ2zeb7ybli3zzdll6airpZ _data]#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 查看卷
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker volume ls
DRIVER VOLUME NAME
local ngconf
# 创建卷
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker volume create temp
temp
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker volume ls
DRIVER VOLUME NAME
local ngconf
local temp
# 查看卷路径
[root@iZ2zeb7ybli3zzdll6airpZ ~]# docker volume inspect ngconf
[
{
"CreatedAt": "2024-06-05T00:54:05+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/ngconf/_data",
"Name": "ngconf",
"Options": null,
"Scope": "local"
}
]
[root@iZ2zeb7ybli3zzdll6airpZ ~]#

image-20240605011157790


nginx配置文件示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
[root@iZ2zeb7ybli3zzdll6airpZ _data]# cat nginx.conf 

user nginx;
worker_processes auto;

error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;


events {
worker_connections 1024;
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

include /etc/nginx/conf.d/*.conf;

server {
listen 80;
server_name localhost;
location / {
root /home/ruoyi/stock_app/dist;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}

location /prod-api/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8081/;
}
}
}
[root@iZ2zeb7ybli3zzdll6airpZ _data]#

四、网络

五、其他


Docker学习
https://chris-z-su.github.io/2024/06/04/技术/Docker学习/
作者
Chris
发布于
2024年6月4日
许可协议