Docker零碎笔记

  1. 云栖社区>
  2. 博客>
  3. 正文

Docker零碎笔记

paulandcode 2019-06-08 10:42:33 浏览519
展开阅读全文

001.查看镜像、容器、数据卷所占用的空间

docker system df

002.具体查看镜像内的历史记录

docker history nginx:v2

003.使用Dockerfile构建镜像(注意后面有个点)

docker build -t myip .

004.使用compose对docker容器进行编排管理时,需要编写docker-compose.yml文件需要注意缩进

yml文件换行后的缩进,不允许使用tab键字符,只能使用空格,而空格的数量要一致

005.docker-compose

负责实现对 Docker 容器集群的快速编排

006.docker-machine

负责在多种平台上快速安装docker环境

007.swarm mode

负责将多个docker主机封装为单个大型的虚拟dockerd主机,快速打造一套容器云平台

008.安装virtualbox

sudo apt-get purge virtualbox
sudo apt-get
install linux-generic
linux-image-generic
linux-headers-generic
linux-signed-generic
sudo apt-get
install virtualbox

009.docker镜像加速器下载速度对比

加速器 地址 Httpd,177MB下载镜像耗时 MySQL,407MB下载镜像耗时 Nginx,109MB下载镜像耗时
官方 https://registry.docker-cn.com 52 s 42 s 20 s
阿里 https://2lqq34jg.mirror.aliyuncs.com 36 s 38 s 20 s
网易 http://hub-mirror.c.163.com 67 s 73 s 475 s
官方错误 http://registry.docker-cn.com 1770 s 不忍测试 闲时再测
不使用 --- 一个字慢! 不忍测试 闲时再测

结论:
1、使用加速器后,下载速度飞快,家庭100Mb宽带环境下,镜像下载速度能达到3-5MB/s以上。
2、由于Docker采用分层文件系统,不同镜像公用一些相同的层文件,在下载后续镜像时下载速度更快,比如阿里加速器下载MySQL镜像,“达到”了10.7MB/s;
3、加速器地址一定要写对,官方加速器的不能写成http,一定要写https,否则无加速效果;
4、加速器的加速效果差不多?官方的、阿里的、网易163的加速器效果如下:阿里 >官方 > 网易 > 不加速
5、Docker支持同时配置多个加速器,建议依上面的次序同时配置多个加速器。
6、代码:

{
 "registry-mirrors": [
  "https://2lqq34jg.mirror.aliyuncs.com",
  "https://registry.docker-cn.com",
  "http://hub-mirror.c.163.com"
 ]
}

010.docker的run命令

docker run -it -v /usr/java/:/mnt/software/ myubuntu /bin/bash
docker run <相关参数> <镜像ID 或者 仓库名:标签名> <初始命令>
相关参数包括:  
    -i:表示以“交互模式”运行容器  
    -t:表示容器启动后会进入其命令行  
    -v:表示需要将本地哪个目录挂载到容器中,格式:-v <宿主机目录>:<容器目录>
初始命令表示一旦容器启动,需要运行的命令,若此处命令使用"/bin/bash",则表示什么也不做,只需进入命令行即可
退出容器使用Ctrl+d或输入exit
再次启动已启动过但现在是关闭的容器
docker start 容器ID或容器名
这个命令只会启动容器,不会进入容器。要进入容器得用命令
docker attach 容器ID或容器名

011.docker如何查看已存在的容器所挂载的目录

docker inspect container_name | grep Mounts -A 20

012.docker启动mysql容器

初次启动需要指定root用户密码, 也可在docker-compose中指定, 直接用docker-compose启动

docker run -e MYSQL_ROOT_PASSWORD=root -p 33066:3306 --name mysql-5.7 -d mysql:5.7 -v /data/mysql/my.cnf:/etc/mysql/mysql.conf.d -v /data/mysql/logs:/usr/local/mysql/logs -v /data/mysql/data:/var/lib/mysql

013.docker进入mysql容器

docker exec -it mysql-5.7 bash
# 然后进入mysql服务
mysql -uroot -p

014.docker启动tomcat

docker run --mount source=tomcat-8.5-xyj,target=/usr/local/tomcat/webapps -p 8081:8080 --name tomcat-8.5 -d tomcat:8.5

015.DockerCompose通过compose工程名以及服务名从dockerEngine获取当前所有含有此标记的容器以检查当前工程所包含的服务状态,根据当前状态为每个服务制定接下来的动作

  1. 若容器不存在,则服务动作设置为创建(create)
  2. 若容器存在但设置不允许重建,则服务动作设置为启动(start)
  3. 若容器配置发生变化(config-hash)或者设置强制重建标志,则服务动作设置为重建(recreate)
  4. 若容器状态为停止,则服务动作设置为启动(start)
  5. 若容器状态为运行但其依赖容器需要重建,则服务状态设置为重建(recreate)
  6. 若容器状态为运行其无配置改变则不作操作

