docker安装配置、docker命令

news2024/12/25 12:33:06

一、CentOS7安装docker

1、安装

Docker CE 支持 64 位版本 CentOS 7,并且要求内核版本不低于 3.10, CentOS 7 满足最低内核的要求,所以我们在CentOS 7安装Docker。

  1. 卸载旧docker

    如果之前安装过旧版本的Docker,可以使用下面命令卸载:

    yum remove docker \
                      docker-client \
                      docker-client-latest \
                      docker-common \
                      docker-latest \
                      docker-latest-logrotate \
                      docker-logrotate \
                      docker-selinux \
                      docker-engine-selinux \
                      docker-engine \
                      docker-ce
    
  2. 安装yum工具

    yum install -y yum-utils \
               # device-mapper-persistent-data \
               # lvm2 --skip-broken
    
  3. 然后更新本地镜像源:

    # 设置docker镜像源
    yum-config-manager \
        --add-repo \
        https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    
  4. 输入命令:

    yum install -y docker-ce
    

    docker-ce为社区免费版本。稍等片刻,docker即可安装成功。

2、启动

  1. 输入docker -v有显示并不意味着安装成功;输入docker images如下显示,因为没有启动docker
完毕!
[root@localhost home]# docker -v
Docker version 24.0.7, build afdd53b
[root@localhost home]# 
[root@localhost home]# 
[root@localhost home]# docker images
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[root@localhost home]# 
[root@localhost home]# 
[root@localhost home]# 
  1. Docker应用需要用到各种端口,逐一去修改防火墙设置。非常麻烦,因此直接关闭防火墙。启动docker前,一定要关闭防火墙!!
# 关闭
systemctl stop firewalld
# 禁止开机启动防火墙
systemctl disable firewalld
  1. 通过命令启动docker:
systemctl start docker  # 启动docker服务
   
systemctl stop docker  # 停止docker服务
   
systemctl restart docker  # 重启docker服务

控制台

[root@localhost home]# systemctl stop firewalld
[root@localhost home]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost home]# systemctl start docker
[root@localhost home]# systemctl stop docker
Warning: Stopping docker.service, but it can still be activated by:
  docker.socket
[root@localhost home]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
[root@localhost home]# systemctl restart docker

3、配置镜像加速

ocker官方镜像仓库网速较差,我们需要设置国内镜像服务:

参考阿里云的镜像加速文档:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

配置镜像加速器的地址

---命令1
sudo mkdir -p /etc/docker
---命令2
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://xxxxxywk.mirror.aliyuncs.com"]
}
EOF
---命令3
sudo systemctl daemon-reload
---命令4
sudo systemctl restart docker

二、部署MySQL

只需一行命令就能安装mysql

[root@localhost docker]# docker run -d --name mysql -p 3306:3306 -e TZ=Asia/Shanghai -e MYSQL_ROOT_PASSWORD=root mysql

docker run -d --name nacos \
  -p 8848:8848 \
  -e MODE=standalone \
  -e SPRING_DATASOURCE_PLATFORM=mysql \
  -e MYSQL_SERVICE_HOST=192.168.200.131 \
  -e MYSQL_SERVICE_DB_NAME=nacos_config \
  -e MYSQL_SERVICE_USER=root \
  -e MYSQL_SERVICE_PASSWORD=root \
  nacos/nacos-server:v2.1.0

运行命令,设置mysql密码为root

[root@localhost docker]# docker run -d --name mysql -p 3306:3306 -e TZ=Asia/Shanghai -e MYSQL_ROOT_PASSWORD=root mysql
Unable to find image 'mysql:latest' locally
latest: Pulling from library/mysql
72a69066d2fe: Pull complete 
93619dbc5b36: Pull complete 
99da31dd6142: Pull complete 
626033c43d70: Pull complete 
37d5d7efb64e: Pull complete 
ac563158d721: Pull complete 
d2ba16033dad: Pull complete 
688ba7d5c01a: Pull complete 
00e060b6d11d: Pull complete 
1c04857f594f: Pull complete 
4d7cfa90e6ea: Pull complete 
e0431212d27d: Pull complete 
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:latest
e3aaa62878d1a89635b7f39ec6e1c9b258a4d2a63ee90155473114d03dca0cc9
[root@localhost docker]# 

用navicat连接虚拟机地址的mysql,连接成功

三、docker命令

