部署

先卸载原有版本

  1. yum remove docker \
  2. docker-client \
  3. docker-client-latest \
  4. docker-common \
  5. docker-latest \
  6. docker-latest-logrotate \
  7. docker-logrotate \
  8. docker-engine

安装

  1. yum install -y yum-utils #提供yum-config-manager 实用程序
  2. yum-config-manager \
  3. --add-repo \
  4. https://download.docker.com/linux/centos/docker-ce.repo
  5. #安装新版docker和containerd
  6. yum install docker-ce docker-ce-cli containerd.io
  7. #安装指定docker和containerd版本
  8. yum list docker-ce --showduplicates | sort -r
  9. yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io #例如docker-ce-18.09.1

卸载

https://www.cnblogs.com/kingsonfu/p/11582495.html

命令

copy

正确的用法 COPY ./a.txt
错误的用法 COPY ../a.txt

  1. 1、从容器里面拷文件到宿主机?
  2. 答:在宿主机里面执行以下命令
  3. docker cp 容器名:要拷贝的文件在容器里面的路径 要拷贝到宿主机的相应路径
  4. 示例: 假设容器名为testtomcat,要从容器里面拷贝的文件路为:/usr/local/tomcat/webapps/test/js/test.js, 现在要将test.js从容器里面拷到宿主机的/opt路径下面,那么命令应该怎么写呢?
  5. 答案:在宿主机上面执行命令
  6. docker cp testtomcat:/usr/local/tomcat/webapps/test/js/test.js /opt
  7. 2、从宿主机拷文件到容器里面
  8. 答:在宿主机里面执行如下命令
  9. docker cp 要拷贝的文件路径 容器名:要拷贝到容器里面对应的路径
  10. 示例:假设容器名为testtomcat,现在要将宿主机/opt/test.js文件拷贝到容器里面的/usr/local/tomcat/webapps/test/js路径下面,那么命令该怎么写呢?
  11. 答案:在宿主机上面执行如下命令
  12. docker cp /opt/test.js testtomcat:/usr/local/tomcat/webapps/test/js
  13. 3、在这里在记录一个问题,怎么看容器名称?
  14. 执行命令:docker ps,出现如图所示,其中NAMES就是容器名了。
  15. 4、需要注意的是,不管容器有没有启动,拷贝命令都会生效。

login

登录到镜像仓库

  1. docker login --username=user -p password harbor_url

交互式登录,避免密码出现在history

