docker学习笔记

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 Tair(兼容Redis),内存型 2GB
简介: docker学习笔记

docker学习笔记

-官方文档
官方文档

离线安装docker

离线安装docker

搜索docker镜像

搜索docker镜像

[root@localhost ~]# docker search nginx
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                             Official build of Nginx.                        15910               [OK]                
jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   2098                                    [OK]
richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   820                                     [OK]
jc21/nginx-proxy-manager          Docker container for managing Nginx proxy ho…   286                                     
linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   160                                     
tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   147                                     [OK]
jlesage/nginx-proxy-manager       Docker container for Nginx Proxy Manager        144                                     [OK]
alfg/nginx-rtmp                   NGINX, nginx-rtmp-module and FFmpeg from sou…   110                                     [OK]
nginxdemos/hello                  NGINX webserver that serves a simple page co…   79                                      [OK]
privatebin/nginx-fpm-alpine       PrivateBin running on an Nginx, php-fpm & Al…   61                                      [OK]
nginx/nginx-ingress               NGINX and  NGINX Plus Ingress Controllers fo…   57                                      
nginxinc/nginx-unprivileged       Unprivileged NGINX Dockerfiles                  54                                      
nginxproxy/nginx-proxy            Automated Nginx reverse proxy for docker con…   28                                      
staticfloat/nginx-certbot         Opinionated setup for automatic TLS certs lo…   25                                      [OK]
nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter for NGINX and NGIN…   22                                      
schmunk42/nginx-redirect          A very simple container to redirect HTTP tra…   19                                      [OK]
centos/nginx-112-centos7          Platform for running nginx 1.12 or building …   16                                      
centos/nginx-18-centos7           Platform for running nginx 1.8 or building n…   13                                      
flashspys/nginx-static            Super Lightweight Nginx Image                   11                                      [OK]
webdevops/nginx                   Nginx container                                 9                                       [OK]
mailu/nginx                       Mailu nginx frontend                            9                                       [OK]
sophos/nginx-vts-exporter         Simple server that scrapes Nginx vts stats a…   7                                       [OK]
ansibleplaybookbundle/nginx-apb   An APB to deploy NGINX                          3                                       [OK]
arnau/nginx-gate                  Docker image with Nginx with Lua enabled on …   1                                       [OK]
wodby/nginx                       Generic nginx                                   1                                       [OK]
[root@localhost ~]# 
下载docker镜像

docker pull centos:centos7.9.2009