1、常用命令

  • docker run :创建并运行一个新的容器

    • -d 让容器在后台运行

    • –name mysql 给容器起个名字叫mysql,必须唯一

    • -p 3306:3306 端口映射,宿主机端口和容器内端口,容器内端口取决于容器,不改动;左边宿主机端口可以改动。可以通过访问宿主机的3306端口访问到容器内部的3306端口

    • -e KEY=VALUE 设置环境变量,由镜像决定,需要查看镜像的文档说明

    • mysql 指定运行的镜像的名称,即是什么镜像;正确写法mysql:5.7

      • [repository]:[tag] 标准写法,镜像名:版本号,tag不写默认latest
  • docker stop:停止容器内部的进程,并非停止整个容器,容器还存在

  • docker start:启动容器内的进程(不会创建新的容器)

  • docker ps:查看容器的运行状态,默认只显示运行中的容器

    • -a:所有的包括已停止的容器
  • docker inspect:查看容器详情,ip,端口,镜像,数据卷挂载等信息

  • docker rm:删掉容器

  • docekr exec :进入到容器内部,后续可以在容器内部做一些处理

    docker exec -it [容器名] /bin/bash

  • docker logs:查看日志

  • docker pull:从docker仓库里拉取一个镜像

  • docekr push:将镜像推到仓库

  • docker images:查看本地已经存在了哪些镜像

  • docker rmi:本地删除镜像

2、自定义镜像相关命令

  • docker build:基于dockerfile构建自定义镜像
  • docker save:将镜像保存为压缩文件
  • docker load:将压缩文件加载为镜像
  • docker push:将镜像推到仓库
    在这里插入图片描述

三、部署Nginx

  1. 拉取镜像
[root@localhost ~]# 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@localhost ~]#
  1. 查看本地存在的镜像
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        latest    605c77e624dd   22 months ago   141MB
mysql        latest    3218b38490ce   23 months ago   516MB
[root@localhost ~]# 
  1. 将镜像保存为压缩文件。ll命令为 ls -l的缩写
[root@localhost ~]# 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@localhost ~]# ll
总用量 4
-rw-------. 1 root root 1261 1116 16:42 anaconda-ks.cfg
drwxr-xr-x. 2 root root   20 1117 08:58 test_order
[root@localhost ~]# docker save -o nginx.tar nginx:latest
[root@localhost ~]# ll
总用量 142492
-rw-------. 1 root root      1261 1116 16:42 anaconda-ks.cfg
-rw-------. 1 root root 145905152 1117 15:28 nginx.tar
drwxr-xr-x. 2 root root        20 1117 08:58 test_order
[root@localhost ~]# 
  1. 删除nginx:latest镜像
[root@localhost ~]# docker rmi nginx:latest
Untagged: nginx:latest
Untagged: nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
mysql        latest    3218b38490ce   23 months ago   516MB
[root@localhost ~]# 
  1. 加载镜像压缩文件
[root@localhost ~]# 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@localhost ~]# docker load -i nginx.tar
2edcec3590a4: Loading layer [==================================================>]  83.86MB/83.86MB
e379e8aedd4d: Loading layer [==================================================>]     62MB/62MB
b8d6e692a25e: Loading layer [==================================================>]  3.072kB/3.072kB
f1db227348d0: Loading layer [==================================================>]  4.096kB/4.096kB
32ce5f6a5106: Loading layer [==================================================>]  3.584kB/3.584kB
d874fd2bc83b: Loading layer [==================================================>]  7.168kB/7.168kB
Loaded image: nginx:latest
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        latest    605c77e624dd   22 months ago   141MB
mysql        latest    3218b38490ce   23 months ago   516MB
[root@localhost ~]# 
  1. 创建并运行一个容器
[root@localhost ~]# docker run -d --name nginx -p 80:80 nginx
d23e74f2b63699635312a58dbd2cd1a1dbf95c8816e8a3a91e44dbf4f082a4de
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                   CREATED          STATUS         PORTS                                                  NAMES
d23e74f2b636   nginx     "/docker-entrypoint.…"   13 seconds ago   Up 8 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp                      nginx
e3aaa62878d1   mysql     "docker-entrypoint.s…"   4 hours ago      Up 4 hours     0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql
[root@localhost ~]# 
  1. 使docker ps的以规定格式输出内容
[root@localhost ~]# docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Ports}}\t{{.Status}}\t{{.Names}}"
CONTAINER ID   IMAGE     PORTS                                                  STATUS         NAMES
d23e74f2b636   nginx     0.0.0.0:80->80/tcp, :::80->80/tcp                      Up 4 minutes   nginx
e3aaa62878d1   mysql     0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   Up 4 hours     mysql
[root@localhost ~]#
  1. 停掉容器
[root@localhost ~]# docker stop nginx
nginx
[root@localhost ~]# docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Ports}}\t{{.Status}}\t{{.Names}}"
CONTAINER ID   IMAGE     PORTS                                                  STATUS       NAMES
e3aaa62878d1   mysql     0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   Up 4 hours   mysql
[root@localhost ~]# docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Ports}}\t{{.Status}}\t{{.Names}}" -a
CONTAINER ID   IMAGE     PORTS                                                  STATUS                      NAMES
d23e74f2b636   nginx                                                            Exited (0) 44 seconds ago   nginx
e3aaa62878d1   mysql     0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   Up 4 hours                  mysql
[root@localhost ~]# 
  1. 启动容器
