docker安装配置、docker命令

一、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 mysqldocker 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 --helpUsage:  docker save [OPTIONS] IMAGE [IMAGE...]Save one or more images to a tar archive (streamed to STDOUT by default)Aliases:docker image save, docker saveOptions:-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 --helpUsage:  docker load [OPTIONS]Load an image from a tar archive or STDINAliases:docker image load, docker loadOptions:-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 - GPLCopyright (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
└── Dockerfile0 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 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::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 1000link/ether 00:0c:29:a0:84:74 brd ff:ff:ff:ff:ff:ffinet 192.168.179.128/24 brd 192.168.179.255 scope global noprefixroute dynamic ens33valid_lft 1212sec preferred_lft 1212secinet6 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:ffinet 172.17.0.1/16 brd 172.17.255.255 scope global docker0valid_lft forever preferred_lft foreverinet6 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 0inet6 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:ffinet 172.18.0.1/16 brd 172.18.255.255 scope global br-9e0f10bb1bf2valid_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.mzph.cn/news/878426.shtml

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

相关文章

1999-2023年上市公司年报文本数据(PDF+TXT)

1999-2023年上市公司年报文本数据&#xff08;PDFTXT&#xff09; 1、时间&#xff1a;1999-2023年 2、来源&#xff1a;上市公司年度报告 3、范围&#xff1a;A股上市公司&#xff0c;5600企业&#xff0c;6.3W份 4、格式&#xff1a;PDFTXT 5、下载链接&#xff1a; 199…

c++11新特性-lambda表达式

1. 概念 lambda表达式实际上是一个匿名类的成员函数&#xff0c;该类由编译器为lambda创建&#xff0c;该函数被隐式地定义为内联。因此&#xff0c;调用lambda表达式相当于直接调用它的operator()函数&#xff0c;这个函数可以被编译器内联优化&#xff08;建议&#xff09;。…

Ubuntu服务器时间和本地时间不一致怎么解决——Linux的Local Time和RTC time

最近一直在搞大模型的相关工作&#xff0c;所以一直在用Linux服务器&#xff0c;前面的文章里也提到了&#xff0c;我用的是一台Dell PowerEdge R730xd。 但在使用中发现&#xff0c;IDRAC中的日志时间和本地时间存在时差&#xff0c;大概相关8小时。 对于技术人员&#xff0c…

数据结构:树形结构(树、堆)详解

数据结构&#xff1a;树形结构&#xff08;树、堆&#xff09;详解 一、树&#xff08;一&#xff09;树的性质&#xff08;二&#xff09;树的种类二叉树多叉树满N叉树完全N叉树 &#xff08;三&#xff09;二叉树的实现1、二叉树结构定义2、二叉树功能实现&#xff08;1&…

windows安全中心永久卸载工具分享

使用方法 博客&#xff1a;h0ck1r丶羽~从零到一 卸载工具下载链接&#xff1a; 夸克网盘分享 一路回车&#xff0c;选项Y即可 耐心等待几秒种&#xff0c;自动重启 此时打开windows安全中心&#xff0c;已经完全不能使用了&#xff0c;响应的杀毒功能也关了 往期推荐 【渗透测…

QT做一个USB HID设备识别软件

1.下载 HidApi库&#xff1a;GitHub - yigityuce/HidApi: Human Interface Device Api (HidApi) with C 2.pro文件添加 DEFINES - UNICODE LIBS -lsetupapi 3.建立三个对象 HidApi hidApi;HidDevice hidDev;//HID设备HidDeviceList devList;//HID设备列表 4.对 HID 设备进…

JavaWeb - Spring Boot

Spring 官网​​​​​Spring | Home Spring Boot Spring Boot是一个由Pivotal团队提供的开源框架&#xff0c;旨在简化Spring应用的初始搭建以及开发过程。在Spring Boot项目中&#xff0c;通常会有Controller、Service、Mapper和Entity等层次结构。下面将详细介绍这些层次的…

用 Higress AI 网关降低 AI 调用成本 - 阿里云天池云原生编程挑战赛参赛攻略

作者介绍&#xff1a;杨贝宁&#xff0c;爱丁堡大学博士在读&#xff0c;研究方向为向量数据库 《Higress AI 网关挑战赛》正在火热进行中&#xff0c;Higress 社区邀请了目前位于排行榜 top5 的选手杨贝宁同学分享他的心得。下面是他整理的参赛攻略&#xff1a; 背景 我们…

Serilog文档翻译系列(三) - 基础配置

Serilog 使用简单的 C# API 来配置日志记录。当需要外部配置时&#xff0c;可以&#xff08;慎用&#xff09;通过使用 Serilog.Settings.AppSettings 包或 Serilog.Settings.Configuration 包进行混合配置。 创建日志记录器 日志记录器是通过 LoggerConfiguration 对象创建的…

【RabbitMQ】快速上手

目 录 一. RabbitMQ 安装二. RabbitMQ 核心概念2.1 Producer 和 Consumer2.2 Connection 和 Channel2.3 Virtual host2.4 Queue2.5 Exchange2.6 RabbitMQ 工作流程 三. AMQP四. web界面操作4.1 用户相关操作4.2 虚拟主机相关操作 五. RabbitMQ 快速入门5.1 引入依赖5.2 编写生产…

stm32 8080时序驱动lcd屏幕

PSAM使用的硬件接口 PSAM读时序 PSAM写时序 相关时序 PSAM_RCRx NOR 和PSRAM控制寄存器

28. 双耳配对 - 配置

1. 概述 通过MAC地址的最后一位的奇偶来判断左右耳 2. 验证 右耳:奇数(主耳)-》BT ADDR: 12:42:22:34:34:6d 左耳:偶数(从耳)-》BT ADDR: 12:42:22:34:34:6c

TPH-YOLOv5:基于Transformer预测头的改进YOLOv5,用于无人机捕获场景的目标检测

摘要 提出了TPH-YOLOv5。在YOLOv5的基础上&#xff0c;增加了一个预测头来检测不同尺度的目标。然后用Transformer Prediction Heads&#xff08;TPH&#xff09;代替原有的预测头&#xff0c;探索自注意机制的预测潜力。还集成了卷积块注意力模型&#xff08;CBAM&#xff09;…

Gland安装与Debug

下载地址&#xff1a;https://www.jetbrains.com.cn/go/download/#sectionwindows debug官方文档: https://www.jetbrains.com/help/go/debugging-code.html 创建项目 选择新建项目 填写项目本地路径&#xff0c;以及选择go SDK 项目创建后检查项目设置 添加main包以及…

安装MySQL,navicat以及Django配置遇到的一些问题

MySQL安装问题 安装MySQL按照了此文章&#xff1a; MySQL数据库下载及安装教程&#xff08;最最新版&#xff09;_mysql下载安装-CSDN博客https://blog.csdn.net/weixin_39289696/article/details/128850498首先是遇到了starting the server红色叉号显示 按照上面文章的介绍…

Linux--IO多路复用(select,poll,epoll)

IO多路复用——select&#xff0c;poll&#xff0c;epoll IO多路复用是一种操作系统技术&#xff0c;旨在提高系统处理多个输入输出操作的性能和资源利用率。与传统的多线程或多进程模型相比&#xff0c;IO多路复用避免了因阻塞IO而导致的资源浪费和低效率问题。它通过将多个IO…

Linux awk案例

目录 1. 查询时间超过2000毫秒的请求2. 查询指定列组合出现的次数3. 统计所有文件的大小4. 获取大于指定大小的文件名&#xff0c;并按照从大到小排序5. grep指定字段后&#xff0c;使用awk列转行6. 查询第四个字段等于指定值的内容 1. 查询时间超过2000毫秒的请求 ✅log: 202…

[Leetcode 216][Medium]组合总和 III--回溯

目录 一、题目描述 二、整体思路 三、代码 一、题目描述 原题地址 二、整体思路 对于组合问题&#xff0c;首先要想到回溯法。那么可以根据回溯法模版进行设计。 void backtrace(元素){if(满足题目要求的条件){保存目前路径/状态/结果;return;}for循环,往目前状态相邻的所…

区块链通证系统功能分析

区块链通证系统功能分析涉及多个关键方面&#xff0c;以确保系统能够满足不同的业务需求和合规性要求。 同质与非同质通证&#xff1a;区块链通证系统需要支持同质通证&#xff08;如ERC-20&#xff09;和非同质通证&#xff08;如ERC-721&#xff09;&#xff0c;以适应不同类…

《NLP自然语言处理》—— 关键字提取之TF-IDF算法

文章目录 一、TF-IDF算法介绍二、举例说明三、示例&#xff1a;代码实现四、总结 一、TF-IDF算法介绍 TF-IDF&#xff08;Term Frequency-Inverse Document Frequency&#xff09;是一种用于信息检索与文本挖掘的常用加权技术。TF-IDF是一种统计方法&#xff0c;用以评估一个词…