余子越的博客
Toggle navigation
余子越的博客
主页
计算机网络
大数据分析
系统与工具
编程之路
容器引擎
作者
归档
标签
Docker管理工具docker-compose总结
Docker
2019-12-18 10:20:05
29
0
0
yuziyue
Docker
# 一. 安装docker-compose方法 `docker-compose`是一个可执行的二进制文件,所以只需要放到系统路径下即可。 ``` sudo curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose ``` # 二. 命令docker-compose详解 ## 1. docker-compose 命令帮助 ``` Define and run multi-container applications with Docker. Usage: docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...] docker-compose -h|--help Options: -f, --file FILE Specify an alternate compose file (default: docker-compose.yml) -p, --project-name NAME Specify an alternate project name (default: directory name) --verbose Show more output --log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL) --no-ansi Do not print ANSI control characters -v, --version Print version and exit -H, --host HOST Daemon socket to connect to --tls Use TLS; implied by --tlsverify --tlscacert CA_PATH Trust certs signed only by this CA --tlscert CLIENT_CERT_PATH Path to TLS certificate file --tlskey TLS_KEY_PATH Path to TLS key file --tlsverify Use TLS and verify the remote --skip-hostname-check Don't check the daemon's hostname against the name specified in the client certificate --project-directory PATH Specify an alternate working directory (default: the path of the Compose file) --compatibility If set, Compose will attempt to convert keys in v3 files to their non-Swarm equivalent --env-file PATH Specify an alternate environment file Commands: build Build or rebuild services bundle Generate a Docker bundle from the Compose file config Validate and view the Compose file create Create services down Stop and remove containers, networks, images, and volumes events Receive real time events from containers exec Execute a command in a running container help Get help on a command images List images kill Kill containers logs View output from containers pause Pause services port Print the public port for a port binding ps List containers pull Pull service images push Push service images restart Restart services rm Remove stopped containers run Run a one-off command scale Set number of containers for a service start Start services stop Stop services top Display the running processes unpause Unpause services up Create and start containers version Show the Docker-Compose version information ``` ## 2. docker-compose 常用命令 ``` $ 首次运行容器,相当于docker run命令。 $ -f docker-compose.yml 指定配置文件路径,不指定会在当前目录查找。 $ -d 表示后台运行,不指定在前台运行,Ctrl+c停止容器。 docker-compose up -d $ 查看当前运行的容器 docker-compose ps $ 查看当前运行的容器所有service docker-compose ps --services $ 进入指定服务的容器(使用docker-compose ps --services查看service) docker-compose exec nginx /bin/bash $ 启动 停止 重启 删除 容器 docker-compose start|stop|restart|rm $ 查看当前项目的日志,相当于tail -f,查看所有的日志文件,然后fllow。 docker-compose logs -f $ 查看当前项目的日志,相当于tail -f,每个日志文件只查看最后50行,然后fllow。 docker-compose logs -f --tail="50" ``` <br> # 三. 文件docker-compose.yml详解 - image 指定镜像 指定使用的镜像。同Dockerfile里面的FROM 如果此镜象不存在 会自己尝试拉去此镜象 `image: centos:7.6.1810` `image: nginx` <br> - build 使用Dockerfile构建 服务除了可以基于指定的镜像,还可以指定目录下的Dockerfile构建。可以绝对路径和相对路径 相对路径 `build: ./datadir` 绝对路径 `build: /path/to/datadir` 如果想要指定目录下的Dockerfile `build:` ` context: /data/soft/redis` ` dockerfile: Dockerfile` <br> - privileged 使用特权模式 `privileged: true`。使用该参数,container内的root拥有真正的root权限。 <br> - environment 指定环境变量。 ``` environment: - REDIS_CONF=on - REQUIREPASSWD=hanye131 - MAXCLIENTS_NUM=6000 - MAXMEMORY_SIZE=4096 ``` 这样在redis.conf里面可以调用此变量了,如果使用现成的镜像,通常这些环境变量已经在镜像里面制作好了。 ``` maxclients ${MAXCLIENTS_NUM} maxmemory ${MAXMEMORY_SIZE}M requirepass "${REQUIREPASSWD}" ``` <br> - container_name 指定构建后的容器名字。 通常情况下可以不指定,不指定时,容器名按照docker-com.yml所在目录名称+服务名称+序号来命名,比如:redash_nginx_1。 `container_name: redis` <br> - depends_on 指定构建容器的启动顺序。 比如下面的例子表示:先启动mysql、nginx,然后再启动redis。 ``` version: "3" services: redis: image: redis:latest depends_on: - mysql - nginx container_name: redis mysql: image: mysql:5.7 container_name: mysql57 nginx: image: nginx:1.17.2 container_name: nginx1172 ``` <br> - ports 映射端口。 使用格式为(主机端口:容器端口),或者只是指定容器的端口,此时宿主机会随机映射端口。 ``` ports: - "8080:80" - "3309:3306" - "192.168.1.39:6379:6379" # 宿主机绑定指定IP和端口 - "3000-3005" # 暴漏容器3000-3005端口,宿主机随机端口 $ v3.2新增参数 ports: - target: 80 # 容器端口 published: 8080 # 宿主机端口 protocol: tcp # 协议类型 mode: host # host表示在每个节点上发布主机端口, ingress表示对于群模式端口进行负载均衡 ``` <br> - links 容器之间的连接。 与Docker client的--link一样效果,会连接到其它容器中的服务。下面的例子表示:再nginx容器里面可以直接使用 redash 名称即可连接到redash容器,redash名称会解析成成redash容器的IP地址。 ``` nginx: links: - mysql:redash ``` <br> - extra_hosts 写入数据到/ect/hosts文件。 ``` extra_hosts: - "hanye.com:192.168.1.39" - "redis.conf:192.168.1.40" ``` <br> - dns 指定dns ``` dns: 8.8.8.8 dns: - 8.8.8.8 - 114.114.114 ``` <br> - volumes 指定挂在目录 ``` volumes: - "/etc/redis.conf:/etc/redis.conf:rw" - "/etc/localtime:/etc/localtime:ro" - "/usr/local/redis/var:/data/redis" ``` <br> - volumes_from 指定挂在容器的目录 ``` volumes_from: - nginx ``` <br> - sysctls 指定开启内核参数 相当于直接修改/etc/sysctl.conf ``` sysctls: net.core.somaxconn: "1024" fs.file-max: "1000000" ``` <br> - ulimits 设置容器打开文件线程数量 ``` ulimits: nproc: 65535 nofile: soft: 20000 hard: 40000 ``` <br> - cap_add 增加容器可以使用那些功能 ``` cap_add: - ALL ``` <br> - cap_dorp 删除容器可以使用的内核功能 ``` cap_drop: - NET_ADMIN ``` <br> - restart 容器出现问题是否重启 no时,是默认的重启策略,在任何情况下都不会重启容器。 always时,容器总是重新启动。 on-failure时,如果退出代码指示出现故障错误,将重新启动容器。 unless-stopped,除非被动停止,否则任何情况都会重新启动容器。 ``` restart: no restart: always restart: on-failure restart: unless-stopped ``` <br> - hostname 指定容器的主机名 ``` hostname: redis ``` <br> - logging 配置日志 ``` logging: driver: "json-file" options: max-size: "20m" # 单个文件大小20m max-file: "10" # 文件保留10个 ``` <br> - deploy 设置资源限制 ``` version: "3" services: redis: image: redis:alpine deploy: resources: limits: # 设置容器的资源限制 cpus: '0.50' # 设置该容器最多只能使用 50% 的 CPU memory: '50M' #设置该容器最多只能使用 50M 的内存空间 reservations: # 设置为容器预留的系统资源(随时可用) cpus: '0.25' # 为该容器保留 20% 的 CPU memory: '20M' # 为该容器保留 20M 的内存空间 ``` <br> - command 使用 command 可以覆盖容器启动后默认执行的命令。 ``` command: bundle exec thin -p 3000 ``` <br> - network 将容器加入指定网络 将容器加入指定网络(等同于 docker network connect 的作用), networks可以位于compose文件顶级键和services键的二级键。 ``` version: "3" services: nginx: image: nginx:latest ports: - "9090:80" networks: flownet: ipv4_address: 172.3.0.10 redis: image: redis:latest restart: always ports: - "6300:6379" networks: flownet: ipv4_address: 172.3.0.11 networks: flownet: ipam: driver: default config: - subnet: 172.3.0.0/24 ``` <br> - 参考资料 [docker-compose简单命令](https://blog.51cto.com/9025736/2431738) <br><br><br>
上一篇:
maven快速入门
下一篇:
Kafka快速上手
0
赞
29 人读过
新浪微博
微信
腾讯微博
QQ空间
人人网
文档导航