[root@localhost ~]# docker start nginx
nginx
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                   CREATED          STATUS         PORTS                                                  NAMES
d23e74f2b636   nginx     "/docker-entrypoint.…"   11 minutes ago   Up 6 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp                      nginx
e3aaa62878d1   mysql     "docker-entrypoint.s…"   4 hours ago      Up 4 hours     0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql
[root@localhost ~]# 
  1. 查看日志;docker logs -f 会持续跟踪日志–follow
[root@localhost ~]# docker logs nginx
/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/11/17 07:33:50 [notice] 1#1: using the "epoll" event method
2023/11/17 07:33:50 [notice] 1#1: nginx/1.21.5
2023/11/17 07:33:50 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2023/11/17 07:33:50 [notice] 1#1: OS: Linux 3.10.0-957.el7.x86_64
2023/11/17 07:33:50 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 65536:65536
2023/11/17 07:33:50 [notice] 1#1: start worker processes
2023/11/17 07:33:50 [notice] 1#1: start worker process 31
2023/11/17 07:43:37 [notice] 1#1: signal 3 (SIGQUIT) received, shutting down
2023/11/17 07:43:37 [notice] 31#31: gracefully shutting down
2023/11/17 07:43:37 [notice] 31#31: exiting
2023/11/17 07:43:37 [notice] 31#31: exit
2023/11/17 07:43:37 [notice] 1#1: signal 17 (SIGCHLD) received from 31
2023/11/17 07:43:37 [notice] 1#1: worker process 31 exited with code 0
2023/11/17 07:43:37 [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
2023/11/17 07:45:28 [notice] 1#1: using the "epoll" event method
2023/11/17 07:45:28 [notice] 1#1: nginx/1.21.5
2023/11/17 07:45:28 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2023/11/17 07:45:28 [notice] 1#1: OS: Linux 3.10.0-957.el7.x86_64
2023/11/17 07:45:28 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 65536:65536
2023/11/17 07:45:28 [notice] 1#1: start worker processes
2023/11/17 07:45:28 [notice] 1#1: start worker process 24
[root@localhost ~]# 
  1. 进入容器内部 docker exec -it nginx bash(-it是添加一个可输入的终端,使用bash进行交互)

进入nginx容器

[root@localhost ~]# docker exec -it nginx bash
root@d23e74f2b636:/# 
root@d23e74f2b636:/# ls -l
total 12
drwxr-xr-x.   2 root root 4096 Dec 20  2021 bin
drwxr-xr-x.   2 root root    6 Dec 11  2021 boot
drwxr-xr-x.   5 root root  340 Nov 17 07:45 dev
drwxr-xr-x.   1 root root   41 Dec 29  2021 docker-entrypoint.d
-rwxrwxr-x.   1 root root 1202 Dec 29  2021 docker-entrypoint.sh
drwxr-xr-x.   1 root root   19 Nov 17 07:33 etc
drwxr-xr-x.   2 root root    6 Dec 11  2021 home
drwxr-xr-x.   1 root root   45 Dec 20  2021 lib
drwxr-xr-x.   2 root root   34 Dec 20  2021 lib64
drwxr-xr-x.   2 root root    6 Dec 20  2021 media
drwxr-xr-x.   2 root root    6 Dec 20  2021 mnt
drwxr-xr-x.   2 root root    6 Dec 20  2021 opt
dr-xr-xr-x. 148 root root    0 Nov 17 07:45 proc
drwx------.   2 root root   37 Dec 20  2021 root
drwxr-xr-x.   1 root root   23 Nov 17 07:45 run
drwxr-xr-x.   2 root root 4096 Dec 20  2021 sbin
drwxr-xr-x.   2 root root    6 Dec 20  2021 srv
dr-xr-xr-x.  13 root root    0 Nov 17 00:42 sys
drwxrwxrwt.   1 root root    6 Dec 29  2021 tmp
drwxr-xr-x.   1 root root   66 Dec 20  2021 usr
drwxr-xr-x.   1 root root   19 Dec 20  2021 var
root@d23e74f2b636:/# exit
exit
[root@localhost ~]# 

进入mysql容器

[root@localhost ~]# docker exec -it mysql bash
root@e3aaa62878d1:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.27 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.86 sec)

mysql> exit
Bye
root@e3aaa62878d1:/# exit
exit
[root@localhost ~]# 
  1. 删除容器。默认不能删运行中的容器。可以先stop后rm或者rm -f。
[root@localhost ~]# docker rm mysql
Error response from daemon: You cannot remove a running container e3aaa62878d1a89635b7f39ec6e1c9b258a4d2a63ee90155473114d03dca0cc9. Stop the container before attempting removal or force remove
[root@localhost ~]# docker rm mysql -f
mysql
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                   CREATED          STATUS          PORTS                               NAMES
d23e74f2b636   nginx     "/docker-entrypoint.…"   24 minutes ago   Up 13 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx
[root@localhost ~]# 