016.docker查看某个容器大小

进入docker容器默认位置:/var/lib/docker/containers, 文件夹名称即为容器名,文件夹大小即为容器大小

17. Docker参考网站

Docker Hub官方网站(用于查找各种镜像):
https://hub.docker.com/
docker-compose.yml文件各个参数的官方说明:
https://docs.docker.com/compose/compose-file/

18. Docker的各种docker-compose.yml文件

(1) MySQL

version: "3.5"

services:
 mysql:
  image: mysql:5.7
  restart: always
  container_name: mysql
  environment:
   MYSQL_ROOT_PASSWORD: root
  ports:
   - "3306:3306"
  networks:
   mysql:
    ipv4_address: 172.20.0.2
  volumes:
   - type: volume
     source: data
     target: /var/lib/mysql
   - type: volume
     source: config
     target: /etc/mysql/mysql.conf.d
   - type: volume
     source: logs
     target: /usr/local/mysql/logs

volumes:
 data:
 config:
 logs:

networks:
 mysql:
  driver: bridge
  name: mysql
  ipam:
   driver: default
   config:
   -
    subnet: 172.20.0.0/24

(2) Redis

version: "3.5"

services:
 redis:
  image: redis:4.0.9
  restart: always
  container_name: redis
  volumes:
   - type: volume
     source: config
     target: /usr/local/etc/redis
   - type: volume
     source: data
     target: /data
  command: redis-server /usr/local/etc/redis/redis.conf
  ports:
   - "6379:6379"
  networks:
   redis:
    ipv4_address: 172.20.0.2

volumes:
 config:
 data:

networks:
 redis:
  driver: bridge
  name: redis
  ipam:
   driver: default
   config:
   -
    subnet: 172.20.0.0/24

(3) Tomcat

version: "3.5"

services:
 blog:
  image: tomcat:8.5
  restart: always
  container_name: blog
  ports:
   - "8083:8080"
  networks:
   - mysql
  volumes:
   - type: volume
     source: blog
     target: /usr/local/tomcat/webapps

volumes:
 blog:
  name: blog

networks:
  mysql:
   external: true
   name: mysql

(4) Java示例(执行了java -jar /opt/blogback.jar命令, 方便启动容器后自动运行Java程序, 也可用于SpringBoot项目的部署)

version: "3.5"

services:
 blogback:
  image: java:8
  restart: always
  container_name: blogback
  ports:
   - "8082:8080"
  working_dir: /opt
  command: java -jar /opt/blogback.jar
  networks:
   - mysql
  volumes:
   - type: volume
     source: blogback
     target: /opt

volumes:
 blogback:
  name: blogback

networks:
 mysql:
  external: true
  name: mysql

(5) GitLab

version: "3.5"

services:
 gitlab:
  image: gitlab/gitlab-ce:10.5.4-ce.0
  restart: always
  container_name: gitlab
  ports:
   - "8085:80"
   - "2222:22"
  hostname: "gitlab.ywz.com"
  environment:
   GITLAB_OMNIBUS_CONFIG: |
    gitlab_rails['gitlab_shell_ssh_port'] = 2222
  volumes:
   - type: volume
     source: config
     target: /etc/gitlab
   - type: volume
     source: logs
     target: /var/log/gitlab
   - type: volume
     source: data
     target: /var/opt/gitlab

volumes:
 config:
  name: gitlab_config
 logs:
  name: gitlab_logs
 data:
  name: gitlab_data

(6) Jenkins

version: "3.5"

services:
 jenkins:
  image: jenkins:2.60.3
  restart: always
  container_name: jenkins
  ports:
   - "8080:8080"
  volumes:
   - type: bind
     source: /home/paul/docker/myvolume/jenkins/home
     target: /var/jenkins_home

(7) Nexus

version: "3.5"

services:
 nexus:
  image: sonatype/nexus3:3.8.0
  restart: always
  container_name: nexus
  ports:
   - "8081:8081"
  volumes:
   - type: volume
     source: data
     target: /nexus-data

volumes:
 data:

(8) Nginx

version: "3.5"

services:
 nginx:
  image: nginx:1.13
  restart: always
  container_name: nginx
  ports:
   - "80:80"
  volumes:
   - type: volume
     source: config
     target: /etc/nginx