基于nfs跨宿主机挂载

  1. version: "3.2" #使用前提:3.2以上
  2. networks: #顶层网络键允许您指定要创建的网络
  3. backend: #定义的网络名
  4. ipam: #指定自定义IPAM配置。 这是一个具有多个属性的对象,每个属性都是可选的
  5. config: #config:具有零个或多个配置块的列表
  6. - subnet: 172.18.0.0/16
  7. #subnet: 表示网段的CIDR格式的子网,另外还有
  8. #ip_range: 从中分配容器IPs的IP范围
  9. #gateway: 主子网的IPv4或IPv6网关
  10. #aux_addresses:网络驱动程序使用的辅助IPv4或IPv6地址,作为从主机名到IP地址的映射
  11. services:
  12. oa.fanxxx.com:
  13. nginx-lb:
  14. image: nginx-lb:v2
  15. container_name: nginx-lb
  16. ports:
  17. - "80" #设置的 network_mode(网络模式)为host,这里的80和443端口相当于宿主机的
  18. - "443"
  19. #command: /bin/bash -c "nginx"
  20. network_mode:
  21. host #与宿主机共享Network Namespace
  22. environment:
  23. - TZ=Asia/Shanghai
  24. volumes:
  25. - type: volume
  26. source: nfs-wwwlogs
  27. target: /data/wwwlogs
  28. volume:
  29. no
  30. image: nginx-php:v9
  31. container_name: oa.fanxxx.com
  32. ports:
  33. - "9000"
  34. command: /bin/bash -c "nginx && php-fpm"
  35. networks:
  36. backend: #调用上面定义的网络
  37. ipv4_address: 172.18.0.3
  38. environment:
  39. - TZ=Asia/Shanghai
  40. volumes:
  41. - type: volume #定义volumes类型,有volume、bind、tmpfs、npipe
  42. source: nfs-wwwlogs #挂载的源,主机上用于绑定挂载的路径或顶级volumes中定义的卷的名称 。不适用于tmpfs挂载。
  43. target: /data/wwwlogs #安装了卷的容器中的路径
  44. volume: #配置其他选项,如nocopy
  45. nocopy: true #创建卷时禁用从容器复制数据
  46. - type: volume #第二个挂载项
  47. source: nfs-oa.fanxxx.com-project
  48. target: /home/wwwroot/oa.fanxxx.com
  49. volume:
  50. nocopy: true
  51. volumes:
  52. nfs-wwwlogs: #定义卷名
  53. driver: local #Linux上的内置驱动程序
  54. driver_opts: #选项
  55. type: "nfs" # nfs逻辑卷
  56. o: "addr=172.16.36.49,rw,soft,nolock"
  57. #定义nfs服务端信息
  58. # NFS参数 soft 软挂载方式挂载,NFS请求超时则返回错误 hard硬加载则是一直请求到成功
  59. # NFS参数 nolock表示文件锁只对同一个主机上的应用有效,默认为lock对不同主机应用有效
  60. device: ":/data/wwwlogs" #nfs服务端提供挂载的目录
  61. nfs-oa.fanxxx.com-project:
  62. driver: local
  63. driver_opts:
  64. type: "nfs"
  65. o: "addr=172.16.36.49,rw,soft,nolock"
  66. device: ":/home/wwwroot/oa.fanxxx.com"
  67. 查看网络:
  68. [www@iZbp16lt03725esszbm7rqZ nginx_php]$ docker network ls
  69. NETWORK ID NAME DRIVER SCOPE
  70. 1c73ddcf53f8 bridge bridge local
  71. d8a2c8f95bd7 host host local
  72. 40b69529f0e0 none null local
  73. 5fe313d8841b project_backend bridge local
  74. 为什么网络的名字为project_backend ,不应该是backend吗?原因是我们的docker-compose.yml文件在project目录下

基于nfs的volume

  1. docker service create -d \
  2. --name nfs-service \
  3. --mount 'type=volume,source=nfsvolume,target=/tmp/docker/test,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/data/nfs,volume-opt=o=addr=10.0.0.8' \
  4. test/nginx:1.13
  1. version: "3.2"
  2. services:
  3. busybox1:
  4. image: test/nginx:1.13
  5. container_name: b1
  6. volumes:
  7. - type: volume
  8. source: nfs-test
  9. target: /data/nfs
  10. volume:
  11. nocopy: true
  12. volumes:
  13. nfs-test:
  14. driver: local
  15. driver_opts:
  16. type: "nfs"
  17. o: "addr=10.0.0.8,rw,soft,nolock"
  18. device: ":/tmp"
  1. docker volume create --driver local --opt type=nfs --opt o=addr=10.0.0.8,rw --opt device=:/data/nfs volume-nfs

热重启docker

热重启命令
sudo kill -SIGHUP $(pidof dockerd)

支持热更新的配置

  1. debug: it changes the daemon to debug mode when set to true.
  2. cluster-store: it reloads the discovery store with the new address.
  3. cluster-store-opts: it uses the new options to reload the discovery store.
  4. cluster-advertise: it modifies the address advertised after reloading.
  5. labels: it replaces the daemon labels with a new set of labels.
  6. live-restore: Enables keeping containers alive during daemon downtime.
  7. max-concurrent-downloads: it updates the max concurrent downloads for each pull.
  8. max-concurrent-uploads: it updates the max concurrent uploads for each push.
  9. default-runtime: it updates the runtime to be used if not is specified at container creation. It defaults to default which is the runtime shipped with the official docker packages.
  10. runtimes: it updates the list of available OCI runtimes that can be used to run containers.
  11. authorization-plugin: it specifies the authorization plugins to use.
  12. allow-nondistributable-artifacts: Replaces the set of registries to which the daemon will push nondistributable artifacts with a new set of registries.
  13. insecure-registries: it replaces the daemon insecure registries with a new set of insecure registries. If some existing insecure registries in daemons configuration are not in newly reloaded insecure resgitries, these existing ones will be removed from daemons config.
  14. registry-mirrors: it replaces the daemon registry mirrors with a new set of registry mirrors. If some existing registry mirrors in daemons configuration are not in newly reloaded registry mirrors, these existing ones will be removed from daemons config.
  15. shutdown-timeout: it replaces the daemons existing configuration timeout with a new timeout for shutting down all containers.
  16. features: it explicitly enables or disables specific features.