四、命令别名

~表示root目录

[root@localhost ~]# vi ~/.bashrc
[root@localhost ~]# 
[root@localhost ~]# source ~/.bashrc      //让命令生效
[root@localhost ~]# 
[root@localhost ~]# dps 
CONTAINER ID   IMAGE     PORTS                               STATUS          NAMES
d23e74f2b636   nginx     0.0.0.0:80->80/tcp, :::80->80/tcp   Up 21 minutes   nginx
[root@localhost ~]# dps
CONTAINER ID   IMAGE     PORTS                               STATUS          NAMES
d23e74f2b636   nginx     0.0.0.0:80->80/tcp, :::80->80/tcp   Up 21 minutes   nginx
[root@localhost ~]# 

在这里插入图片描述

五、数据卷挂载

1、修改Nginx静态html文件

目标:1、修改nginx容器内的html目录下的index.html文件内容;2、将静态资源部署到nginx的html目录

  1. 启动服务
[root@localhost test_order]# systemctl start docker
[root@localhost test_order]# dps
CONTAINER ID   IMAGE     PORTS     STATUS    NAMES
[root@localhost test_order]#
  1. 启动nginx容器
[root@localhost test_order]# docker start nginx
nginx
[root@localhost test_order]# 
  1. 进入到容器内部
[root@localhost test_order]# docker exec -it nginx bash
root@d23e74f2b636:/#
  1. 进入到nginx的静态资源目录/usr/share/nginx/html。发现不能使用vi命令。
root@d23e74f2b636:/# cd /usr/share/nginx/html
root@d23e74f2b636:/usr/share/nginx/html# ll
bash: ll: command not found
root@d23e74f2b636:/usr/share/nginx/html# ls
50x.html  index.html
root@d23e74f2b636:/usr/share/nginx/html# vi index.html
bash: vi: command not found
root@d23e74f2b636:/usr/share/nginx/html#

2、数据卷

数据卷:是一个虚拟目录,是容器内目录与宿主机目录之间映射的桥梁。一旦数据卷产生,会实现两个目录的双向绑定。

在这里插入图片描述

因此,可以直接通过在宿主机上修改文件,容器相对应的容器也会被修改。

docker volume --help

数据卷挂载:

​ 在执行docker run命令是,使用 -v 数据卷:容器内目录 可以完成数据卷挂载(只能在这一步挂载,不能在容器创建之后);当创建容器时,如果挂载了数据卷且数据卷不存在,会自动创建数据卷

数据卷挂载后的命令:

命令说明
docker colume create创建数据卷
docker volume ls查看所有数据卷
docker volume rm删除指定数据卷
docker volume inspect [name]查看某个数据卷详情
docker volume prune清除未使用的数据卷

3、重新创建容器并挂载数据卷

  1. 删除之前的nginx容器并重新创建,同时挂载一个html数据卷,挂载的容器内目录为/usr/share/nginx/html
[root@localhost test_order]# docker rm -f nginx
nginx
[root@localhost test_order]# dps
CONTAINER ID   IMAGE     PORTS     STATUS    NAMES
[root@localhost test_order]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost test_order]# docker run -d --name nginx -p 80:80 -v html:/usr/share/nginx/html nginx
243562e523d1f52211539fa8dd2908eedf6586b8b11896ccbd216140d9898252
[root@localhost test_order]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                   CREATED          STATUS          PORTS                               NAMES
243562e523d1   nginx     "/docker-entrypoint.…"   13 seconds ago   Up 12 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx
[root@localhost test_order]#
  1. 查看数据卷详情,数据卷被挂载到/var/lib/docker/volumes/下,且/var/lib/docker/volumes/html/_data下的文件与nginx容器内/usr/share/nginx/html/下人文件相同
[root@localhost test_order]# docker volume ls
DRIVER    VOLUME NAME
local     d5a198ffeee2c28589adb04619336b1236035473033c11c37055c4a1c7d3c792
local     html
[root@localhost test_order]# docker volume inspect html
[
    {
        "CreatedAt": "2023-11-20T10:02:41+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/html/_data",
        "Name": "html",
        "Options": null,
        "Scope": "local"
    }
]
[root@localhost test_order]# cd /var/lib/docker/volumes/html/_data
[root@localhost _data]# ll
总用量 8
-rw-r--r--. 1 root root 497 1228 2021 50x.html
-rw-r--r--. 1 root root 615 1228 2021 index.html
[root@localhost _data]# 
  1. 直接在/var/lib/docker/volumes/html/_data目录下修改index.html文件
    在这里插入图片描述

  2. 成功修改
    在这里插入图片描述

  3. 此外,可以将资源放到/var/lib/docker/volumes/html/_data目录下实现数据迁移。

六、本地目录挂载

数据卷挂载的弊端:1、匿名数据卷不方便使用;2、宿主机目录固定为/var/lib/docker/volumes/