volumes:
 config:

(9) Python

version: "3.5"

services:
 python:
  image: python:3.5
  restart: always
  container_name: python
  tty: true
  command: bash
  volumes:
   - type: volume
     source: workspace
     target: /home
   - type: volume
     source: lib
     target: /usr/local/lib

volumes:
 workspace:
  name: python_workspace
 lib:
  name: python_lib

(10) 多个Tomcat项目共用MySQL, Redis示例

version: "3.5"

services:
 jn:
  image: tomcat:8.5
  restart: always
  container_name: tomcat_oa_jn
  ports:
   - "8082:8080"
  networks:
   - mysql_oa
   - redis_oa
  volumes:
   - type: bind
     source: /home/paul/docker/myvolume/tomcat_oa_jn/webapps
     target: /usr/local/tomcat/webapps
  depends_on:
   - mysql_oa
   - redis_oa

 xq:
  image: tomcat:8.5
  restart: always
  container_name: tomcat_oa_xq
  ports:
   - "8083:8080"
  networks:
   - mysql_oa
   - redis_oa
  volumes:
   - type: bind
     source: /home/paul/docker/myvolume/tomcat_oa_xq/webapps
     target: /usr/local/tomcat/webapps
  depends_on:
   - mysql_oa
   - redis_oa

 test:
  image: tomcat:8.5
  restart: always
  container_name: tomcat_oa_test
  ports:
   - "8084:8080"
  networks:
   - mysql_oa
   - redis_oa
  volumes:
   - type: bind
     source: /home/paul/docker/myvolume/tomcat_oa_test/webapps
     target: /usr/local/tomcat/webapps
  depends_on:
   - mysql_oa
   - redis_oa

 mysql_oa:
  image: mysql:5.7
  restart: always
  container_name: mysql_oa
  environment:
   MYSQL_ROOT_PASSWORD: root
  ports:
   - "3307:3306"
  networks:
   mysql_oa:
    ipv4_address: 172.20.0.2
  volumes:
   - type: volume
     source: mysql_data
     target: /var/lib/mysql
   - type: volume
     source: mysql_config
     target: /etc/mysql/mysql.conf.d
   - type: volume
     source: mysql_logs
     target: /usr/local/mysql/logs

 redis_oa:
  image: redis:4.0.9
  restart: always
  container_name: redis_oa
  command: redis-server /usr/local/etc/redis/redis.conf
  ports:
   - "6379:6379"
  networks:
   redis:
    ipv4_address: 172.21.0.2
  volumes:
   - type: volume
     source: redis_config
     target: /usr/local/etc/redis
   - type: volume
     source: redis_data
     target: /data

volumes:
 mysql_data:
  name: mysql_oa_data
 mysql_config:
  name: mysql_oa_config
 mysql_logs:
  name: mysql_oa_logs
 redis_config:
  name: redis_oa_config
 redis_data:
  name: redis_oa_data

networks:
  mysql_oa:
   driver: bridge
   name: mysql_oa
   ipam:
    driver: default
    config:
    -
     subnet: 172.20.0.0/24
  redis_oa:
   driver: bridge
   name: redis_oa
   ipam:
    driver: default
    config:
    -
     subnet: 172.21.0.0/24

19. Dockerfile实现在centos7-jdk8容器中安装应用(以libreoffice为例)

(1) Dockerfile文件

FROM bpatterson/centos7-jdk8

RUN yum update -y

RUN yum install -y libreoffice-headless libreoffice-writer libreoffice-calc libreoffice- libreoffice-impress libreoffice-langpack-zh-Hans libreoffice-langpack-zh-Hant libreoffice-langpack-ja ImageMagick curl

RUN yum clean all

WORKDIR /

(2) 在Dockerfile文件所在目录下执行如下命令生成镜像

docker build -t libreoffice:1.0 .

(3) 镜像生成后就可以通过compose文件或者直接使用命令启动容器了

version: "3.3"

services:
 libreoffice:
  image: libreoffice:1.0
  restart: always
  tty: true
  command: /bin/bash
  container_name: libreoffice
  ports:
   - "8192:8080"
  volumes:
   - type: bind
     source: /home/libreoffice/data
     target: /home/libreoffice/data

(4) 启动容器后运行容器内应用

docker exec -t libreoffice libreoffice --headless --convert-to pdf /home/libreoffice/data/source/test.xls --outdir /home/libreoffice/data/target

(5) libreoffice功能
实现附件格式转换, 如: xls文件转换为doc文件, doc文件转换为pdf文件等等.

网友评论

登录后评论
0/500
评论
paulandcode
+ 关注