您现在的位置是:首页 >技术教程 >Docker环境/命令/数据卷网站首页技术教程

Docker环境/命令/数据卷

Rkun18 2024-05-31 00:00:02
简介Docker环境/命令/数据卷

Docker基本环境

docker安装

  • yum包更新
 yum update
  • 安装软件包 yum-util提供yum-config-managet功能 其他为devicemapper取得依赖
 yum install -y yum-utils device-mapper-persistent-data lvm2
  • 设置yum源
[root@server ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
已加载插件:fastestmirror
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
  • 安装docker ,出现输入按下y
[root@server ~]# yum install -y docker-ce
  • 查看版本,验证安装是否成功
[root@server ~]# docker -v
Docker version 23.0.5, build bc4487a

配置docker镜像加速器

容器镜像服务 (aliyun.com)

进入阿里云镜像加速器页面,按照提示执行命令,每个人参数不相同

[root@server ~]# sudo mkdir -p /etc/docker
[root@server ~]# sudo tee /etc/docker/daemon.json <<-'EOF'
> {
>   "registry-mirrors": ["https://m03eesz2.mirror.aliyuncs.com"]
> }
> EOF
{
  "registry-mirrors": ["https://m03eesz2.mirror.aliyuncs.com"]
}
[root@server ~]# sudo systemctl daemon-reload
[root@server ~]# sudo systemctl restart docker
[root@server ~]# cat  /etc/docker/daemon.json
{
  "registry-mirrors": ["https://m03eesz2.mirror.aliyuncs.com"]
}

Docker命令

服务命令

  • 启动docker
[root@server ~]# systemctl start docker
  • 关闭docker
[root@server ~]# systemctl stop docker
  • 查看docker状态
[root@server ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since 六 2023-05-06 09:53:55 CST; 4min 58s ago
     Docs: https://docs.docker.com
 Main PID: 62302 (dockerd)
    Tasks: 7
   Memory: 25.7M
   CGroup: /system.slice/docker.service
           └─62302 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/co...

5月 06 09:53:53 server.cluster.com systemd[1]: Starting Docker Application....
5月 06 09:53:53 server.cluster.com dockerd[62302]: time="2023-05-06T09:53:5...
5月 06 09:53:54 server.cluster.com dockerd[62302]: time="2023-05-06T09:53:5...
5月 06 09:53:55 server.cluster.com dockerd[62302]: time="2023-05-06T09:53:5...
5月 06 09:53:55 server.cluster.com dockerd[62302]: time="2023-05-06T09:53:5...
5月 06 09:53:55 server.cluster.com dockerd[62302]: time="2023-05-06T09:53:5...
5月 06 09:53:55 server.cluster.com dockerd[62302]: time="2023-05-06T09:53:5...
5月 06 09:53:55 server.cluster.com systemd[1]: Started Docker Application ....
5月 06 09:53:55 server.cluster.com dockerd[62302]: time="2023-05-06T09:53:5...
Hint: Some lines were ellipsized, use -l to show in full.
  • 设置自启动
[root@server ~]# systemctl enable docker

镜像命令

  • 查询镜像
[root@server ~]# docker images
[root@server ~]# docker search redis  #选择redis
  • 下载镜像
[root@server ~]# docker pull redis  不写版本号默认最新版本下载
Using default tag: latest

latest: Pulling from library/redis
a2abf6c4d29d: Pull complete
c7a4e4382001: Pull complete
4044b9ba67c9: Pull complete
c8388a79482f: Pull complete
413c8bb60be2: Pull complete
1abfd3011519: Pull complete
Digest: sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest   

查看

[root@server ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
redis        latest    7614ae9453d1   16 months ago   113MB

下载指定版本
[root@server ~]# docker pull redis:5.0
5.0: Pulling from library/redis
a2abf6c4d29d: Already exists
c7a4e4382001: Already exists
4044b9ba67c9: Already exists
106f2419edf3: Pull complete
9772114922b9: Pull complete
63031aedd0c4: Pull complete
Digest: sha256:a30e893aa92ea4b57baf51e5602f1657ec5553b65e62ba4581a71e161e82868a
Status: Downloaded newer image for redis:5.0
docker.io/library/redis:5.0
[root@server ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
redis        5.0       c5da061a611a   16 months ago   110MB
redis        latest    7614ae9453d1   16 months ago   113MB
  • 删除镜像
[root@server ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
redis        5.0       c5da061a611a   16 months ago   110MB
redis        latest    7614ae9453d1   16 months ago   113MB
[root@server ~]# docker rmi 7614ae9453d1   删除指定镜像 后面+镜像ID
Untagged: redis:latest
Untagged: redis@sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339
Deleted: sha256:7614ae9453d1d87e740a2056257a6de7135c84037c367e1fffa92ae922784631
Deleted: sha256:49c70179bc923a7d48583d58e2b6c21bde1787edf42ed1f8de9e9b96e2e88e65
Deleted: sha256:396e06df5d1120368a7a8a4fd1e5467cdc2dd4083660890df078c654596ddc1c
Deleted: sha256:434d118df2e9edb51238f6ba46e9efdfa21be68e88f54787531aa39a720a0740
[root@server ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
redis        5.0       c5da061a611a   16 months ago   110MB
  • 查看所有镜像id
docker images -q

容器命令

  • i 容器一直运行

  • t 给容器分配终端

  • name 给容器起名

  • /bin/bash 进入容器初始化指令

[root@server ~]# docker run -it --name=c1 centos:7 /bin/bash
[root@4450c148a0d0 /]#
[root@4450c148a0d0 /]# ll      容器内部使用命令
total 12
-rw-r--r--.   1 root root 12114 Nov 13  2020 anaconda-post.log
lrwxrwxrwx.   1 root root     7 Nov 13  2020 bin -> usr/bin
drwxr-xr-x.   5 root root   360 May  6 02:33 dev
drwxr-xr-x.   1 root root    66 May  6 02:33 etc
drwxr-xr-x.   2 root root     6 Apr 11  2018 home
lrwxrwxrwx.   1 root root     7 Nov 13  2020 lib -> usr/lib
lrwxrwxrwx.   1 root root     9 Nov 13  2020 lib64 -> usr/lib64
drwxr-xr-x.   2 root root     6 Apr 11  2018 media
drwxr-xr-x.   2 root root     6 Apr 11  2018 mnt
drwxr-xr-x.   2 root root     6 Apr 11  2018 opt
dr-xr-xr-x. 124 root root     0 May  6 02:33 proc
dr-xr-x---.   2 root root   114 Nov 13  2020 root
drwxr-xr-x.  11 root root   148 Nov 13  2020 run
lrwxrwxrwx.   1 root root     8 Nov 13  2020 sbin -> usr/sbin
drwxr-xr-x.   2 root root     6 Apr 11  2018 srv
dr-xr-xr-x.  13 root root     0 May  6 01:14 sys
drwxrwxrwt.   7 root root   132 Nov 13  2020 tmp
drwxr-xr-x.  13 root root   155 Nov 13  2020 usr
drwxr-xr-x.  18 root root   238 Nov 13  2020 var



exit 退出容器


查看存在容器
[root@server ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

-it创建容器退出后自动关闭

  • d后台运行创建
[root@server ~]# docker run -id --name=c2 centos:7 
6d29a31959fe7e56dd7311bfc26f164d15d66e69b8a7095a24af0d23c9ca67a8

 进入容器
[root@server ~]# docker exec -it c2 /bin/bash
[root@6d29a31959fe /]#

 exit推出后使用d参数的容器是不会自动关闭

创建了三个容器查看状态

[root@server ~]# docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                      PORTS     NAMES
ccd4d8a00ac4   centos:7   "/bin/bash"   8 minutes ago    Up 8 minutes                          c3
6d29a31959fe   centos:7   "/bin/bash"   10 minutes ago   Up 10 minutes                         c2
4450c148a0d0   centos:7   "/bin/bash"   17 minutes ago   Exited (0) 14 minutes ago             c1
 关闭c2容器
 [root@server ~]# docker stop c2
c2
[root@server ~]# docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                       PORTS     N    AMES
ccd4d8a00ac4   centos:7   "/bin/bash"   9 minutes ago    Up 9 minutes                           c    3
6d29a31959fe   centos:7   "/bin/bash"   12 minutes ago   Exited (137) 5 seconds ago             c    2
4450c148a0d0   centos:7   "/bin/bash"   18 minutes ago   Exited (0) 16 minutes ago              c    1
  • 删除容器
删除c1容器

[root@server ~]# docker rm c1
c1
[root@server ~]# docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                       PORTS     NAMES
ccd4d8a00ac4   centos:7   "/bin/bash"   11 minutes ago   Up 11 minutes                          c3
6d29a31959fe   centos:7   "/bin/bash"   14 minutes ago   Exited (137) 2 minutes ago             c2

开启的容器不能删除,只要把容器关闭才能删除

  • 查看容器信息
docker inspect c2
查看容器c2

数据卷

  • 容器删除后,容器产生数据还存在嘛? 不存在

  • 容器能否和外部机器直接交换文件? 不能

  • 容器之间想要进行数据交互?

数据卷是宿主机中的一个文件或者目录

  • 当容器目录和数据卷目录绑定后,对方修改会同步

  • 数据卷解决上面三个问题

  • 一个数据卷可以被多个容器同时挂载

  • 一个容器也能挂载多个数据卷

配置数据卷

  • 创建启动容器时,使用-v参数 设置数据卷

  • 目录必须绝对路径

  • 如果目录不存在,会自动创建

  • 可以挂载多个数据卷

首先清空容器
 docker stop $(docker ps -aq)

docker rm $(docker ps -aq)

[root@server ~]# docker run -it --name=c1 -v /root/data:/root/data_container centos:7 /bin/bash
[root@f28485e64589 /]# cd ~
[root@f28485e64589 ~]# ll
total 4
-rw-------. 1 root root 3416 Nov 13  2020 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 May  6 03:38 data_container 

[root@server ~]# ll
总用量 4
-rw-------. 1 root root 1241 424 13:04 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 56 11:38 data
宿主机新建文件
[root@server ~]# cd data
[root@server data]# touch d1.txt 

查看容器
[root@f28485e64589 ~]# cd data_container/
[root@f28485e64589 data_container]# ll
total 0
-rw-r--r--. 1 root root 0 May  6 03:40 d1.txt

文件被同步过去了


在容器中创建文件写入一些信息

[root@f28485e64589 data_container]# echo "hello docker" > d2.txt

 在宿主机里也能被同步过去
[root@server data]# cat d2.txt
hello docker


删除容器
[root@f28485e64589 data_container]# exit
exit
[root@server ~]# docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                     PORTS     NAMES
f28485e64589   centos:7   "/bin/bash"   15 minutes ago   Exited (0) 5 seconds ago             c1
[root@server ~]# docker rm c1
c1  

查看宿主机内容


[root@server ~]# ll data
总用量 4
-rw-r--r--. 1 root root  0 56 11:40 d1.txt
-rw-r--r--. 1 root root 13 56 11:45 d2.txt 
文件依旧存在

重新生成容器挂载数据卷,查看能否同步

[root@server ~]# docker run -it --name=c1 -v /root/data:/root/data_container centos:7 /bin/bash
[root@3a985bd81335 /]# cd /root/data_container/
[root@3a985bd81335 data_container]# ll
total 4
-rw-r--r--. 1 root root  0 May  6 03:40 d1.txt
-rw-r--r--. 1 root root 13 May  6 03:45 d2.txt

两个容器挂载同一个数据卷,实现两个容器的数据交换

创建c3容器
[root@server data]# docker run -it --name=c3 -v /root/data:/root/data centos:7
[root@8c026ecd619c /]# cd ~
[root@8c026ecd619c ~]# ls data
d1.txt  d2.txt  

创建c4容器
[root@server ~]# docker run -it --name=c4 -v /root/data:/root/data centos:7
[root@dcf3525a84c7 /]# cd ~/data
[root@dcf3525a84c7 data]# ls
d1.txt  d2.txt



容器c4创建文件写入内容
[root@dcf3525a84c7 data]# echo "docker test" > test.txt  

在容器c3里查看数据能否共享

[root@8c026ecd619c ~]# ls data
d1.txt  d2.txt  test.txt


[root@8c026ecd619c ~]# cat ~/data/test.txt
docker test


数据卷容器

多容器进行数据交换

  • 多个容器挂载同一个数据卷

  • 数据卷容器

首先停止容器,然后删除所有容器

  • 创建启动c3容器,使用-v参数设置数据卷
docker run -it --name=c3 -v /vloume centos:7 /bin/bash

  • 查看c3数据卷目录
 [root@server ~]# docker inspect c3

  这段内容的Source就是数据卷目录
 
 "Mounts": [
            {
                "Type": "volume",
                "Name": "eceb391e8fed25bc8c306fc127708dcf134184ff778cb2041b7d9267d53f1835",
                "Source": "/var/lib/docker/volumes/eceb391e8fed25bc8c306fc127708dcf134184ff778cb2041b7d9267d53f1835/_data",
                "Destination": "/vloume",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],


  • 创建c1 c2容器 使用–volumes-from 参数 设置数据卷
 
[root@server ~]# docker run -it --name=c1 --volumes-from c3 centos:7
[root@05f67331eabb /]# ll
total 12
-rw-r--r--.   1 root root 12114 Nov 13  2020 anaconda-post.log
lrwxrwxrwx.   1 root root     7 Nov 13  2020 bin -> usr/bin
drwxr-xr-x.   5 root root   360 May  6 12:09 dev
drwxr-xr-x.   1 root root    66 May  6 12:09 etc
drwxr-xr-x.   2 root root     6 Apr 11  2018 home
lrwxrwxrwx.   1 root root     7 Nov 13  2020 lib -> usr/lib
lrwxrwxrwx.   1 root root     9 Nov 13  2020 lib64 -> usr/lib64
drwxr-xr-x.   2 root root     6 Apr 11  2018 media
drwxr-xr-x.   2 root root     6 Apr 11  2018 mnt
drwxr-xr-x.   2 root root     6 Apr 11  2018 opt
dr-xr-xr-x. 139 root root     0 May  6 12:09 proc
dr-xr-x---.   2 root root   114 Nov 13  2020 root
drwxr-xr-x.  11 root root   148 Nov 13  2020 run
lrwxrwxrwx.   1 root root     8 Nov 13  2020 sbin -> usr/sbin
drwxr-xr-x.   2 root root     6 Apr 11  2018 srv
dr-xr-xr-x.  13 root root     0 May  6 01:14 sys
drwxrwxrwt.   7 root root   132 Nov 13  2020 tmp
drwxr-xr-x.  13 root root   155 Nov 13  2020 usr
drwxr-xr-x.  18 root root   238 Nov 13  2020 var
drwxr-xr-x.   2 root root     6 May  6 12:01 vloume   #c1也有vloume目录

[root@server ~]# docker run -it --name=c2 --volumes-from c3 centos:7
[root@87a6097e8df0 /]# ll
total 12
-rw-r--r--.   1 root root 12114 Nov 13  2020 anaconda-post.log
lrwxrwxrwx.   1 root root     7 Nov 13  2020 bin -> usr/bin
drwxr-xr-x.   5 root root   360 May  6 12:13 dev
drwxr-xr-x.   1 root root    66 May  6 12:13 etc
drwxr-xr-x.   2 root root     6 Apr 11  2018 home
lrwxrwxrwx.   1 root root     7 Nov 13  2020 lib -> usr/lib
lrwxrwxrwx.   1 root root     9 Nov 13  2020 lib64 -> usr/lib64
drwxr-xr-x.   2 root root     6 Apr 11  2018 media
drwxr-xr-x.   2 root root     6 Apr 11  2018 mnt
drwxr-xr-x.   2 root root     6 Apr 11  2018 opt
dr-xr-xr-x. 145 root root     0 May  6 12:13 proc
dr-xr-x---.   2 root root   114 Nov 13  2020 root
drwxr-xr-x.  11 root root   148 Nov 13  2020 run
lrwxrwxrwx.   1 root root     8 Nov 13  2020 sbin -> usr/sbin
drwxr-xr-x.   2 root root     6 Apr 11  2018 srv
dr-xr-xr-x.  13 root root     0 May  6 01:14 sys
drwxrwxrwt.   7 root root   132 Nov 13  2020 tmp
drwxr-xr-x.  13 root root   155 Nov 13  2020 usr
drwxr-xr-x.  18 root root   238 Nov 13  2020 var
drwxr-xr-x.   2 root root     6 May  6 12:01 vloume

  • 测试
c3在vloume新建文件,写入内容

[root@db1d435dd685 /]# cd vloume/
[root@db1d435dd685 vloume]# echo "docker test" >test

 在c1 c2里查看你能否接收到 

[root@05f67331eabb /]# cat vloume/test
docker test  

[root@87a6097e8df0 /]# cat vloume/test
docker test


可以接收到


删除数据卷容器c3
[root@db1d435dd685 vloume]# exit
exit
[root@server data]# docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                      PORTS     NAMES
87a6097e8df0   centos:7   "/bin/bash"   6 minutes ago    Up 6 minutes                          c2
05f67331eabb   centos:7   "/bin/bash"   9 minutes ago    Up 9 minutes                          c1
db1d435dd685   centos:7   "/bin/bash"   18 minutes ago   Exited (0) 13 seconds ago             c3
[root@server data]# docker rm c3
c3
[root@server data]# docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS          PORTS     NAMES
87a6097e8df0   centos:7   "/bin/bash"   6 minutes ago    Up 6 minutes              c2
05f67331eabb   centos:7   "/bin/bash"   10 minutes ago   Up 10 minutes              c1

然后再在宿主机上c3挂载的数据卷,追加一些数据在文件里,查看c1c2能否被同步数据
[root@server data]# cd /var/lib/docker/volumes/eceb391e8fed25bc8c306fc127708dcf134184ff778cb2041b7d9267d53f1835/_data
[root@server _data]# ll
总用量 4
-rw-r--r--. 1 root root 12 56 20:15 test
[root@server _data]# echo "some info" >> test
 
[root@05f67331eabb /]# cat vloume/test
docker test
some info
 


[root@87a6097e8df0 /]# cat vloume/test
docker test
some info

就算删掉了数据卷容器c3,c1 c2也能同步数据
 
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。