本地目录挂载:在执行docker run命令时,使用 -v 本地目录:容器内目录 可以完成本地目录挂载(只能在这一步挂载,不能在容器创建之后);本地目录必须以"/“或”./"开头,否则会被识别为数据卷而非本地目录

七、自定义镜像

1、DockerFile语法

部署一个Java应用的步骤:

1、准备一个linux服务器

2、安装JRE并配置环境变量

3、拷贝JAR包

4、运行JAR包

指令说明示例
FROM指定基础镜像FROM centos:6
ENV设置环境变量,可在后面指令使用ENV key value
COPY拷贝本地文件到镜像的指定目录COPY ./mysql-5.7.rpm /tmp
RUN执行Linux的shell命令,一般是安装过程的命令RUN yum install gcc
EXPOSE指定容器运行时监听的端口,是给镜像使用者看的EXPOSE 8080
ENTRYPOINT镜像中应用的启动命令,容器运行时调用ENTRYPOINT java -jar xx.jar

2、自定义docker-demo镜像

  1. Dockerfile文件
    在这里插入图片描述

  2. 文件结构

[root@localhost demo]# ll
总用量 25024
-rw-r--r--. 1 root root 25620395 1120 13:59 docker-demo.jar
-rw-r--r--. 1 root root      250 1120 13:58 Dockerfile
[root@localhost demo]# tree
.
├── docker-demo.jar
└── Dockerfile

0 directories, 2 files
[root@localhost demo]# 
  1. 下载openjdk:11.0-jre-buster基础镜像
[root@localhost demo]# docker pull openjdk:11.0-jre-buster
11.0-jre-buster: Pulling from library/openjdk
c4cc477c22ba: Pull complete 
077c54d048f1: Pull complete 
0368544993b2: Pull complete 
d2b3c389e55f: Pull complete 
7fde22603cbd: Pull complete 
e540594d7c47: Pull complete 
Digest: sha256:3546a17e6fb4ff4fa681c38f3f6644efd393f9bb7ed6ebbd85f06065f5d570ed
Status: Downloaded newer image for openjdk:11.0-jre-buster
docker.io/library/openjdk:11.0-jre-buster
[root@localhost demo]# docker images
REPOSITORY   TAG               IMAGE ID       CREATED         SIZE
nginx        latest            605c77e624dd   23 months ago   141MB
mysql        latest            3218b38490ce   23 months ago   516MB
openjdk      11.0-jre-buster   57925f2e4cff   23 months ago   301MB
[root@localhost demo]#
  1. 构建自定义镜像
[root@localhost demo]# docker build -t docker-demo .
[+] Building 4.8s (8/8) FINISHED                                                                                                                                                                                                docker:default
 => [internal] load build definition from Dockerfile                                                                                                                                                                                      0.0s
 => => transferring dockerfile: 349B                                                                                                                                                                                                      0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                         0.0s
 => => transferring context: 2B                                                                                                                                                                                                           0.0s
 => [internal] load metadata for docker.io/library/openjdk:11.0-jre-buster                                                                                                                                                                0.0s
 => [1/3] FROM docker.io/library/openjdk:11.0-jre-buster                                                                                                                                                                                  0.1s
 => [internal] load build context                                                                                                                                                                                                         4.1s
 => => transferring context: 25.63MB                                                                                                                                                                                                      4.0s
 => [2/3] RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo Asia/Shanghai > /etc/timezone                                                                                                                              4.4s
 => [3/3] COPY docker-demo.jar /app.jar                                                                                                                                                                                                   0.1s
 => exporting to image                                                                                                                                                                                                                    0.1s
 => => exporting layers                                                                                                                                                                                                                   0.1s
 => => writing image sha256:b659fb9f1b714016b9a0a7d15346046e7fa710204aa3c301d02e35a3f37ca2f0                                                                                                                                              0.0s
 => => naming to docker.io/library/docker-demo                                                                                                                                                                                            0.0s
[root@localhost demo]# docker images
REPOSITORY    TAG               IMAGE ID       CREATED          SIZE
docker-demo   latest            b659fb9f1b71   46 seconds ago   327MB
nginx         latest            605c77e624dd   23 months ago    141MB
mysql         latest            3218b38490ce   23 months ago    516MB
openjdk       11.0-jre-buster   57925f2e4cff   23 months ago    301MB
[root@localhost demo]# 
  1. 创建容器
[root@localhost demo]# docker run -d --name dd -p 8090:8090 docker-demo
f2fbba619278e83b7510ed17a5bed4d26abc447cf5a7923fa8fc8b812b1c2394
[root@localhost demo]# dps
CONTAINER ID   IMAGE         PORTS                                       STATUS         NAMES
f2fbba619278   docker-demo   0.0.0.0:8090->8090/tcp, :::8090->8090/tcp   Up 8 seconds   dd
  1. 浏览器测试