[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
e5ae68f74026: Pull complete 
21e0df283cd6: Pull complete 
ed835de16acd: Pull complete 
881ff011f1c9: Pull complete 
77700c52c969: Pull complete 
44be98c0fab6: Pull complete 
Digest: sha256:9522864dd661dcadfd9958f9e0de192a1fdda2c162a35668ab6ac42b465f0603
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
列出所有本地docker镜像
[root@localhost ~]# docker images
REPOSITORY                                        TAG                 IMAGE ID            CREATED             SIZE
nginx                                             latest              f652ca386ed1        21 hours ago        141MB
ferry_web                                         latest              c3a3aaaa7715        22 hours ago        160MB
ferry                                             latest              263660e16b59        2 days ago          146MB
alpine                                            latest              c059bfaa849c        8 days ago          5.59MB
redis                                             6                   40c68ed3a4d2        2 weeks ago         113MB
mysql                                             5.7                 8b43c6af2ad0        2 weeks ago         448MB
node                                              14.18-alpine        f7229193551e        2 weeks ago         118MB
golang                                            1.15                40349a2425ef        3 months ago        840MB
nginx                                             1.18.0              c2c45d506085        7 months ago        133MB
registry.cn-beijing.aliyuncs.com/lanyulei/ferry   v1.0.0-2            286bf772af1b        12 months ago       2.15GB
node                                              14.15.0             b90fa0d7cbd1        13 months ago       943MB
[root@localhost ~]# 
使用镜像centos:7以交互模式启动一个容器,在容器内执行/bin/bash命令。
[root@localhost centos]# docker run --privileged=true -d -it centos:7 /bin/bash
列出所有正在运行的docker容器
[root@localhost ferry]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                            NAMES
c48b8851b9ab        ferry_web:latest    "/entrypoint.sh"         21 hours ago        Up About an hour    80/tcp, 0.0.0.0:8001->8001/tcp   ferry_web
746ad9e65517        ferry:latest        "/opt/workflow/ferry…"   21 hours ago        Up About an hour    0.0.0.0:8002->8002/tcp           ferry_backend
62df063a474c        mysql:5.7           "docker-entrypoint.s…"   21 hours ago        Up About an hour    3306/tcp, 33060/tcp              ferry_mysql
6935b5c6f370        redis:6             "docker-entrypoint.s…"   21 hours ago        Up About an hour    6379/tcp                         ferry_redis
[root@localhost ferry]# 
停止正在运行的容器
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                            NAMES
7be73502bc36        nginx               "/docker-entrypoint.…"   11 minutes ago      Up About a minute   0.0.0.0:8080->80/tcp             nginx01
c48b8851b9ab        ferry_web:latest    "/entrypoint.sh"         22 hours ago        Up 2 hours          80/tcp, 0.0.0.0:8001->8001/tcp   ferry_web
746ad9e65517        ferry:latest        "/opt/workflow/ferry…"   22 hours ago        Up 2 hours          0.0.0.0:8002->8002/tcp           ferry_backend
62df063a474c        mysql:5.7           "docker-entrypoint.s…"   22 hours ago        Up 2 hours          3306/tcp, 33060/tcp              ferry_mysql
6935b5c6f370        redis:6             "docker-entrypoint.s…"   22 hours ago        Up 2 hours          6379/tcp                         ferry_redis
[root@localhost ~]# docker stop nginx01
nginx01
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS                            NAMES
7be73502bc36        nginx               "/docker-entrypoint.…"   11 minutes ago      Exited (0) 4 seconds ago                                    nginx01
c48b8851b9ab        ferry_web:latest    "/entrypoint.sh"         22 hours ago        Up 2 hours                 80/tcp, 0.0.0.0:8001->8001/tcp   ferry_web
746ad9e65517        ferry:latest        "/opt/workflow/ferry…"   22 hours ago        Up 2 hours                 0.0.0.0:8002->8002/tcp           ferry_backend
62df063a474c        mysql:5.7           "docker-entrypoint.s…"   22 hours ago        Up 2 hours                 3306/tcp, 33060/tcp              ferry_mysql
6935b5c6f370        redis:6             "docker-entrypoint.s…"   22 hours ago        Up 2 hours                 6379/tcp                         ferry_redis
启动docker容器
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                  PORTS                            NAMES
7be73502bc36        nginx               "/docker-entrypoint.…"   3 days ago          Exited (0) 3 days ago                                    nginx01
c48b8851b9ab        ferry_web:latest    "/entrypoint.sh"         4 days ago          Up 24 seconds           80/tcp, 0.0.0.0:8001->8001/tcp   ferry_web
746ad9e65517        ferry:latest        "/opt/workflow/ferry…"   4 days ago          Up 17 seconds           0.0.0.0:8002->8002/tcp           ferry_backend
62df063a474c        mysql:5.7           "docker-entrypoint.s…"   4 days ago          Up 24 seconds           3306/tcp, 33060/tcp              ferry_mysql
6935b5c6f370        redis:6             "docker-entrypoint.s…"   4 days ago          Up 24 seconds           6379/tcp                         ferry_redis
[root@localhost ~]# docker start 7be73502bc36
7be73502bc36
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                            NAMES
7be73502bc36        nginx               "/docker-entrypoint.…"   3 days ago          Up 3 seconds        0.0.0.0:8080->80/tcp             nginx01
c48b8851b9ab        ferry_web:latest    "/entrypoint.sh"         4 days ago          Up About a minute   80/tcp, 0.0.0.0:8001->8001/tcp   ferry_web
746ad9e65517        ferry:latest        "/opt/workflow/ferry…"   4 days ago          Up 58 seconds       0.0.0.0:8002->8002/tcp           ferry_backend
62df063a474c        mysql:5.7           "docker-entrypoint.s…"   4 days ago          Up About a minute   3306/tcp, 33060/tcp              ferry_mysql
6935b5c6f370        redis:6             "docker-entrypoint.s…"   4 days ago          Up About a minute   6379/tcp                         ferry_redis
[root@localhost ~]# 
以root用户交互模式进入正在运行的容器

临时退出正在运行的容器 Ctrl + p + q

[root@localhost ~]# docker exec -itu root 7be73502bc36 /bin/bash
root@7be73502bc36:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@7be73502bc36:/# cd /etc/nginx/
root@7be73502bc36:/etc/nginx# ls
conf.d    fastcgi_params    mime.types  modules  nginx.conf  scgi_params  uwsgi_params
root@7be73502bc36:/etc/nginx# 
后台运行docker镜像

docker端口映射 docker run --privileged=true -p [本地端口:镜像端口]

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                            NAMES
a7880a05d88a        nginx:latest        "/docker-entrypoint.…"   13 seconds ago      Up 9 seconds        0.0.0.0:8080->80/tcp             nginx01
c48b8851b9ab        ferry_web:latest    "/entrypoint.sh"         6 days ago          Up 5 minutes        80/tcp, 0.0.0.0:8001->8001/tcp   ferry_web
746ad9e65517        ferry:latest        "/opt/workflow/ferry…"   6 days ago          Up 5 minutes        0.0.0.0:8002->8002/tcp           ferry_backend
62df063a474c        8b43c6af2ad0        "docker-entrypoint.s…"   6 days ago          Up 5 minutes        3306/tcp, 33060/tcp              ferry_mysql
6935b5c6f370        redis:6             "docker-entrypoint.s…"   6 days ago          Up 5 minutes        6379/tcp                         ferry_redis
[root@localhost ~]# docker run --privileged=true -d --name nginx01 -p 8080:80 nginx:latest
a7880a05d88a9d8fb1cf22bf50ee4f86ea12936e86d0bc66bf4581d0e9e0a670
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                            NAMES
a7880a05d88a        nginx:latest        "/docker-entrypoint.…"   13 seconds ago      Up 9 seconds        0.0.0.0:8080->80/tcp             nginx01
c48b8851b9ab        ferry_web:latest    "/entrypoint.sh"         6 days ago          Up 5 minutes        80/tcp, 0.0.0.0:8001->8001/tcp   ferry_web
746ad9e65517        ferry:latest        "/opt/workflow/ferry…"   6 days ago          Up 5 minutes        0.0.0.0:8002->8002/tcp           ferry_backend
62df063a474c        8b43c6af2ad0        "docker-entrypoint.s…"   6 days ago          Up 5 minutes        3306/tcp, 33060/tcp              ferry_mysql
6935b5c6f370        redis:6             "docker-entrypoint.s…"   6 days ago          Up 5 minutes        6379/tcp                         ferry_redis
[root@localhost ~]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1169/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1258/master         
tcp6       0      0 :::8001                 :::*                    LISTEN      2029/docker-proxy   
tcp6       0      0 :::8002                 :::*                    LISTEN      2639/docker-proxy   
tcp6       0      0 :::8080                 :::*                    LISTEN      2855/docker-proxy   
tcp6       0      0 :::22                   :::*                    LISTEN      1169/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1258/master         
[root@localhost ~]# 

查看docker CPU内存状态
[root@localhost ~]# docker stats
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
7be73502bc36        nginx01             0.00%               3.062MiB / 1.934GiB   0.15%               1.1kB / 0B          23.6MB / 0B         5
c48b8851b9ab        ferry_web           0.00%               84.34MiB / 1.934GiB   4.26%               908B / 0B           80.2MB / 8.19kB     6
746ad9e65517        ferry_backend       0.22%               48.07MiB / 1.934GiB   2.43%               563kB / 1.08MB      139kB / 43kB        10
62df063a474c        ferry_mysql         0.17%               193.4MiB / 1.934GiB   9.76%               1.93kB / 1.07kB     123MB / 25.4MB      28
6935b5c6f370        ferry_redis         0.42%               6.656MiB / 1.934GiB   0.34%               1.08MB / 561kB      39.6MB / 0B         5
删除容器
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                            NAMES
7be73502bc36        nginx               "/docker-entrypoint.…"   3 days ago          Up About an hour    0.0.0.0:8080->80/tcp             nginx01
c48b8851b9ab        ferry_web:latest    "/entrypoint.sh"         4 days ago          Up About an hour    80/tcp, 0.0.0.0:8001->8001/tcp   ferry_web
746ad9e65517        ferry:latest        "/opt/workflow/ferry…"   4 days ago          Up About an hour    0.0.0.0:8002->8002/tcp           ferry_backend
62df063a474c        mysql:5.7           "docker-entrypoint.s…"   4 days ago          Up About an hour    3306/tcp, 33060/tcp              ferry_mysql
6935b5c6f370        redis:6             "docker-entrypoint.s…"   4 days ago          Up About an hour    6379/tcp                         ferry_redis
[root@localhost ~]# docker stop 7be73502bc36
7be73502bc36
[root@localhost ~]# docker rm 7be73502bc36
7be73502bc36
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                            NAMES
c48b8851b9ab        ferry_web:latest    "/entrypoint.sh"         4 days ago          Up About an hour    80/tcp, 0.0.0.0:8001->8001/tcp   ferry_web
746ad9e65517        ferry:latest        "/opt/workflow/ferry…"   4 days ago          Up About an hour    0.0.0.0:8002->8002/tcp           ferry_backend
62df063a474c        mysql:5.7           "docker-entrypoint.s…"   4 days ago          Up About an hour    3306/tcp, 33060/tcp              ferry_mysql
6935b5c6f370        redis:6             "docker-entrypoint.s…"   4 days ago          Up About an hour    6379/tcp                         ferry_redis
[root@localhost ~]# 
向docker容器拷贝文件
[root@localhost /]# docker cp /media/ a3ce9ee3bec4:/opt
[root@localhost /]# docker exec -it a3ce9ee3bec4 /bin/bash
root@a3ce9ee3bec4:/# cd /opt/
root@a3ce9ee3bec4:/opt# ls
media  root
[root@localhost /]# 
从docker容器拷贝文件
[root@localhost /]# docker cp a3ce9ee3bec4:/opt /mnt
[root@localhost /]# ll /mnt/opt/media/
total 4
drwxr-xr-x. 2 root root 6 Dec  7 10:37 1.sh
-rw-r--r--. 1 root root 2 Dec  7 10:37 2.sh
[root@localhost /]# 
将修改过的容器快照为新的镜像

打包出来的新镜像不带挂载设置

[root@localhost /]# docker commit -a="NoYoWiFi" -m="add /opt/1.sh" a3ce9ee3bec4 nginx02:1.0
sha256:49e81b1d231f8d7eca7e5ead579623d8864541718c31b45b6507112101bdc67f
[root@localhost /]# docker images
REPOSITORY                                        TAG                 IMAGE ID            CREATED             SIZE
nginx02                                           1.0                 49e81b1d231f        11 seconds ago      141MB
nginx                                             latest              f652ca386ed1        4 days ago          141MB
ferry_web                                         latest              c3a3aaaa7715        4 days ago          160MB
ferry                                             latest              263660e16b59        6 days ago          146MB
alpine                                            latest              c059bfaa849c        12 days ago         5.59MB
redis                                             6                   40c68ed3a4d2        2 weeks ago         113MB
mysql                                             5.7                 8b43c6af2ad0        2 weeks ago         448MB
node                                              14.18-alpine        f7229193551e        3 weeks ago         118MB
golang                                            1.15                40349a2425ef        4 months ago        840MB
nginx                                             1.18.0              c2c45d506085        8 months ago        133MB
registry.cn-beijing.aliyuncs.com/lanyulei/ferry   v1.0.0-2            286bf772af1b        12 months ago       2.15GB
node                                              14.15.0             b90fa0d7cbd1        13 months ago       943MB
docker文件挂载

使用镜像 nginx02:1.0,以后台模式启动一个容器,将容器的 80 端口映射到主机的 8080 端口,主机的目录 /home/ceshi1 映射到容器的 /opt。

[root@localhost /]# docker run --privileged=true -d -p 8080:80 --name nginx02 -v /home/ceshi:/opt nginx02:1.0
34c0aa497ee722fcdbf70a6581c0647df4c469ce6a47ebd9d4501bd9545da70e
[root@localhost home]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                            NAMES
34c0aa497ee7        nginx02:1.0         "/docker-entrypoint.…"   21 minutes ago      Up 21 minutes       0.0.0.0:8080->80/tcp             nginx02
c48b8851b9ab        ferry_web:latest    "/entrypoint.sh"         4 days ago          Up 7 hours          80/tcp, 0.0.0.0:8001->8001/tcp   ferry_web
746ad9e65517        ferry:latest        "/opt/workflow/ferry…"   4 days ago          Up 7 hours          0.0.0.0:8002->8002/tcp           ferry_backend
62df063a474c        mysql:5.7           "docker-entrypoint.s…"   4 days ago          Up 7 hours          3306/tcp, 33060/tcp              ferry_mysql
6935b5c6f370        redis:6             "docker-entrypoint.s…"   4 days ago          Up 7 hours          6379/tcp                         ferry_redis
[root@localhost home]# docker inspect 296dd28d30a3
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/home/ceshi2",
                "Destination": "/opt",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
启动mysql
[root@localhost ~]# docker run --privileged=true -d -p 13306:3306 -v /opt/mysql/etc/mysql/conf.d:/etc/mysql/conf.d -v /opt/mysql/var/lib/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
cd5572e2e0d67e9f16550baed02189177ddcfb8c0156dbff2ba1d92078967da3
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                NAMES
cd5572e2e0d6        mysql:5.7           "docker-entrypoint.s…"   7 seconds ago       Up 4 seconds        33060/tcp, 0.0.0.0:13306->3306/tcp   mysql01
c48b8851b9ab        ferry_web:latest    "/entrypoint.sh"         6 days ago          Up 4 hours          80/tcp, 0.0.0.0:8001->8001/tcp       ferry_web
746ad9e65517        ferry:latest        "/opt/workflow/ferry…"   6 days ago          Up 4 hours          0.0.0.0:8002->8002/tcp               ferry_backend
62df063a474c        8b43c6af2ad0        "docker-entrypoint.s…"   6 days ago          Up 4 hours          3306/tcp, 33060/tcp                  ferry_mysql
6935b5c6f370        redis:6             "docker-entrypoint.s…"   6 days ago          Up 4 hours          6379/tcp                             ferry_redis
[root@localhost ~]# 
匿名挂载
[root@localhost mysql]# docker run --privileged=true -d -P --name nginx02 -v /etc/nginx nginx
4d17fa3b6f1bfba62d027e22a0e05ebe08bf5804a773a761d541f054469cd707
[root@localhost mysql]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                NAMES
cd657e227302        nginx               "/docker-entrypoint.…"   8 minutes ago       Up 8 minutes        0.0.0.0:32769->80/tcp                nginx03
4d17fa3b6f1b        nginx               "/docker-entrypoint.…"   9 minutes ago       Up 9 minutes        0.0.0.0:32768->80/tcp                nginx02
7bf439d30758        mysql:5.7           "docker-entrypoint.s…"   22 minutes ago      Up 22 minutes       33060/tcp, 0.0.0.0:13306->3306/tcp   mysql01
c48b8851b9ab        ferry_web:latest    "/entrypoint.sh"         6 days ago          Up 4 hours          80/tcp, 0.0.0.0:8001->8001/tcp       ferry_web
746ad9e65517        ferry:latest        "/opt/workflow/ferry…"   6 days ago          Up 4 hours          0.0.0.0:8002->8002/tcp               ferry_backend
62df063a474c        8b43c6af2ad0        "docker-entrypoint.s…"   6 days ago          Up 4 hours          3306/tcp, 33060/tcp                  ferry_mysql
6935b5c6f370        redis:6             "docker-entrypoint.s…"   6 days ago          Up 4 hours          6379/tcp                             ferry_redis
[root@localhost mysql]# docker inspect 4d17fa3b6f1b | grep volume
                "Type": "volume",
                "Source": "/var/lib/docker/volumes/53ee8a3159a835548f8a63b177b670ac7ce10b8ed653cfdc769e26337475bc31/_data",
[root@localhost mysql]# docker volume inspect 53ee8a3159a835548f8a63b177b670ac7ce10b8ed653cfdc769e26337475bc31
[
    {
        "CreatedAt": "2021-12-09T14:55:32+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/53ee8a3159a835548f8a63b177b670ac7ce10b8ed653cfdc769e26337475bc31/_data",
        "Name": "53ee8a3159a835548f8a63b177b670ac7ce10b8ed653cfdc769e26337475bc31",
        "Options": null,
        "Scope": "local"
    }
]
[root@localhost mysql]# docker volume ls
DRIVER              VOLUME NAME
local               3b6fab2c2fa2a1f7f47690d153d737bfff483d1096726089fecca8990ce7a1d1
local               6b01d3a377ac2111d5e5ae3da98da775fd1b9e61e04be4d1341f39f367f815d5
local               17ba1b4771db773df5b6619a9d4a41bcb8bae4a3d9a12474deb35a404c1fee07
local               53ee8a3159a835548f8a63b177b670ac7ce10b8ed653cfdc769e26337475bc31
local               96c46bc0b00529bbb98387406db5638367b90fccbf92e6660558473f57495caf
local               nginx_latest-juming
[root@localhost mysql]# 
具名挂载
[root@localhost mysql]# docker run --privileged=true -d -P --name nginx03 -v nginx_latest-juming:/etc/nginx nginx
cd657e227302bf3e095d610898221ba3d7dffebb8b4e89cc93d341b876dff94d
[root@localhost mysql]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                NAMES
cd657e227302        nginx               "/docker-entrypoint.…"   8 minutes ago       Up 8 minutes        0.0.0.0:32769->80/tcp                nginx03
4d17fa3b6f1b        nginx               "/docker-entrypoint.…"   9 minutes ago       Up 9 minutes        0.0.0.0:32768->80/tcp                nginx02
7bf439d30758        mysql:5.7           "docker-entrypoint.s…"   22 minutes ago      Up 22 minutes       33060/tcp, 0.0.0.0:13306->3306/tcp   mysql01
c48b8851b9ab        ferry_web:latest    "/entrypoint.sh"         6 days ago          Up 4 hours          80/tcp, 0.0.0.0:8001->8001/tcp       ferry_web
746ad9e65517        ferry:latest        "/opt/workflow/ferry…"   6 days ago          Up 4 hours          0.0.0.0:8002->8002/tcp               ferry_backend
62df063a474c        8b43c6af2ad0        "docker-entrypoint.s…"   6 days ago          Up 4 hours          3306/tcp, 33060/tcp                  ferry_mysql
6935b5c6f370        redis:6             "docker-entrypoint.s…"   6 days ago          Up 4 hours          6379/tcp                             ferry_redis
[root@localhost mysql]# docker inspect cd657e227302 | grep volume
                "Type": "volume",
                "Source": "/var/lib/docker/volumes/nginx_latest-juming/_data",
[root@localhost mysql]# docker volume inspect nginx_latest-juming
[
    {
        "CreatedAt": "2021-12-09T14:57:10+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/nginx_latest-juming/_data",
        "Name": "nginx_latest-juming",
        "Options": null,
        "Scope": "local"
    }
]
[root@localhost mysql]# docker volume ls
DRIVER              VOLUME NAME
local               3b6fab2c2fa2a1f7f47690d153d737bfff483d1096726089fecca8990ce7a1d1
local               6b01d3a377ac2111d5e5ae3da98da775fd1b9e61e04be4d1341f39f367f815d5
local               17ba1b4771db773df5b6619a9d4a41bcb8bae4a3d9a12474deb35a404c1fee07
local               53ee8a3159a835548f8a63b177b670ac7ce10b8ed653cfdc769e26337475bc31
local               96c46bc0b00529bbb98387406db5638367b90fccbf92e6660558473f57495caf
local               nginx_latest-juming
[root@localhost mysql]# 
只读挂载卷

卷挂载成只读的话,只能通过宿主机修改文件,容器无法修改文件

[root@localhost mysql]# docker run --privileged=true -d -P --name nginx03 -v nginx_latest-juming:/etc/nginx:ro nginx
读写挂载卷
[root@localhost mysql]# docker run --privileged=true -d -P --name nginx03 -v nginx_latest-juming:/etc/nginx:rw nginx
构建自己的docker镜像

docker client 会默认把Dockerfile同级所有文件发给docker Deamon中,所以建议将Dockerfile放到空目录

[root@localhost centos]# pwd
/opt/centos
[root@localhost centos]# ll
total 4
-rw-r--r--. 1 root root 88 Dec  9 17:27 Dockerfile
[root@localhost centos]# cat Dockerfile 
FROM centos:7
VOLUME [ "volume01","volume02" ]
CMD echo "------end------"
CMD /bin/bash
[root@localhost centos]# docker build -f ./Dockerfile -t centos_v20211209:7.9.2009 .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM centos:7
 ---> eeb6ee3f44bd
Step 2/4 : VOLUME [ "volume01","volume02" ]
 ---> Running in a5b63e1a31a1
Removing intermediate container a5b63e1a31a1
 ---> d295b418541d
Step 3/4 : CMD echo "------end------"
 ---> Running in 00afaad616b8
Removing intermediate container 00afaad616b8
 ---> 7bba9540ba27
Step 4/4 : CMD /bin/bash
 ---> Running in 7ec15a59e3f1
Removing intermediate container 7ec15a59e3f1
 ---> b861047cad71
Successfully built b861047cad71
Successfully tagged centos_v20211209:7.9.2009
[root@localhost centos]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos_v20211209    7.9.2009            b861047cad71        6 seconds ago       204MB
mysql               5.7                 738e7101490b        6 days ago          448MB
nginx               latest              f652ca386ed1        6 days ago          141MB
alpine              latest              c059bfaa849c        2 weeks ago         5.59MB
redis               6                   40c68ed3a4d2        3 weeks ago         113MB
node                14.18-alpine        f7229193551e        3 weeks ago         118MB
centos              7                   eeb6ee3f44bd        2 months ago        204MB
golang              1.15                40349a2425ef        4 months ago        840MB
nginx               1.18.0              c2c45d506085        8 months ago        133MB
node                14.15.0             b90fa0d7cbd1        13 months ago       943MB
[root@localhost centos]# 

mysql数据同步,采用docker的--volume-form技术
[root@localhost ~]# docker run --privileged=true -d -p 13306:3306 -v /opt/mysql/etc/mysql/conf.d:/etc/mysql/conf.d -v /opt/mysql/var/lib/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
[root@localhost ~]# docker run --privileged=true -d -p 23306:3306 --volumes-from mysql01 -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 mysql:5.7
4ece1ca838bfb10a6ce7ca50882241f268eb6884408b200ddc9fdce5adbce1e0
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                NAMES
4ece1ca838bf        mysql:5.7           "docker-entrypoint.s…"   8 seconds ago       Up 7 seconds        33060/tcp, 0.0.0.0:23306->3306/tcp   mysql02
8a0ec05989ff        mysql:5.7           "docker-entrypoint.s…"   2 minutes ago       Up 2 minutes        33060/tcp, 0.0.0.0:13306->3306/tcp   mysql01
[root@localhost ~]# 
dockerfile说明

FROM: 基础镜像。一切从这里开始构建 centos
MAINTAINER: 镜像是谁写的。姓名 + 邮箱
RUN: docker构建时需要执行的命令
ADD: 复制指令,从上下文目录中复制文件或者目录到容器里指定路径。

  • ADD 的优点: 在执行 <源文件> 为 tar 压缩文件的话,压缩格式为 gzip, bzip2 以及 xz 的情况下,会自动复制并解压到 <目标路径>。
  • ADD 的缺点: 在不解压的前提下,无法复制 tar 压缩文件。会令镜像构建缓存失效,从而可能会令镜像构建变得比较缓慢。具体是否使用,可以根据是否需要自动解压来决定。

WORKDIR: 指定工作目录。用 WORKDIR 指定的工作目录,会在构建镜像的每一层中都存在。(WORKDIR 指定的工作目录,必须是提前创建好的)。
docker build 构建镜像过程中的,每一个 RUN 命令都是新建的一层。只有通过 WORKDIR 创建的目录才会一直存在。
VOLUME: 定义匿名数据卷。在启动容器时忘记挂载数据卷,会自动挂载到匿名卷。
作用:

  • 避免重要的数据,因容器重启而丢失,这是非常致命的。
  • 避免容器不断变大。

EXPOSE: 仅仅只是声明端口。
作用:

  • 帮助镜像使用者理解这个镜像服务的守护端口,以方便配置映射。
  • 在运行时使用随机端口映射时,也就是 docker run --privileged=true -P 时,会自动随机映射 EXPOSE 的端口。

CMD: 指定这个容器启动的时候运行的命令

  • CMD 在docker run --privileged=true 时运行。
  • RUN 是在 docker build。
  • 只有最后一个剩下可被代替

ENTRYPOINT: 指定这个容器启动的时候运行的命令

  • 如果运行 docker run --privileged=true 时使用了 --entrypoint 选项,将覆盖 CMD 指令指定的程序。
  • 优点:在执行 docker run --privileged=true 的时候可以指定 ENTRYPOINT 运行所需的参数。
  • 注意:如果 Dockerfile 中如果存在多个 ENTRYPOINT 指令,仅最后一个生效。

ONBUILD: 用于延迟构建命令的执行。
假设镜像为 test-build,当有新的 Dockerfile 使用了之前构建的镜像 FROM test-build ,这时执行新镜像的 Dockerfile 构建时候,会执行 test-build 的 Dockerfile 里的 ONBUILD 指定的命令。

COPY: 复制指令,从上下文目录中复制文件或者目录到容器里指定路径。

COPY [--chown=<user>:<group>] <源路径1>...  <目标路径>
COPY [--chown=<user>:<group>] ["<源路径1>",...  "<目标路径>"]

说明:

  • [--chown=:]:可选参数,用户改变复制到容器内文件的拥有者和属组。
  • <源路径>:源文件或者源目录,这里可以是通配符表达式,其通配符规则要满足 Go 的 filepath.Match 规则。例如:
  • <目标路径>:容器内的指定路径,该路径不用事先建好,路径不存在的话,会自动创建。

ENV: 构建的时候设置环境变量
如设置 NODE_VERSION 7.2.0 , 在后续的指令中可以通过 $NODE_VERSION 引用

构建自己的docker镜像
[root@localhost centos]# pwd
/opt/centos
[root@localhost centos]# ll
total 8
-rw-r--r--. 1 root root 602 Dec 22 10:12 CentOS-Media.repo
-rw-r--r--. 1 root root 396 Dec 22 10:12 Dockerfile
[root@localhost centos]# cat Dockerfile 
FROM centos:centos7.9.2009
MAINTAINER NoYoWiFi<1284524409@qq.com>
RUN mkdir -p /tmp/bak && \
    mv -vf /etc/yum.repos.d/* /tmp/bak && \
    mv -vf /tmp/bak /etc/yum.repos.d/
ADD CentOS-Media.repo /etc/yum.repos.d/CentOS-Media.repo
RUN yum -y install vim net-tools
EXPOSE 80
CMD echo "------end------"
CMD /bin/bash
# VOLUME [ "volume01","volume02" ]
# CMD echo "------end------"
# CMD /bin/bash
[root@localhost centos]# cat CentOS-Media.repo 
# CentOS-Media.repo
#
#  This repo can be used with mounted DVD media, verify the mount point for
#  CentOS-7.  You can use this repo and yum to install items directly off the
#  DVD ISO that we release.
#
# To use this repo, put in your DVD and use it with the other repos too:
#  yum --enablerepo=c7-media [command]
#  
# or for ONLY the media repo, do this:
#
#  yum --disablerepo=\* --enablerepo=c7-media [command]

[c7-media]
name=CentOS-$releasever - Media
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[root@localhost centos]# docker build --force-rm -f Dockerfile -t centos_20211220:0.1 .
Sending build context to Docker daemon  3.584kB
Step 1/8 : FROM centos:centos7.9.2009
 ---> eeb6ee3f44bd
Step 2/8 : MAINTAINER NoYoWiFi\<1284524409@qq.com>
 ---> Using cache
 ---> 24c12d237892
Step 3/8 : RUN mkdir -p /tmp/bak &&     mv -vf /etc/yum.repos.d/* /tmp/bak &&     mv -vf /tmp/bak /etc/yum.repos.d/
 ---> Using cache
 ---> fa89e52b9bb3
Step 4/8 : ADD CentOS-Media.repo /etc/yum.repos.d/CentOS-Media.repo
 ---> Using cache
 ---> 23b51f99c52f
Step 5/8 : RUN yum -y install vim net-tools
 ---> Running in 886e4f056689
Loaded plugins: fastestmirror, ovl
Removing intermediate container 886e4f056689
 ---> 9889602d26aa
Step 6/8 : EXPOSE 80
 ---> Running in 329adfceae36
Removing intermediate container 329adfceae36
 ---> 436f321f74cd
Step 7/8 : CMD echo "------end------"
 ---> Running in 8932c3558846
Removing intermediate container 8932c3558846
 ---> 1019234dc9ea
Step 8/8 : CMD /bin/bash
 ---> Running in e8fc61fa097b
Removing intermediate container e8fc61fa097b
 ---> d220faaac874
Successfully built d220faaac874
Successfully tagged centos_20211220:0.1
[root@localhost centos]# docker images;
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos_20211220     0.1                 d220faaac874        4 seconds ago       323MB
mysql               5.7                 738e7101490b        2 weeks ago         448MB
nginx               latest              f652ca386ed1        2 weeks ago         141MB
alpine              latest              c059bfaa849c        3 weeks ago         5.59MB
redis               6                   40c68ed3a4d2        4 weeks ago         113MB
node                14.18-alpine        f7229193551e        5 weeks ago         118MB
centos              centos7.9.2009      eeb6ee3f44bd        3 months ago        204MB
golang              1.15                40349a2425ef        4 months ago        840MB
nginx               1.18.0              c2c45d506085        8 months ago        133MB
node                14.15.0             b90fa0d7cbd1        14 months ago       943MB
[root@localhost centos]# docker rmi 062391f60005 a76830e3f911 5d8574733b47 3bb6c0968d26
构建自己的Tomcat
[root@localhost centos]# pwd
/opt/centos
[root@localhost centos]# ll
total 154680
-rw-r--r--. 1 root root  11582777 Dec 22 15:58 apache-tomcat-9.0.56.tar.gz
-rw-r--r--. 1 root root       602 Dec 22 10:12 CentOS-Media.repo
-rw-r--r--. 1 root root       785 Dec 22 16:53 Dockerfile
-rw-r--r--. 1 root root 146799982 Dec 22 15:58 jdk-8u311-linux-x64.tar.gz
[root@localhost centos]# cat CentOS-Media.repo 
# CentOS-Media.repo
#
#  This repo can be used with mounted DVD media, verify the mount point for
#  CentOS-7.  You can use this repo and yum to install items directly off the
#  DVD ISO that we release.
#
# To use this repo, put in your DVD and use it with the other repos too:
#  yum --enablerepo=c7-media [command]
#  
# or for ONLY the media repo, do this:
#
#  yum --disablerepo=\* --enablerepo=c7-media [command]

[c7-media]
name=CentOS-$releasever - Media
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[root@localhost centos]# cat Dockerfile 
FROM centos:centos7.9.2009
MAINTAINER NoYoWiFi\<1284524409@qq.com>
RUN mkdir -p /tmp/bak && \
    mv -vf /etc/yum.repos.d/* /tmp/bak && \
    mv -vf /tmp/bak /etc/yum.repos.d/
ADD CentOS-Media.repo /etc/yum.repos.d/CentOS-Media.repo
RUN yum -y install vim net-tools
ADD apache-tomcat-9.0.56.tar.gz /usr/local/
ADD jdk-8u311-linux-x64.tar.gz /usr/local/
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_311
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.56
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.56
ENV PATH \$PATH:$JAVA_HOME/bin:\$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-9.0.56/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.56/bin/logs/catalina.out
[root@localhost centos]# 
[root@localhost centos]# docker build -t diytomcat .
Sending build context to Docker daemon  158.4MB
Step 1/16 : FROM centos:centos7.9.2009
 ---> eeb6ee3f44bd
Step 2/16 : MAINTAINER NoYoWiFi\<1284524409@qq.com>
 ---> Running in 09a205b58258
Removing intermediate container 09a205b58258
 ---> be9b26ed6537
Step 3/16 : RUN mkdir -p /tmp/bak &&     mv -vf /etc/yum.repos.d/* /tmp/bak &&     mv -vf /tmp/bak /etc/yum.repos.d/
 ---> Running in f394979ec2c9
'/etc/yum.repos.d/CentOS-Base.repo' -> '/tmp/bak/CentOS-Base.repo'
'/etc/yum.repos.d/CentOS-CR.repo' -> '/tmp/bak/CentOS-CR.repo'
'/etc/yum.repos.d/CentOS-Debuginfo.repo' -> '/tmp/bak/CentOS-Debuginfo.repo'
'/etc/yum.repos.d/CentOS-Media.repo' -> '/tmp/bak/CentOS-Media.repo'
'/etc/yum.repos.d/CentOS-Sources.repo' -> '/tmp/bak/CentOS-Sources.repo'
'/etc/yum.repos.d/CentOS-Vault.repo' -> '/tmp/bak/CentOS-Vault.repo'
'/etc/yum.repos.d/CentOS-fasttrack.repo' -> '/tmp/bak/CentOS-fasttrack.repo'
'/etc/yum.repos.d/CentOS-x86_64-kernel.repo' -> '/tmp/bak/CentOS-x86_64-kernel.repo'
'/tmp/bak' -> '/etc/yum.repos.d/bak'
Removing intermediate container f394979ec2c9
 ---> 1879ad30bd7d
Step 4/16 : ADD CentOS-Media.repo /etc/yum.repos.d/CentOS-Media.repo
 ---> 9d5896cc1046
Step 5/16 : RUN yum -y install vim net-tools
 ---> Running in d47c2dd86b67
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
Resolving Dependencies
Complete!
Removing intermediate container d47c2dd86b67
 ---> e893f534cf7a
Step 6/16 : ADD apache-tomcat-9.0.56.tar.gz /usr/local/
 ---> 1b3300fccc3c
Step 7/16 : ADD jdk-8u311-linux-x64.tar.gz /usr/local/
 ---> 70993a72e9a0
Step 8/16 : ENV MYPATH /usr/local
 ---> Running in 92d98104b0a7
Removing intermediate container 92d98104b0a7
 ---> 773267e1905a
Step 9/16 : WORKDIR $MYPATH
 ---> Running in c98203d9c533
Removing intermediate container c98203d9c533
 ---> 4da35fcb20ea
Step 10/16 : ENV JAVA_HOME /usr/local/jdk1.8.0_311
 ---> Running in 13b8db56cdd0
Removing intermediate container 13b8db56cdd0
 ---> 31169ef14240
Step 11/16 : ENV CLASSPATH \$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
 ---> Running in 63f01de9e5b4
Removing intermediate container 63f01de9e5b4
 ---> 87432ccec37b
Step 12/16 : ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.56
 ---> Running in 3275703e2794
Removing intermediate container 3275703e2794
 ---> 9d277cb18eaf
Step 13/16 : ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.56
 ---> Running in 5896cde72acd
Removing intermediate container 5896cde72acd
 ---> 96b83febde87
Step 14/16 : ENV PATH \$PATH:\$JAVA_HOME/bin:\$CATALINA_HOME/lib:$CATALINA_HOME/bin
 ---> Running in d0c5f76936dd
Removing intermediate container d0c5f76936dd
 ---> aec334fe360c
Step 15/16 : EXPOSE 8080
 ---> Running in 0cb9b4677b94
Removing intermediate container 0cb9b4677b94
 ---> 07285b4b0609
Step 16/16 : CMD /usr/local/apache-tomcat-9.0.56/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.56/bin/logs/catalina.out
 ---> Running in 2d3c2b7d1eff
Removing intermediate container 2d3c2b7d1eff
 ---> 8ebb0dc35ae3
Successfully built 8ebb0dc35ae3
Successfully tagged diytomcat:latest
[root@localhost centos]# 
[root@localhost centos]# docker images;
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
diytomcat           latest              8ebb0dc35ae3        3 minutes ago       704MB
mysql               5.7                 738e7101490b        2 weeks ago         448MB
nginx               latest              f652ca386ed1        2 weeks ago         141MB
alpine              latest              c059bfaa849c        3 weeks ago         5.59MB
redis               6                   40c68ed3a4d2        4 weeks ago         113MB
node                14.18-alpine        f7229193551e        5 weeks ago         118MB
centos              centos7.9.2009      eeb6ee3f44bd        3 months ago        204MB
golang              1.15                40349a2425ef        4 months ago        840MB
nginx               1.18.0              c2c45d506085        8 months ago        133MB
node                14.15.0             b90fa0d7cbd1        14 months ago       943MB
[root@localhost centos]# 
[root@localhost centos]# docker run -d -p 9090:8080 --name NoYoWiFi_tomcat -v /home/NoYoWiFi/build/tomcat/webapps:/usr/local/apache-tomcat-9.0.56/webapps/test -v /home/NoYoWiFi/build/tomcat/logs:/usr/local/apache-tomcat-9.0.56/logs diytomcat
6707e0a8fe5bdd0199e5711a9fe7885c5ef63ee1bed28adb1c809a04b435c668
[root@localhost centos]# docker exec -it 6707e0a /bin/bash
[root@6707e0a8fe5b apache-tomcat-9.0.56]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:8005          0.0.0.0:*               LISTEN      13/java             
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      13/java             
[root@6707e0a8fe5b apache-tomcat-9.0.56]# exit
exit
[root@localhost centos]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1173/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1262/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1173/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1262/master         
tcp6       0      0 :::9090                 :::*                    LISTEN      6387/docker-proxy   
[root@localhost centos]# 
登录阿里云Docker Registry
[root@localhost ~]# docker login --username=noyowifi registry.cn-hangzhou.aliyuncs.com
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
从Registry中拉取镜像
[root@localhost ~]# docker pull registry.cn-hangzhou.aliyuncs.com/qq1284524409/noyowifi:diytomcat1.0
将镜像推送到Registry
[root@localhost ~]# docker tag 8ebb0dc35ae3 registry.cn-hangzhou.aliyuncs.com/qq1284524409/noyowifi:diytomcat1.0
[root@localhost ~]# docker push registry.cn-hangzhou.aliyuncs.com/qq1284524409/noyowifi:diytomcat1.0
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/qq1284524409/noyowifi]
7e98cf44cb33: Pushed 
05067b09e794: Pushed 
fb282d9194c3: Pushed 
1fd9c38e9662: Pushed 
4a0e328495c8: Pushed 
174f56854903: Pushed 
1.0: digest: sha256:1f157da37c63c8346c242661fd22bdb25a8e1c9c0cc793ad4d0059b7d02492e6 size: 1581
删除tag
[root@localhost ~]# docker rmi registry.cn-hangzhou.aliyuncs.com/qq1284524409/noyowifi:1.0
Untagged: registry.cn-hangzhou.aliyuncs.com/qq1284524409/noyowifi:1.0
查看所有docker网络
[root@localhost ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
20af1d9be248        bridge              bridge              local
10f7fabcfa12        ferry_default       bridge              local
16ba3aadc293        host                host                local
993db4a9d769        none                null                local
[root@localhost ~]# 
docker网络类型
  • bridge:docker默认的网络驱动。 如果未指定驱动程序,则这是需要创建的网络类型。 当应用程序在需要通信的独立容器中运行时,通常会使用桥接网络。

当多个容器在同一个Docker主机上进行通信时,用户定义的桥接网络是最佳选择。

  • host:对于独立的容器,删除容器和Docker主机之间的网络隔离,并直接使用主机的网络(若启用特权容器,将可以直接修改宿主机网络设置)。 host仅适用于Docker 17.06及更高版本上的swarm群集服务。

当网络堆栈不应与Docker主机隔离时,主机网络最佳,但您希望隔离容器的其他方面。

  • overlay:Overlay networks将多个Docker守护程序连接在一起,并使swarm群集服务能够相互通信 。 还可以使用Overlay networks来促进swarm群集服务和独立容器之间的通信,或者在不同Docker守护程序上的两个独立容器之间进行通信。 此策略无需在这些容器之间执行OS级别的路由。

不同Docker主机上运行的容器进行通信时,或当多个应用程序使用swarm服务协同工作时,覆盖网络overlay是最佳选择。

  • macvlan:Macvlan网络允许为容器分配MAC地址,使其显示为网络上的物理设备。 Docker守护程序通过其MAC地址将流量路由到容器。 macvlan在处理期望直接连接到物理网络的传统应用程序时,使用该驱动程序有时是最佳选择,而不是通过Docker主机的网络堆栈进行路由。

当您从VM设置迁移或需要您的容器看起来像网络上的物理主机(每个都具有唯一的MAC地址)时,Macvlan网络是最佳的。

  • none:对于此容器,禁用所有网络。 通常与自定义网络驱动程序一起使用,none不适用于swarm集群服务。
创建自定义bridge
[root@localhost ~]# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 noyowifi_tomcat
6e23461aa7b1daf24009957d3bf149dad5f786d5a6f9b0cf31291343ac471285
[root@localhost ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
20af1d9be248        bridge              bridge              local
10f7fabcfa12        ferry_default       bridge              local
16ba3aadc293        host                host                local
993db4a9d769        none                null                local
6e23461aa7b1        noyowifi_tomcat     bridge              local
[root@localhost ~]# 
查看docker网络详情
[root@localhost ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
20af1d9be248        bridge              bridge              local
10f7fabcfa12        ferry_default       bridge              local
16ba3aadc293        host                host                local
993db4a9d769        none                null                local
6e23461aa7b1        noyowifi_tomcat     bridge              local
[root@localhost ~]# docker network inspect noyowifi_tomcat
[
    {
        "Name": "noyowifi_tomcat",
        "Id": "6e23461aa7b1daf24009957d3bf149dad5f786d5a6f9b0cf31291343ac471285",
        "Created": "2021-12-28T15:03:02.378896971+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "Gateway": "192.168.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]
[root@localhost ~]# 
在自定义网络下运行两个容器
[root@localhost ~]# docker run -d -P --name tomcat02 --net noyowifi_tomcat registry.cn-hangzhou.aliyuncs.com/qq1284524409/noyowifi:diytomcat1.0
dde1b4652dad3fcd38fa5d923c7eec561e95d6c81f8d86fd784a68bce6cb56cd
[root@localhost ~]# docker run -d -P --name tomcat03 --net noyowifi_tomcat registry.cn-hangzhou.aliyuncs.com/qq1284524409/noyowifi:diytomcat1.0
66d4c15943626a4c4ef8563d4828c1d2a9ce58f4b58d91be63ec68067e3594bc
[root@localhost ~]# docker network inspect noyowifi_tomcat
[
    {
        "Name": "noyowifi_tomcat",
        "Id": "6e23461aa7b1daf24009957d3bf149dad5f786d5a6f9b0cf31291343ac471285",
        "Created": "2021-12-28T15:03:02.378896971+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "Gateway": "192.168.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "66d4c15943626a4c4ef8563d4828c1d2a9ce58f4b58d91be63ec68067e3594bc": {
                "Name": "tomcat03",
                "EndpointID": "c878ccd49858e1119dbba4c2e045f31d23dbb9f058443d72235bcf7db3ad1b9e",
                "MacAddress": "02:42:c0:a8:00:03",
                "IPv4Address": "192.168.0.3/16",
                "IPv6Address": ""
            },
            "dde1b4652dad3fcd38fa5d923c7eec561e95d6c81f8d86fd784a68bce6cb56cd": {
                "Name": "tomcat02",
                "EndpointID": "f92419731e7f6084ce204d24b020a19cd1043c2241a8d2db374111634436f6ee",
                "MacAddress": "02:42:c0:a8:00:02",
                "IPv4Address": "192.168.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
[root@localhost ~]# 
docker ping测试容器连通性
[root@localhost ~]# docker network inspect noyowifi_tomcat
[
    {
        "Name": "noyowifi_tomcat",
        "Id": "6e23461aa7b1daf24009957d3bf149dad5f786d5a6f9b0cf31291343ac471285",
        "Created": "2021-12-28T15:03:02.378896971+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "Gateway": "192.168.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "66d4c15943626a4c4ef8563d4828c1d2a9ce58f4b58d91be63ec68067e3594bc": {
                "Name": "tomcat03",
                "EndpointID": "c878ccd49858e1119dbba4c2e045f31d23dbb9f058443d72235bcf7db3ad1b9e",
                "MacAddress": "02:42:c0:a8:00:03",
                "IPv4Address": "192.168.0.3/16",
                "IPv6Address": ""
            },
            "dde1b4652dad3fcd38fa5d923c7eec561e95d6c81f8d86fd784a68bce6cb56cd": {
                "Name": "tomcat02",
                "EndpointID": "f92419731e7f6084ce204d24b020a19cd1043c2241a8d2db374111634436f6ee",
                "MacAddress": "02:42:c0:a8:00:02",
                "IPv4Address": "192.168.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
[root@localhost ~]# 
[root@localhost ~]# docker exec -it tomcat02 ping 192.168.0.3
PING 192.168.0.3 (192.168.0.3) 56(84) bytes of data.
64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=11.3 ms
64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.096 ms
64 bytes from 192.168.0.3: icmp_seq=3 ttl=64 time=0.097 ms
64 bytes from 192.168.0.3: icmp_seq=4 ttl=64 time=0.122 ms
64 bytes from 192.168.0.3: icmp_seq=5 ttl=64 time=0.098 ms
64 bytes from 192.168.0.3: icmp_seq=6 ttl=64 time=0.095 ms
^C
--- 192.168.0.3 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5004ms
rtt min/avg/max/mdev = 0.095/1.982/11.388/4.206 ms
[root@localhost ~]# docker exec -it tomcat02 ping tomcat03
PING tomcat03 (192.168.0.3) 56(84) bytes of data.
64 bytes from tomcat03.noyowifi_tomcat (192.168.0.3): icmp_seq=1 ttl=64 time=0.390 ms
64 bytes from tomcat03.noyowifi_tomcat (192.168.0.3): icmp_seq=2 ttl=64 time=0.158 ms
64 bytes from tomcat03.noyowifi_tomcat (192.168.0.3): icmp_seq=3 ttl=64 time=0.132 ms
64 bytes from tomcat03.noyowifi_tomcat (192.168.0.3): icmp_seq=4 ttl=64 time=0.099 ms
64 bytes from tomcat03.noyowifi_tomcat (192.168.0.3): icmp_seq=5 ttl=64 time=0.098 ms
64 bytes from tomcat03.noyowifi_tomcat (192.168.0.3): icmp_seq=6 ttl=64 time=0.185 ms
^C
--- tomcat03 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5003ms
rtt min/avg/max/mdev = 0.098/0.177/0.390/0.100 ms
[root@localhost ~]# 
不同bridge间建立互通
[root@localhost ~]# docker run -d -P --name tomcat01 --net noyowifi_tomcat registry.cn-hangzhou.aliyuncs.com/qq1284524409/noyowifi:diytomcat1.0
637eaa35ca30228681467fd4a530c51c1753f9443a6de1f42b5fa236aeb163a8
[root@localhost ~]# docker run -d -P --name tomcat02 --net noyowifi_tomcat registry.cn-hangzhou.aliyuncs.com/qq1284524409/noyowifi:diytomcat1.0
79b55fab10b067bffbe2b77af1f93e38790d66a69cb35084a1db58e2a7092650
[root@localhost ~]# docker run -d -P --name tomcat03 registry.cn-hangzhou.aliyuncs.com/qq1284524409/noyowifi:diytomcat1.0
f58375ffcf2a6a5fb2e28387d5925f89a9741de149edb4b43cac2e87cd8e1d5d
[root@localhost ~]# docker run -d -P --name tomcat04 registry.cn-hangzhou.aliyuncs.com/qq1284524409/noyowifi:diytomcat1.0
d151d833d7c1f95f54a2fe46e0e3265033e4086f7381439fbf3b0574f6cc5df8
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                                                                  COMMAND                  CREATED             STATUS              PORTS                     NAMES
d151d833d7c1        registry.cn-hangzhou.aliyuncs.com/qq1284524409/noyowifi:diytomcat1.0   "/bin/sh -c '/usr/lo…"   7 seconds ago       Up 6 seconds        0.0.0.0:32774->8080/tcp   tomcat04
f58375ffcf2a        registry.cn-hangzhou.aliyuncs.com/qq1284524409/noyowifi:diytomcat1.0   "/bin/sh -c '/usr/lo…"   12 seconds ago      Up 11 seconds       0.0.0.0:32773->8080/tcp   tomcat03
79b55fab10b0        registry.cn-hangzhou.aliyuncs.com/qq1284524409/noyowifi:diytomcat1.0   "/bin/sh -c '/usr/lo…"   23 seconds ago      Up 22 seconds       0.0.0.0:32772->8080/tcp   tomcat02
637eaa35ca30        registry.cn-hangzhou.aliyuncs.com/qq1284524409/noyowifi:diytomcat1.0   "/bin/sh -c '/usr/lo…"   29 seconds ago      Up 28 seconds       0.0.0.0:32771->8080/tcp   tomcat01
[root@localhost ~]# docker exec -it tomcat01 ping tomcat03
ping: tomcat03: Name or service not known
[root@localhost ~]# docker network connect noyowifi_tomcat tomcat03
[root@localhost ~]# docker network inspect noyowifi_tomcat
[
    {
        "Name": "noyowifi_tomcat",
        "Id": "6e23461aa7b1daf24009957d3bf149dad5f786d5a6f9b0cf31291343ac471285",
        "Created": "2021-12-28T15:03:02.378896971+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "Gateway": "192.168.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "637eaa35ca30228681467fd4a530c51c1753f9443a6de1f42b5fa236aeb163a8": {
                "Name": "tomcat01",
                "EndpointID": "67c37aa2e2bbd5723fb6c4f59a951fa3809bddc467c0027b3c35523a2b851864",
                "MacAddress": "02:42:c0:a8:00:02",
                "IPv4Address": "192.168.0.2/16",
                "IPv6Address": ""
            },
            "79b55fab10b067bffbe2b77af1f93e38790d66a69cb35084a1db58e2a7092650": {
                "Name": "tomcat02",
                "EndpointID": "2bb5f51de2d42df3c1f1ca87b0ff13710ab9647b7ce9356f75f8894a3908e724",
                "MacAddress": "02:42:c0:a8:00:03",
                "IPv4Address": "192.168.0.3/16",
                "IPv6Address": ""
            },
            "f58375ffcf2a6a5fb2e28387d5925f89a9741de149edb4b43cac2e87cd8e1d5d": {
                "Name": "tomcat03",
                "EndpointID": "42a2ff638e7af49a18963b8cca9f71f6b618030cbb2858b64a82ab78e969341b",
                "MacAddress": "02:42:c0:a8:00:04",
                "IPv4Address": "192.168.0.4/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
[root@localhost ~]# docker exec -it tomcat01 ping tomcat03
PING tomcat03 (192.168.0.4) 56(84) bytes of data.
64 bytes from tomcat03.noyowifi_tomcat (192.168.0.4): icmp_seq=1 ttl=64 time=0.266 ms
64 bytes from tomcat03.noyowifi_tomcat (192.168.0.4): icmp_seq=2 ttl=64 time=0.117 ms
64 bytes from tomcat03.noyowifi_tomcat (192.168.0.4): icmp_seq=3 ttl=64 time=0.126 ms
64 bytes from tomcat03.noyowifi_tomcat (192.168.0.4): icmp_seq=4 ttl=64 time=0.099 ms
^C
--- tomcat03 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 0.099/0.152/0.266/0.066 ms
[root@localhost ~]# 
取消 docker 容器网络连接
[root@localhost ~]# docker exec -it tomcat01 ping tomcat03
PING tomcat03 (172.19.0.4) 56(84) bytes of data.
64 bytes from tomcat03.noyowifi_tomcat (172.19.0.4): icmp_seq=1 ttl=64 time=0.236 ms
64 bytes from tomcat03.noyowifi_tomcat (172.19.0.4): icmp_seq=2 ttl=64 time=0.160 ms
64 bytes from tomcat03.noyowifi_tomcat (172.19.0.4): icmp_seq=3 ttl=64 time=0.095 ms
64 bytes from tomcat03.noyowifi_tomcat (172.19.0.4): icmp_seq=4 ttl=64 time=0.098 ms
^C
--- tomcat03 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3002ms
rtt min/avg/max/mdev = 0.095/0.147/0.236/0.058 ms
[root@localhost ~]# docker network disconnect noyowifi_tomcat tomcat03
[root@localhost ~]# docker exec -it tomcat01 ping tomcat03
ping: tomcat03: Name or service not known
[root@localhost ~]# 
删除自定义docker网络
[root@localhost ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
20af1d9be248        bridge              bridge              local
10f7fabcfa12        ferry_default       bridge              local
16ba3aadc293        host                host                local
993db4a9d769        none                null                local
6e23461aa7b1        noyowifi_tomcat     bridge              local
[root@localhost ~]# docker network rm noyowifi_tomcat
6e23461aa7b1
[root@localhost ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
20af1d9be248        bridge              bridge              local
10f7fabcfa12        ferry_default       bridge              local
16ba3aadc293        host                host                local
993db4a9d769        none                null                local
[root@localhost ~]# 
脚本创建6个Redis集群
[root@localhost ~]# cat redis.sh 
#!/bin/bash
for port in $(seq 1 6);\
do \
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >/mydata/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done
[root@localhost ~]# 
启动 Redis容器
[root@localhost ~]# cat redis.sh 
#!/bin/bash
for port in $(seq 1 6);\
do \
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >/mydata/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF

docker run -p 637${port}:6379 -p 1637${port}:16379 --name noyowifi_redis-${port} \
-v /mydata/redis/node-${port}/data:/data \
-v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d --net noyowifi_redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf;
done
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                                              NAMES
8a69d5b83201        redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   50 seconds ago      Up 48 seconds       0.0.0.0:6376->6379/tcp, 0.0.0.0:16376->16379/tcp   noyowifi_redis-6
dde9abe26f1e        redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   50 seconds ago      Up 49 seconds       0.0.0.0:6375->6379/tcp, 0.0.0.0:16375->16379/tcp   noyowifi_redis-5
4ff8d8737dec        redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   51 seconds ago      Up 50 seconds       0.0.0.0:6374->6379/tcp, 0.0.0.0:16374->16379/tcp   noyowifi_redis-4
4eb707d180f0        redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   52 seconds ago      Up 51 seconds       0.0.0.0:6373->6379/tcp, 0.0.0.0:16373->16379/tcp   noyowifi_redis-3
b2c7b65c5059        redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   53 seconds ago      Up 52 seconds       0.0.0.0:6372->6379/tcp, 0.0.0.0:16372->16379/tcp   noyowifi_redis-2
1feee8acb0a6        redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   54 seconds ago      Up 52 seconds       0.0.0.0:6371->6379/tcp, 0.0.0.0:16371->16379/tcp   noyowifi_redis-1
[root@localhost ~]# 
docker 创建Redis集群
[root@localhost ~]# docker exec -it noyowifi_redis-1 /bin/sh
/data # ls
appendonly.aof  nodes.conf
/data # redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.38.0.15:6379 to 172.38.0.11:6379
Adding replica 172.38.0.16:6379 to 172.38.0.12:6379
Adding replica 172.38.0.14:6379 to 172.38.0.13:6379
M: 611b472a7bc519a050f0f58025d17ba3f8839493 172.38.0.11:6379
   slots:[0-5460] (5461 slots) master
M: c94f5c2093a331f612acfa3bb0794c240ba53e8d 172.38.0.12:6379
   slots:[5461-10922] (5462 slots) master
M: 8532ad1f6361e82a1bd8207ca0ed7b5067d5728c 172.38.0.13:6379
   slots:[10923-16383] (5461 slots) master
S: 942e98a62354dd04bf7841a59f54da1573ee100d 172.38.0.14:6379
   replicates 8532ad1f6361e82a1bd8207ca0ed7b5067d5728c
S: 7764e2665fb1afc3af61bb02f372af39569ac4a3 172.38.0.15:6379
   replicates 611b472a7bc519a050f0f58025d17ba3f8839493
S: cf4ead4a04e545885c2675db582d3feaa907d2e0 172.38.0.16:6379
   replicates c94f5c2093a331f612acfa3bb0794c240ba53e8d
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
..
>>> Performing Cluster Check (using node 172.38.0.11:6379)
M: 611b472a7bc519a050f0f58025d17ba3f8839493 172.38.0.11:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: c94f5c2093a331f612acfa3bb0794c240ba53e8d 172.38.0.12:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 7764e2665fb1afc3af61bb02f372af39569ac4a3 172.38.0.15:6379
   slots: (0 slots) slave
   replicates 611b472a7bc519a050f0f58025d17ba3f8839493
M: 8532ad1f6361e82a1bd8207ca0ed7b5067d5728c 172.38.0.13:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: cf4ead4a04e545885c2675db582d3feaa907d2e0 172.38.0.16:6379
   slots: (0 slots) slave
   replicates c94f5c2093a331f612acfa3bb0794c240ba53e8d
S: 942e98a62354dd04bf7841a59f54da1573ee100d 172.38.0.14:6379
   slots: (0 slots) slave
   replicates 8532ad1f6361e82a1bd8207ca0ed7b5067d5728c
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
/data # redis-cli -c
127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:276
cluster_stats_messages_pong_sent:284
cluster_stats_messages_sent:560
cluster_stats_messages_ping_received:279
cluster_stats_messages_pong_received:276
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:560
127.0.0.1:6379> cluster nodes
c94f5c2093a331f612acfa3bb0794c240ba53e8d 172.38.0.12:6379@16379 master - 0 1640682929377 2 connected 5461-10922
611b472a7bc519a050f0f58025d17ba3f8839493 172.38.0.11:6379@16379 myself,master - 0 1640682927000 1 connected 0-5460
7764e2665fb1afc3af61bb02f372af39569ac4a3 172.38.0.15:6379@16379 slave 611b472a7bc519a050f0f58025d17ba3f8839493 0 1640682929000 5 connected
8532ad1f6361e82a1bd8207ca0ed7b5067d5728c 172.38.0.13:6379@16379 master - 0 1640682928867 3 connected 10923-16383
cf4ead4a04e545885c2675db582d3feaa907d2e0 172.38.0.16:6379@16379 slave c94f5c2093a331f612acfa3bb0794c240ba53e8d 0 1640682929377 6 connected
942e98a62354dd04bf7841a59f54da1573ee100d 172.38.0.14:6379@16379 slave 8532ad1f6361e82a1bd8207ca0ed7b5067d5728c 0 1640682928353 4 connected
127.0.0.1:6379> set a b
-> Redirected to slot [15495] located at 172.38.0.13:6379
OK

[root@localhost ~]# docker stop noyowifi_redis-3
noyowifi_redis-3

172.38.0.13:6379> get a
Could not connect to Redis at 172.38.0.13:6379: Host is unreachable
(8.71s)
not connected> exit
/data # redis-cli -c
127.0.0.1:6379> get a
-> Redirected to slot [15495] located at 172.38.0.14:6379
"b"
172.38.0.14:6379> 

-导出image镜像

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE                                 COMMAND                  CREATED         STATUS          PORTS                      
fc8b43fd22b7   zabbix-agent2:centos-6.0.1            "/usr/bin/tini -- /u…"   6 minutes ago   Up 5 minutes    0.0.0.0:10050->10050/tcp, :
5abb07aceeb5   zabbix-web-nginx-mysql:centos-6.0.1   "docker-entrypoint.sh"   30 hours ago    Up 44 minutes   8443/tcp, 0.0.0.0:80->8080/
284aaabb2465   zabbix-server-mysql:centos-6.0.1      "/usr/bin/tini -- /u…"   30 hours ago    Up 44 minutes   0.0.0.0:10051->10051/tcp, :
80ac98271659   zabbix-java-gateway:centos-6.0.1      "docker-entrypoint.s…"   30 hours ago    Up 44 minutes   10052/tcp                  
7b40a9f30d90   mysql:8.0                             "docker-entrypoint.s…"   30 hours ago    Up 44 minutes   3306/tcp, 33060/tcp        
[root@localhost ~]# docker save -o zabbix-agent2.tar zabbix-agent2:centos-6.0.1

-恢复备份docker镜像

[root@localhost yum.repos.d]# docker load < zabbix-agent2.tar 
74ddd0ec08fa: Loading layer [==================================================>]  238.6MB/238.6MB
84ff821e3340: Loading layer [==================================================>]  12.29kB/12.29kB
d8426b3e16c1: Loading layer [==================================================>]  16.53MB/16.53MB
321d5154ea09: Loading layer [==================================================>]  294.4kB/294.4kB
6fe7f92ce717: Loading layer [==================================================>]  398.8kB/398.8kB
7ae9fa059a30: Loading layer [==================================================>]   16.9kB/16.9kB
0ed558f4605a: Loading layer [==================================================>]   27.3MB/27.3MB
5f70bf18a086: Loading layer [==================================================>]  1.024kB/1.024kB
51e23f4b240d: Loading layer [==================================================>]  10.75kB/10.75kB
Loaded image: zabbix-agent2:centos-6.0.1
[root@localhost yum.repos.d]# docker images;
REPOSITORY      TAG            IMAGE ID       CREATED        SIZE
zabbix-agent2   centos-6.0.1   5d843a608caa   38 hours ago   274MB
[root@localhost yum.repos.d]# 

-停止删除none docker镜像
docker images
docker ps -a | grep "Exited" | awk '{print $1 }'|xargs docker stop
docker rm $(docker ps -a -q)
docker images|grep none|awk '{print $3 }'|xargs docker rmi

-容器不存在,删除镜像时候提示 No such image
docker 镜像文件是存在/var/lib/docker/image/overlay2/imagedb/content/sha256目录

  • 修改镜像后提交,并删除就镜像
[root@localhost patch]# docker commit 2b0 quay.io/centos/centos:stream8
sha256:ce3303aff62312b8444fbc9c0b789f3a8e0c37e2bf15d92d988e22f21075f36a
[root@localhost patch]# docker images;
REPOSITORY               TAG             IMAGE ID       CREATED          SIZE
quay.io/centos/centos    stream8         2598e63f8dcd   38 seconds ago   483MB
quay.io/centos/centos    <none>          f8a44dfb7278   6 weeks ago      339MB
[root@localhost patch]# docker save -o centos8.tar.gz quay.io/centos/centos:stream8
[root@localhost patch]# docker rmi 259
Untagged: quay.io/centos/centos:stream8
Deleted: sha256:2598e63f8dcdd7c4a68faf559b6b58bf7d8fb2e213206772220d7ae1312461bc
Deleted: sha256:a21c358501bb4a1fbe238bb093d3dd9f6329866b1ce958e40ed172ea79f4cb0a
[root@localhost zbx]# docker rmi $(docker images |grep centos | awk -F ' ' '{print $3}')
Untagged: quay.io/centos/centos@sha256:8f2c5bfa77df306ae5f34bac3303055b5e5a443e56f4acdb26b80d2e921e657a
Deleted: sha256:f8a44dfb727810e194908a5060ab8f69fb6fc66af29a16850ccd74e8d70337c5
Deleted: sha256:81595ef400b55f5c08bb85e432336c8695349fa62a491d5523f9fd196bf484e0
Deleted: sha256:ff5872797073d7a6963e0345d1ac9db5843f431952f6663fd23cd4bce541bc73
Deleted: sha256:196dd19b5f59b4db1d2c3ede8d0ea6759bbd2149222e40f785bde296871bb6c3
Deleted: sha256:5bf135c4a0de07e52c11282c0954e3e6b7c7ddc6c8834a7fd2803c3dc6a31a69
[root@localhost patch]# docker load < centos8.tar.gz
  • 查看容器使用的IP

docker inspect --format='{{.NetworkSettings.Networks.deyatech.IPAddress}}' nginx

  • 修改 ip 和 port

cd /var/lib/docker/containers/容器ID/
vim hostconfig.json
修改HostIp和HostPort,若绑定0.0.0.0,则HostIp改为空就行。例:{"HostIp":"","HostPort":"9200"}

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
3月前
|
Kubernetes Ubuntu Linux
docker学习笔记
docker学习笔记
52 0
|
1月前
|
安全 网络安全 开发者
Docker学习笔记(一):Docker命令总结
本文介绍了Docker的基本概念、优点以及常用命令。Docker是一个开源的应用容器引擎,它通过容器打包应用程序及其依赖项,实现快速部署和迁移。主要优点包括轻量级、可移植性、易于管理、安全性和开源性。文章还区分了镜像和容器的概念,并提供了构建镜像、查看容器、运行容器、停止和删除容器等常用Docker命令的示例。
135 0
|
2月前
|
Docker Windows Python
Windows-Docker学习笔记(一)
本文介绍了Windows环境下Docker的安装与使用方法。首先通过链接下载Windows版本的Docker,然后按步骤配置:启用Windows功能、设置默认WSL版本为2,并检查Docker及其组件是否正常工作。接着详细列出了Docker的基础命令及其用法,包括搜索、下载镜像、管理容器等。同时介绍了Dockerfile的编写方法及其构建过程,以及docker-compose.yml文件的用途。最后提供了运行和管理容器的具体示例。
Windows-Docker学习笔记(一)
|
3月前
|
前端开发 jenkins 持续交付
jenkins学习笔记之二十:docker in docker运行pipeline
jenkins学习笔记之二十:docker in docker运行pipeline
|
3月前
|
jenkins Java 持续交付
jenkins学习笔记之十九:Docker安装jenkins master及动、静态配置slave
jenkins学习笔记之十九:Docker安装jenkins master及动、静态配置slave
|
6月前
|
安全 Linux Docker
Ubantu docker学习笔记(六)容器数据卷
Ubantu docker学习笔记(六)容器数据卷
Ubantu docker学习笔记(六)容器数据卷
|
6月前
|
Kubernetes 调度 Docker
Ubantu docker学习笔记(十一)k8s基本操作
Ubantu docker学习笔记(十一)k8s基本操作
|
6月前
|
Prometheus 监控 Cloud Native
Ubantu docker学习笔记(九)容器监控 自带的监控+sysdig+scope+cAdvisor+prometheus
Ubantu docker学习笔记(九)容器监控 自带的监控+sysdig+scope+cAdvisor+prometheus
|
6月前
|
存储 应用服务中间件 nginx
Ubantu docker学习笔记(八)私有仓库
Ubantu docker学习笔记(八)私有仓库
|
6月前
|
网络协议 Docker 容器
Ubantu docker学习笔记(七)容器网络
Ubantu docker学习笔记(七)容器网络
下一篇
无影云桌面