更改docker数据目录

  1. # 修改daemon.josn,增加"data-root": "/home/container/docker/"
  2. vim /etc/docker/daemon.josn
  3. {"registry-mirrors": [
  4. "https://a5ezgmcg.mirror.aliyuncs.com",
  5. "http://f1361db2.m.daocloud.io",
  6. "https://dockerhub.azk8s.cn",
  7. "https://reg-mirror.qiniu.com"
  8. ],
  9. "exec-opts": ["native.cgroupdriver=systemd"],
  10. "log-driver": "json-file",
  11. "log-opts": {
  12. "max-size": "100m"
  13. },
  14. "insecure-registries": ["10.1.130.100:9010"],
  15. "live-restore": true,
  16. "data-root": "/home/container/docker/"
  17. }
  18. # 重启docker
  19. systemctl restart docker

如果仅仅是更改docker数据目录:
vim /etc/docker/daemon.josn
{
“data-root”: “/home/container/docker/“
}

查看未知docker容器跑的是啥

  1. # 查看未知容器部署方式
  2. docker inspect 容器id # 查看详细信息,可以看到是使用docker-composer起的
  3. "com.docker.compose.container-number": "1",
  4. "com.docker.compose.oneoff": "False",
  5. "com.docker.compose.project": "apisvr",
  6. "com.docker.compose.project.config_files": "/pgyvate/data/apisvr/docker-compose.yml",
  7. "com.docker.compose.project.working_dir": "/pgyvate/data/apisvr",
  8. "com.docker.compose.service": "pgyapisvr",
  9. "com.docker.compose.version": "1.26.2",
  10. cat /pgyvate/data/apisvr/docker-compose.yml # 得知证书是通过变量传进去的,注意这里的证书是base64编码后的
  11. # 进入容器后,ps -ef 查看程序的启动命令
  12. [root@iZbp1e3t4tj14gcl5jntv0Z ~]# docker exec -it 728f9fb7a213 bash
  13. bash-5.0# ps -ef
  14. PID USER TIME COMMAND
  15. 1 root 0:00 {entrypoint-star} /bin/bash /sbin/entrypoint-start.sh
  16. 32 root 0:01 /usr/local/bin/pgyapisvr -c /etc/pgyapisvr/config.ini
  17. 50 root 0:00 /bin/sh
  18. 56 root 0:00 bash
  19. 61 root 0:00 ps -ef
  20. bash-5.0# cat /etc/pgyapisvr/config.ini
  21. [server]
  22. identityhost = ""
  23. sslcert = "/usr/local/pgyapisvr/ssl.crt"
  24. sslkey = "/usr/local/pgyapisvr/ssl.key"
  25. bash-5.0# cat /usr/local/pgyapisvr/config.ini.dist # 这个文件?
  26. [server]
  27. identityhost = "{PAS_ADDRESS}"
  28. sslcert = "{HTTPS_CRT}" # 传进来的变量
  29. sslkey = "{HTTPS_KEY}"
  30. 实际上config.ini.dist这个文件的内容是通过1号进程处理(/sbin/entrypoint-start.sh),最终配置写入 config.ini
  31. # 根据docker镜像,反推dockerfile
  32. docker history --format {{.CreatedBy}} --no-trunc=true 镜像id |sed "s?/bin/sh\ -c\ \#(nop)\ ??g"|sed "s?/bin/sh\ -c?RUN?g" | tac
  33. 参考https://blog.csdn.net/yuanshangshenghuo/article/details/106896963
  34. # docker-compose更新image
  35. docker-compose -f docker-compose.yaml stop
  36. docker-compose -f docker-compose.yaml up -d --build