在这里插入图片描述

  1. 查看容器日志

在这里插入图片描述

以上说明自定义镜像构建成功

八、容器网络互联

命令说明
docker network create创建一个网络
docker network ls查看指定网络
docker network rm删除指定网络
docker network prune清除未使用的网络
docker network connect使指定容器连接加入某网路
docker network disconnect使指定容器连接离开某网路
docker network inspect查看网络详细信息

目标:使dd容器能够连接mysql容器

  1. 创建新的网络testNet。其中bridge(即docker0)是docker自带的默认网桥,容器创建时如果没有指定网络,则默认处于该连接下。
[root@localhost demo]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
76d6c5c9df88   bridge    bridge    local
5072bd55e284   host      host      local
74ff2fed89b9   none      null      local
[root@localhost demo]# docker network create testNet
9e0f10bb1bf25b5e97e87af223e5a061d11a9a17d125cd508144fb7d223f1b91
[root@localhost demo]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
76d6c5c9df88   bridge    bridge    local
5072bd55e284   host      host      local
74ff2fed89b9   none      null      local
9e0f10bb1bf2   testNet   bridge    local
[root@localhost demo]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:a0:84:74 brd ff:ff:ff:ff:ff:ff
    inet 192.168.179.128/24 brd 192.168.179.255 scope global noprefixroute dynamic ens33
       valid_lft 1212sec preferred_lft 1212sec
    inet6 fe80::e391:3f36:2ff2:da5c/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:2b:a2:a2:50 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:2bff:fea2:a250/64 scope link 
       valid_lft forever preferred_lft forever
13: veth160830c@if12: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether da:ea:f1:b8:42:80 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::d8ea:f1ff:feb8:4280/64 scope link 
       valid_lft forever preferred_lft forever
14: br-9e0f10bb1bf2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:69:ec:d6:61 brd ff:ff:ff:ff:ff:ff
    inet 172.18.0.1/16 brd 172.18.255.255 scope global br-9e0f10bb1bf2
       valid_lft forever preferred_lft forever
[root@localhost demo]# 
  1. mysql容器连接到testNet。发现mysql处于两个网络。
[root@localhost demo]# docker run -d --name mysql -p 3306:3306 -e TZ=Asia/Shanghai -e MYSQL_ROOT_PASSWORD=root mysql
0186ddeb4c108d3accd8068ffee6bd86be3e5d2336b6ae3bd8ffff64c7595f71
[root@localhost demo]# dps
CONTAINER ID   IMAGE         PORTS                                                  STATUS          NAMES
0186ddeb4c10   mysql         0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   Up 2 seconds    mysql
f2fbba619278   docker-demo   0.0.0.0:8090->8090/tcp, :::8090->8090/tcp              Up 35 minutes   dd
[root@localhost demo]# docker network connect testNet mysql
[root@localhost demo]# docker inspect mysql
......
 "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "76d6c5c9df88a644a949b060ff4f188dde5db61ecc42efc9f7358a36d4f164b7",
                    "EndpointID": "806490529ffde5e5860f416fa0df12361927ae67fcbd8b3364bf91c6a96a8c51",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03",
                    "DriverOpts": null
                },
                "testNet": {
                    "IPAMConfig": {},
                    "Links": null,
                    "Aliases": [
                        "0186ddeb4c10"
                    ],
                    "NetworkID": "9e0f10bb1bf25b5e97e87af223e5a061d11a9a17d125cd508144fb7d223f1b91",
                    "EndpointID": "35f2059a5331e5221a4d6346762c74e1f213376a47b19ad5962797abc29ebb4d",
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:12:00:02",
                    "DriverOpts": {}
                }
            }
......
  1. 删除dd容器,重新创建,且在创建时就指定网络,发现dd容器没有默认的bridge网络了。
[root@localhost demo]# docker rm -f dd
dd
[root@localhost demo]# docker run -d --name dd -p 8090:8090 --network testNet docker-demo
266c61091e6fd535e83824c32eb0280a12f8004880eee23d5d2a6b4ef784c36d
[root@localhost demo]# docker inspect dd
......
 "Networks": {
                "testNet": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "266c61091e6f"
                    ],
                    "NetworkID": "9e0f10bb1bf25b5e97e87af223e5a061d11a9a17d125cd508144fb7d223f1b91",
                    "EndpointID": "ada42e9ad84f8b4307ebfe32fa1d08416b651dbe7528af76bffaf152783b97e6",
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:12:00:03",
                    "DriverOpts": null
                }
            }
......
  1. 这时容器可以通过容器名进行网络互联(因为处于同一个网桥里),但是默认的bridge不可以,只能通过ip地址连接(但是没luan用,因为每次启动ip地址会变化)。
