• Docker 常用命令 - 容器数据卷


    容器数据卷(run -v 主机目录:容器内目录)

    将容器内的目录挂载至主机上,目的是为容器的持久化与同步操作,容器之间可以共享数据。

    docker run -v 主机目录:容器内目录

    1. [root@k8snode docker]# docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=abc --name mysql01 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql mysql
    2. 38f9bebd5ffb813ebd5cb0cf1d8fd6b862aa6c3af0199b90090c39db9a7ddac8
    3. # 查看主机挂载目录
    4. [root@k8snode docker]# ls /home/mysql
    5. conf data

    docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=abc --name mysql01 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql mysql

    -d 后台运行

    -e 环境配置

    -p 端囗映射(主机端囗:容器端囗)

    -v 容器数据卷挂载(主机目录:容器内目录)

    --name 容器名称

    使用主机的MySQL客户端连接虚拟机中的docker启动的mysql,连接成功(注意:需要开启虚拟机中的3306端囗),如下图所示: 

    查看容器数据卷

     docker volume xx

    1. [root@k8snode docker]# docker volume --help
    2. Usage: docker volume COMMAND
    3. Manage volumes
    4. Commands:
    5. create Create a volume
    6. inspect Display detailed information on one or more volumes
    7. ls List volumes
    8. prune Remove all unused local volumes
    9. rm Remove one or more volumes
    10. Run 'docker volume COMMAND --help' for more information on a command.
    11. [root@k8snode docker]#

    具名挂载

    docker run -v 容器数据卷名称:容器内路径

    1. [root@k8snode docker]# docker run -d --name centos01 -v centos_volume:/home centos
    2. 20e2b5b37066cd3b0a7efa02e61c89d389b65a43cdf7f5ff346539ed27836b40
    3. [root@k8snode docker]# docker volume ls
    4. DRIVER VOLUME NAME
    5. local centos_volume
    6. [root@k8snode docker]# docker volume inspect centos_volume
    7. [
    8. {
    9. "CreatedAt": "2022-06-19T05:58:48+08:00",
    10. "Driver": "local",
    11. "Labels": null,
    12. "Mountpoint": "/var/lib/docker/volumes/centos_volume/_data",
    13. "Name": "centos_volume",
    14. "Options": null,
    15. "Scope": "local"
    16. }
    17. ]
    18. #查看docker目录中的volume目录
    19. [root@k8snode docker]# ls /var/lib/docker/volumes/
    20. backingFsBlockDev centos_volume metadata.db

    匿名挂载

    docker run -v 容器内的路径

    # 只有容器内的路径,没有主机路径

    1. [root@k8snode docker]# docker run -d --name cenots02 -v /home centos
    2. 5e82d50161f601f2db7f112fb7627af666bcce4a32a4f8f2c32ac051f1aa5019
    3. [root@k8snode docker]# docker volume ls
    4. DRIVER VOLUME NAME
    5. local 8f15c1d4a8e486e64dcffc868bccfb129a1eafafd49c014d5faafeafe1d01eb4
    6. local centos_volume
    7. [root@k8snode docker]# docker volume inspect 8f15c1d4a8e486e64dcffc868bccfb129a1eafafd49c014d5faafeafe1d01eb4
    8. [
    9. {
    10. "CreatedAt": "2022-06-19T06:08:52+08:00",
    11. "Driver": "local",
    12. "Labels": null,
    13. "Mountpoint": "/var/lib/docker/volumes/8f15c1d4a8e486e64dcffc868bccfb129a1eafafd49c014d5faafeafe1d01eb4/_data",
    14. "Name": "8f15c1d4a8e486e64dcffc868bccfb129a1eafafd49c014d5faafeafe1d01eb4",
    15. "Options": null,
    16. "Scope": "local"
    17. }
    18. ]

    设置读写权限

    docker run -v /home:ro  # 容器中目录只读 readonly,只能通过宿主机操作

    docker run -v /home:rw  # 容器中目录可读可写 read-write(默认)

    Dockerfile

    常用指令说明

    常用指令说明
    指令说明
    FROM基础镜像,一切从这FROM开始构建
    MAINTAINER作者:姓名+邮箱
    RUN镜像构建时需要运行的命令
    ADD添加的内容,压缩包会自动解压
    COPY类似于ADD,将文件拷贝到镜像中
    WORKDIR镜像的工作目录
    VOLUME容器数据卷(挂载的目录)
    EXPOST暴露端囗
    CMD容器启动时要运行的命令,只有最后一个命令会生效(命令被覆盖)
    ENTRYPOINT容器启动时要运行的命令,可以追加命令
    ONBUILD当构建一个被继承的Dockerfile时会运行ONBUILD指令(触发指令)
    ENV构建时设置环境变量

    Docker Hub中99%镜像都是从FROM scratch开始的。

    构建centos

    (1)编写Dockerfile文件 

    vim Dockerfile

    [root@k8snode docker]# vim Dockerfile

    文件内容如下: 

    FROM centos:7

    MAINTAINER tracy

    RUN mkdir /usr/local/golang

    ENV GOPATH /usr/local/golang

    WORKDIR $GOPATH

    RUN yum install -y vim

    EXPOSE 8088

    CMD echo $GOPATH

    CMD /bin/bash

    按Esc退出编辑,输入:wq保存并退出。 

    (2)构建镜像

    查看docker build帮助

     docker build --help

    1. [root@k8snode ~]# docker build --help
    2. Usage: docker build [OPTIONS] PATH | URL | -
    3. Build an image from a Dockerfile
    4. Options:
    5. --add-host list Add a custom host-to-IP mapping (host:ip)
    6. --build-arg list Set build-time variables
    7. --cache-from strings Images to consider as cache sources
    8. --cgroup-parent string Optional parent cgroup for the container
    9. --compress Compress the build context using gzip
    10. --cpu-period int Limit the CPU CFS (Completely Fair Scheduler) period
    11. --cpu-quota int Limit the CPU CFS (Completely Fair Scheduler) quota
    12. -c, --cpu-shares int CPU shares (relative weight)
    13. --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
    14. --cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
    15. --disable-content-trust Skip image verification (default true)
    16. -f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile')
    17. --force-rm Always remove intermediate containers
    18. --iidfile string Write the image ID to the file
    19. --isolation string Container isolation technology
    20. --label list Set metadata for an image
    21. -m, --memory bytes Memory limit
    22. --memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
    23. --network string Set the networking mode for the RUN instructions during build (default "default")
    24. --no-cache Do not use cache when building the image
    25. --pull Always attempt to pull a newer version of the image
    26. -q, --quiet Suppress the build output and print image ID on success
    27. --rm Remove intermediate containers after a successful build (default true)
    28. --security-opt strings Security options
    29. --shm-size bytes Size of /dev/shm
    30. -t, --tag list Name and optionally a tag in the 'name:tag' format
    31. --target string Set the target build stage to build.
    32. --ulimit ulimit Ulimit options (default [])

     构建镜像

    docker build -f dockerfile文件路径 -t 镜像名称:[TAG] .

    # 最后的 .表示当前路径

    1. [root@k8snode docker]# docker build -f Dockerfile -t my_first_centos:1.0 .
    2. Sending build context to Docker daemon 2.048kB
    3. Step 1/9 : FROM centos:7
    4. 7: Pulling from library/centos
    5. 2d473b07cdd5: Pull complete
    6. Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
    7. Status: Downloaded newer image for centos:7
    8. ---> eeb6ee3f44bd
    9. Step 2/9 : MAINTAINER tracy
    10. ---> Running in c0e7785b55ca
    11. Removing intermediate container c0e7785b55ca
    12. ---> 83d86de424bd
    13. Step 3/9 : RUN mkdir /usr/local/golang
    14. ---> Running in f452a13fc4bd
    15. Removing intermediate container f452a13fc4bd
    16. ---> ea257c125669
    17. Step 4/9 : ENV GOPATH /usr/local/golang
    18. ---> Running in d1e43bf99621
    19. Removing intermediate container d1e43bf99621
    20. ---> 810edc15eb13
    21. Step 5/9 : WORKDIR $GOPATH
    22. ---> Running in c38d2ea76846
    23. Removing intermediate container c38d2ea76846
    24. ---> e8462a5c6513
    25. Step 6/9 : RUN yum install -y vim
    26. ---> Running in 7d86127fff9b
    27. Loaded plugins: fastestmirror, ovl
    28. Determining fastest mirrors
    29. * base: mirrors.huaweicloud.com
    30. * extras: mirrors.huaweicloud.com
    31. * updates: mirrors.huaweicloud.com
    32. Resolving Dependencies
    33. --> Running transaction check
    34. # 此处略去安装vim的诸多显示信息
    35. vim-common.x86_64 2:7.4.629-8.el7_9
    36. vim-filesystem.x86_64 2:7.4.629-8.el7_9
    37. which.x86_64 0:2.20-7.el7
    38. Complete!
    39. Removing intermediate container 7d86127fff9b
    40. ---> d6cf1e5acb67
    41. Step 7/9 : EXPOSE 8088
    42. ---> Running in 73978947272a
    43. Removing intermediate container 73978947272a
    44. ---> d1e6f8755c5b
    45. Step 8/9 : CMD echo $GOPATH
    46. ---> Running in 20af124dd8c2
    47. Removing intermediate container 20af124dd8c2
    48. ---> 7b09507b702a
    49. Step 9/9 : CMD /bin/bash
    50. ---> Running in 5be68a75a589
    51. Removing intermediate container 5be68a75a589
    52. ---> e4d0d69335b9
    53. Successfully built e4d0d69335b9
    54. Successfully tagged my_first_centos:1.0
    55. # 查看镜像
    56. [root@k8snode docker]# docker images
    57. REPOSITORY TAG IMAGE ID CREATED SIZE
    58. my_first_centos 1.0 e4d0d69335b9 31 minutes ago 430MB
    59. centos_new 1.0 253389f04919 18 hours ago 231MB
    60. tomcat_new 1.0 261703d60799 18 hours ago 231MB
    61. mysql latest 3218b38490ce 6 months ago 516MB
    62. centos 7 eeb6ee3f44bd 9 months ago 204MB
    63. centos latest 5d0da3dc9764 9 months ago 231MB

    (3)测试

    1. #进入容器
    2. [root@k8snode docker]# docker run -it my_first_centos:1.0
    3. #显示当前路径
    4. [root@6c4da25b7587 golang]# pwd
    5. /usr/local/golang
    6. #显示环境变量值
    7. [root@6c4da25b7587 golang]# echo $GOPATH
    8. /usr/local/golang
    9. #编辑文件
    10. [root@6c4da25b7587 golang]# vim test.txt
    11. [root@6c4da25b7587 golang]# ls
    12. test.txt

    CMD与ENTRYPOINT区别

    (1-1)CMD - 编写Dockerfile

    [root@k8snode docker]# vim Dockerfile_CMD 

    内容如下: 

    FROM centos:7
    CMD ["ls","-a"]

    (1-2)CMD - 构建镜像

    1. [root@k8snode docker]# docker build -f Dockerfile_CMD -t centos_cmd:1.0 .
    2. Sending build context to Docker daemon 3.072kB
    3. Step 1/2 : FROM centos:7
    4. ---> eeb6ee3f44bd
    5. Step 2/2 : CMD ["ls","-a"]
    6. ---> Running in f3bdf1a29969
    7. Removing intermediate container f3bdf1a29969
    8. ---> 42fe762992da
    9. Successfully built 42fe762992da
    10. Successfully tagged centos_cmd:1.0

    (1-3)CMD - 测试

    以下启动容器时,在命令行后面加上-l, 可以看到执行结果报错,是因为-l覆盖了CMD命令,而-l在linux中不是命令,所以报错

    1. # 查看镜像
    2. [root@k8snode docker]# docker images
    3. REPOSITORY TAG IMAGE ID CREATED SIZE
    4. centos_cmd 1.0 42fe762992da 3 minutes ago 204MB
    5. my_first_centos 1.0 e4d0d69335b9 About an hour ago 430MB
    6. centos_new 1.0 253389f04919 18 hours ago 231MB
    7. tomcat_new 1.0 261703d60799 19 hours ago 231MB
    8. mysql latest 3218b38490ce 6 months ago 516MB
    9. centos 7 eeb6ee3f44bd 9 months ago 204MB
    10. centos latest 5d0da3dc9764 9 months ago 231MB
    11. # 启动容器,在命令行后面加上-l, 可以看到执行结果报错,是因为-l覆盖了CMD命令,而-l在linux中不是命令,所以报错
    12. [root@k8snode docker]# docker run 42fe762992da -l
    13. docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "-l": executable file not found in $PATH: unknown.
    14. ERRO[0000] error waiting for container: context canceled

    (2-1)ENTRYPOINT - 编写Dockerfile

    [root@k8snode docker]# vim Dockerfile_ENTRYPOINT

    内容如下: 

    FROM centos:7
    ENTRYPOINT ["ls","-a"]

    (2-2)ENTRYPOINT- 构建镜像

    1. [root@k8snode docker]# docker build -f Dockerfile_ENTRYPOINT -t centos_entrypoint:1.0 .
    2. Sending build context to Docker daemon 4.096kB
    3. Step 1/2 : FROM centos:7
    4. ---> eeb6ee3f44bd
    5. Step 2/2 : ENTRYPOINT ["ls","-a"]
    6. ---> Running in 731e01c14704
    7. Removing intermediate container 731e01c14704
    8. ---> 1673cb2f52ad
    9. Successfully built 1673cb2f52ad
    10. Successfully tagged centos_entrypoint:1.0

    (2-3)ENTRYPOINT- 测试

    以下启动容器时,在命令行后面加上-l, 输入结果正常显示目录下的文件列表,是因为-l被追加到CMD命令后,即执行了 ls -a -l 命令了

    1. #查看镜像
    2. [root@k8snode docker]# docker images
    3. REPOSITORY TAG IMAGE ID CREATED SIZE
    4. centos_entrypoint 1.0 1673cb2f52ad 25 seconds ago 204MB
    5. centos_cmd 1.0 42fe762992da 13 minutes ago 204MB
    6. my_first_centos 1.0 e4d0d69335b9 About an hour ago 430MB
    7. centos_new 1.0 253389f04919 19 hours ago 231MB
    8. tomcat_new 1.0 261703d60799 19 hours ago 231MB
    9. mysql latest 3218b38490ce 6 months ago 516MB
    10. centos 7 eeb6ee3f44bd 9 months ago 204MB
    11. centos latest 5d0da3dc9764 9 months ago 231MB
    12. #启动容器,命令行后加-l,输入结果
    13. [root@k8snode docker]# docker run -it 1673cb2f52ad -l
    14. total 12
    15. drwxr-xr-x 1 root root 6 Jun 19 15:40 .
    16. drwxr-xr-x 1 root root 6 Jun 19 15:40 ..
    17. -rwxr-xr-x 1 root root 0 Jun 19 15:40 .dockerenv
    18. -rw-r--r-- 1 root root 12114 Nov 13 2020 anaconda-post.log
    19. lrwxrwxrwx 1 root root 7 Nov 13 2020 bin -> usr/bin
    20. drwxr-xr-x 5 root root 360 Jun 19 15:40 dev
    21. drwxr-xr-x 1 root root 66 Jun 19 15:40 etc
    22. drwxr-xr-x 2 root root 6 Apr 11 2018 home
    23. lrwxrwxrwx 1 root root 7 Nov 13 2020 lib -> usr/lib
    24. lrwxrwxrwx 1 root root 9 Nov 13 2020 lib64 -> usr/lib64
    25. drwxr-xr-x 2 root root 6 Apr 11 2018 media
    26. drwxr-xr-x 2 root root 6 Apr 11 2018 mnt
    27. drwxr-xr-x 2 root root 6 Apr 11 2018 opt
    28. dr-xr-xr-x 122 root root 0 Jun 19 15:40 proc
    29. dr-xr-x--- 2 root root 114 Nov 13 2020 root
    30. drwxr-xr-x 11 root root 148 Nov 13 2020 run
    31. lrwxrwxrwx 1 root root 8 Nov 13 2020 sbin -> usr/sbin
    32. drwxr-xr-x 2 root root 6 Apr 11 2018 srv
    33. dr-xr-xr-x 13 root root 0 Jun 19 15:40 sys
    34. drwxrwxrwt 7 root root 132 Nov 13 2020 tmp
    35. drwxr-xr-x 13 root root 155 Nov 13 2020 usr
    36. drwxr-xr-x 18 root root 238 Nov 13 2020 var

    自定义网络

    网络模式:

    bridge: 桥接(默认)

    none: 不配置网络

    host: 与宿主机共享网络

    container: 容器网络连通

    网络帮助

     docker network --help

    1. [root@k8snode ~]# docker network --help
    2. Usage: docker network COMMAND
    3. Manage networks
    4. Commands:
    5. connect Connect a container to a network
    6. create Create a network
    7. disconnect Disconnect a container from a network
    8. inspect Display detailed information on one or more networks
    9. ls List networks
    10. prune Remove all unused networks
    11. rm Remove one or more networks
    12. Run 'docker network COMMAND --help' for more information on a command.

  • 相关阅读:
    中创技术|2022全球最常用密码名单,快看有没有你的
    接口开发-restful
    Python保留格式复制多个Excel工作表到汇总表并生成目录(附源码下载)
    类与对象(一)----什么是类和对象
    cuda和cuDNN的安装
    Pytorch 中的AverageMeter 造成内存泄漏
    快速搜索多个word、excel等文件中内容
    我已经搞了三年算法面试培训了
    MySQL |子查询
    【优化求解】基于遗传算法优化PARSEC 方法的翼型形状附matlab代码
  • 原文地址:https://blog.csdn.net/ling1998/article/details/125354089