[root@localhost demo]# docker exec -it dd bash
root@266c61091e6f:/# ping mysql
PING mysql (172.18.0.2) 56(84) bytes of data.
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=1 ttl=64 time=0.063 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=2 ttl=64 time=0.083 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=3 ttl=64 time=0.039 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=4 ttl=64 time=0.085 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=5 ttl=64 time=0.086 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=6 ttl=64 time=0.081 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=7 ttl=64 time=0.067 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=8 ttl=64 time=0.105 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=9 ttl=64 time=0.060 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=10 ttl=64 time=0.080 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=11 ttl=64 time=0.052 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=12 ttl=64 time=0.061 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=13 ttl=64 time=0.108 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=14 ttl=64 time=0.044 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=15 ttl=64 time=0.049 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=16 ttl=64 time=0.110 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=17 ttl=64 time=0.084 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=18 ttl=64 time=0.057 ms
^C
--- mysql ping statistics ---
18 packets transmitted, 18 received, 0% packet loss, time 21ms
rtt min/avg/max/mdev = 0.039/0.073/0.110/0.021 ms
root@266c61091e6f:/# 

九、DockerCompose

语法:docker compose [options] [command]

-f 指定compose文件的路径和名称

-p指定project名称

指令说明
up创建并启动所有service容器
down停止并移除所有容器、网络
ps列出所有启动的容器
logs查看指定容器的日志
stop停止容器
start启动容器
restart重启容器
top查看运行中的进程
exec在指定的运行中容器中执行命令

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2094937.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Codeforces Round 964 (Div. 4) A-E Java题解

比赛地址 Dashboard - Codeforces Round 964 (Div. 4) - Codeforces A题 签到题 给一个两位数 求各位上的数字和 直接对10取余加上本来的数除以10 // 注意类名必须为 Main, 不要有任何 package xxx 信息 // package Dduo; import java.io.*; import java.math.*; import j…

22:差分线规则

1.那些线是差分对&#xff1a; ①有些特定模块就是差分线&#xff1a;USB&#xff0c;HDMI, 以太网口&#xff0c;LEDS等 设置差分对 Panel打开PCB 输入&#xfe62;和- 点击执行 对90欧姆差分对和100Ω差分对进行分类 设置差分对线宽 ①90ohm 由excel可知&a…

孩子自闭症的主要表现:探寻理解之门

自闭症&#xff0c;也称为孤独症&#xff0c;是一种复杂的神经发展障碍&#xff0c;它影响着孩子的社交互动、沟通能力以及行为模式。当家长注意到孩子出现自闭症倾向时&#xff0c;及时识别并寻求专业帮助至关重要。以下是孩子自闭症的一些主要表现&#xff0c;希望能为家长提…

西安电子科技大学研究生新生大数据

西安电子科技大学研究生新生大数据&#xff0c;来自卓越工程学院—杭州研究院 杭研不少来自双非院校&#xff0c;西电也不怎么歧视双非的

游戏开发设计模式之模板方法模式

目录 模板方法模式在游戏开发中的具体应用案例是什么&#xff1f; 如何在不同类型的游戏&#xff08;如角色扮演游戏、策略游戏等&#xff09;中实现模板方法模式&#xff1f; 模板方法模式与其他设计模式&#xff08;如观察者模式、状态模式等&#xff09;相比&#xff0c;…

实战项目:俄罗斯方块(二)

文章目录 &#x1f34a;自我介绍&#x1f34a;俄罗斯方块数据存储三维数组的简单介绍俄罗斯方块数组的设计类型的设计初始值的方块库的设计输出指定位置的图形输出每种图形及其转换形式代码 你的点赞评论就是对博主最大的鼓励 当然喜欢的小伙伴可以&#xff1a;点赞关注评论收藏…

MySQL出现锁等待Lock wait timeout exceeded该如何快速解决

目录 一、前言 二、锁等待是如何产生的&#xff1f; 三、如何避免锁等待&#xff1f; 四、出现锁等待&#xff0c;如何快速解决&#xff1f; 设置合理的锁等待超时时间 使用DBdoctor及时观测锁等待数据 五、总结 一、前言 在使用MySQL等数据库处理业务时&#xff0c;锁…

面对自闭症的孩子:理解、关爱与支持

自闭症&#xff0c;这个看似遥远却又时常出现在我们生活中的词汇&#xff0c;它影响着无数家庭和孩子。面对自闭症的孩子&#xff0c;我们或许会感到困惑、无助&#xff0c;但更重要的是&#xff0c;我们要给予他们理解、关爱与支持。 我们要做的是深入理解自闭症。自闭症是一种…

Linux创建sysfs属性节点 - DEVICE_ATTR宏、device_create_file()、sysfs_create_group()

目录 简介&#xff1a; 一、DEVICE_ATTR介绍 1、DEVICE_ATTR宏 1.1 参数说明 1.2 调用方法 二、sysfs创建属性文件 1、创建一个sysfs属性文件 1.1 device_create_file()函数 1.2 device_create_file()实例 2、创建多个sysfs属性文件 2.1 sysfs_create_group()函数 2…

AI少女/HS2甜心选择2 仿崩坏3卡系列全合集打包

内含AI少女/甜心选择2 仿崩坏3系列全合集打包共11张 内含&#xff1a;月魄装 幽兰黛尔幽兰黛尔薪炎之律者青鸟之庭帕朵菲莉丝雷电芽衣校服布洛妮娅八重樱 冰海琴音爱衣 悠闲旋律爱莉希雅EVA 明日香。 下载地址&#xff1a;https://www.51888w.com/241.html 部分演示图&#…

java构建工具-maven的复习笔记【适用于复习或者初步了解】

&#x1f939;‍♀️潜意识起点&#xff1a;个人主页 &#x1f399;座右铭&#xff1a;得之坦然&#xff0c;失之淡然。 &#x1f48e;擅长领域&#xff1a;前端 是的&#xff0c;我需要您的&#xff1a; &#x1f9e1;点赞❤️关注&#x1f499;收藏&#x1f49b; 是我持…

鸿蒙UIAbility组件进阶

鸿蒙UIAbility组件进阶 AbilityStage基本概念生命周期使用方式 UIAbility组件间交互启动应用内的UIAbility启动应用内的UIAbility并获取返回结果启动UIAbility的指定页面调用方目标UIAbility冷启动目标UIAbility热启动 AbilityStage 基本概念 在了解AbilityStage之前&#xf…

Promise 工具箱:手写实现静态方法的完全指南

前言 &#x1f4eb; 大家好&#xff0c;我是南木元元&#xff0c;热爱技术和分享&#xff0c;欢迎大家交流&#xff0c;一起学习进步&#xff01; &#x1f345; 个人主页&#xff1a;南木元元 Promise有很多静态方法&#xff0c;本文就来分享下如何实现这些静态方法。 目录 …

Markdown语法与Latex公式汇总

1 基本语法 1.1 标题 语法如下&#xff1a; 效果如下&#xff1a; 1.2 字体样式 语法效果普通正文字体普通正文字体*倾斜字体*倾斜字体**加粗字体**加粗字体***倾斜加粗字体***倾斜字体~~划线字体~~倾斜字体 1.3 分割线 语法如下&#xff1a; 效果如下&#xff1a; …

【C++11及其特性】C++类型转换

C类型转换目录 一.C语言的强制类型转换二.static_cast1.父类子类之间指针或引用的转换2.基本数据类型的转换3.空指针转换其他类型指针4.其他类型指针转换为空指针5.static_cast特点6.完整代码 三.reinterpret_cast1.数值与指针之间的转换2.不同类型指针和引用之间的转换3.reint…

【网络安全】重置邮件逻辑漏洞导致账户接管

未经许可&#xff0c;不得转载。 文章目录 正文 正文 目标&#xff1a;xxx.com 点击重置密码&#xff0c;系统会发送一封链接至邮箱。响应如下&#xff1a; 从上图中可以看到&#xff0c;validationSession对象中有一个sessionID 而收到的链接中的token和sessionID的值是一样…

总结之Coze 是一站式 AI Bot 开发平台——使用coze(一)

Coze 是什么&#xff1f; Coze 是新一代一站式 AI Bot 开发平台。无论你是否有编程基础&#xff0c;都可以在 Coze 平台上快速搭建基于 AI 模型的各类问答 Bot&#xff0c;从解决简单的问答到处理复杂逻辑的对话。并且&#xff0c;你可以将搭建的 Bot 发布到各类社交平台和通讯…

[Leetcode 51][Hard]-n皇后问题-回溯

目录 一、题目描述 二、整体思路 三、代码 一、题目描述 原题地址 二、整体思路 这种可以算是组合问题的变种&#xff0c;在回溯函数中我们要保存当前已放置皇后的所有位置&#xff0c;同时递归调用时要进行寻找下一个皇后的放置位置。那么我们可以逐行遍历棋盘并作为递归调…

STM32学习记录-10-2-SPI通信(硬件)

1 SPI外设简介 STM32内部集成了硬件SPI收发电路,可以由硬件自动执行时钟生成、数据收发等功能,减轻CPU的负担 可配置8位/16位数据帧、高位先行/低位先行 时钟频率: fPCLK / (2, 4, 8, 16, 32, 64, 128, 256) 支持多主机模型、主或从操作 可精简为半双工/单工通信 支持…

python源码 PBOCMaster MAC的计算函数及计算过程 2des

注意最后一步要用整个key加密 计算过程&#xff1a; MAC&#xff1a; PBOC-MAC DES算法 密钥 长度16(0x10)字节 57 75 20 4D 69 61 6F 6A 75 6E 40 47 26 44 43 11 初始向量 长度8(0x08)字节 00 00 00 00 00 00 00 00 数据 长度74(0x4A)字节 43 48 45 4E 48 41 4F 2D 50 43 7…