docker-compose安装开发环境
什么是docker-compose?
docker-compose是docker的容器编排⼯具。通过配置yml⽂件来compose 我们开发过程中所需要的服务。
ps: 虽然⽣产上⼤部分⽤的是k8s,但是我们⾃⼰开发的时候,可以⽤docker-compose 来快速的起服务,⽤什么起什么,极其⽅便。(⾄于安装,在这⾥不做过多的讲解)
先看下实例
version: '3' #基于compose哪个版本定制,1已经废弃,⽬前2和3
services:
mysql:
container_name: mysql_dev # 给容器起个名
image: "mysql:5.7"
# hub.docker/_/mysql?tab=description
environment: # 设置环境变量,相当于docker run命令中的-e,boolean类型得⽤引号引起来
TZ: Asia/Shanghai
LANG: en_US.UTF-8
MYSQL_ROOT_PASSWORD: "123456" # root密码
# MYSQL_USER: 'yxkong' # 新增超级⽤户(直接访问MYSQL_DATABASE)
# MYSQL_PASS: '123456' # 新增超级⽤户的密码
MYSQL_DATABASE: demo # 初始化的数据库名称
restart: always # 容器退出以后的重启策略
ports: # 端⼝映射
-
3306:3306
volumes: # 数据卷挂载路径设置,将本机⽬录映射到容器⽬录
- "/Users/yxk/docker/mysql/myf:/etc/mysql/myf" # 本机的mysql配置⽂件
- "/Users/yxk/docker/mysql/data:/var/lib/mysql" # 数据⽬录挂载
- "/Users/yxk/docker/mysql/log/error.log:/var/log/mysql/error.log" # 异常⽇志输出
- "/Users/yxk/docker/mysql/init:/docker-entrypoint-initdb.d/" # 初始sql ⽀持⼏种格式
执⾏
sudo docker-compose -l up -d
-f:指定l⽂件路径
-d:后台启动
常见配置说明
container_name:⾃定义容器名称
cgroup_parent:为容器指定⽗的cgroup组,同时也继承了该cgroup的资源限制。
environment: 设置环境变量,相当于docker run命令中的-e,boolean类型的⽤引号引起来
command:覆盖容器启动的默认命令
depends_on:设置依赖关系,启动时:先启动依赖项,停⽌时:按依赖项倒序停⽌
deploy:部署策略(开发环境没必要关注,可以⽤来限制cpu和内存)
expose:暴露端⼝,但不映射到宿主机,只被连接的服务访问,可以范围
logging:服务的⽇志配置
restart:默认no,任何情况都不会重启;always,总是重启;on-failure,⾮正常退出时重启;unless-stopped 重视重启(排除docker守护进程启动时就停⽌的)
ulimits:设置ulimit
volumes:将本机⽬录或⽂件映射到容器
services:
redis:
container_name: redis_demo
image: redis
command: redis-server /etc/f --requirepass 123456 --appendonly yes
environment:
TZ: Asia/Shanghai
LANG: en_US.UTF-8
volumes:
- "/Users/yxk/docker/redis/data:/data"
- "/Users/yxk/docker/f:/etc/f"
ports:
- 6380:6379
deploy:
mode:replicated # replicated:复制服务,复制指定服务到集的机器上;global:全局服务,服务将部署⾄集的每个节点。 replicas: 2 # replicated模式下的节点数量
labels:
description: "This redis service label"
resources: #配置服务可使⽤的资源限制
limits:
cpus: '0.50'
memory: 200M
reservations:
cpus: '0.25'
memory: 200M
restart_policy: # 重启策略
condition: on-failure # 可选 none、on-failure、any(默认:any)
delay: 2s # 延迟5秒重启,默认0
max_attempts: 2 # 最⼤重试测试,默认⼀直重试
window: 60s # 重启超时时间
rollback_config: # 回滚策略
parallelism: 1 # ⼀次回滚的容器,0,⼀下⼦全回滚
delay: 5s # 每个容器回滚的等待时间
failure_action: pause # 回滚失败pause,还有⼀个是continuedocker进入容器
monitor: 10s # 更新后观察10s是否有异常
max_failure_ratio: 0 # 容忍的故障率
order: stop-first # 操作顺序 stop-first 串⾏回滚,start-first 并⾏回滚,默认stop-first
logging:
driver: json-file #json-file、syslog、none
options:
#syslog-address: "tcp://127.0.0.1:22" 使⽤syslog时,可以指定
max-size: "200k" # 单个⽂件⼤⼩为200k
max-file: "10" # 最多10个⽂件
ulimits:
nproc: 65535
nofile:
soft: 10240
hard: 10240
我们来看下docker-compose 命令
docker-compose [-f <arg>...] [--profile <name>...] [options] [--] [COMMAND] []
docker-compose -h|--help
Options:
-f, --file FILE Specify an alternate compose file # 指定compose的yml⽂件
(default: l)
-p, --project-name NAME Specify an alternate project name # 项⽬名称
(default: directory name)
-
-profile NAME Specify a profile to enable
-c, --context NAME Specify a context name
--verbose Show more output
--log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
--ansi (never|always|auto) Control when to print ANSI control characters
--no-ansi Do not print ANSI control characters (DEPRECATED)
-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 (DEPRECATED)
--env-file PATH Specify an alternate environment file
Commands:
build Build or rebuild services #构建镜像 docker-compose build --no-cache ⽆缓存构建
config Validate and view the Compose file #校验⽂件格式是否正确 docker-compose -l config create Create services # 创建容器
down Stop and remove resources # 停⽌服务
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 # 查看容器的log
pause Pause services #
port Print the public port for a port binding
ps List containers # 快速查看compose⽂件中的容器
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 # 创建并启动容器 docker-compose -l up -d
version Show version information and quit
可以通过docker-compose 命令 --help查看具体的命令
yxkdeMacBook-Pro:doc yxk$ docker-compose ps --help
List containers.
Usage: ps [options] [--] []
Options:
-q, --quiet Only display IDs
--services Display services
--filter KEY=VAL Filter services by a property
-a, --all Show all stopped containers (including those created by the run command)
安装常⽤的开发⼯具
包括:kafka、redis、eureka、mysql、nacos 等
version: '3'
services:
zookeeper:
container_name: zookeeper_dev
image: zookeeper:3.5.5
restart: always
hostname: zookeeper
ports:
- 2181:2181
volumes:
- "/etc/localtime:/etc/localtime"
- "/Users/yxk/docker/zookeeper/data:/data"
- "/Users/yxk/docker/zookeeper/log:/var/log/zookeeper"
kafka:
container_name: kafka_dev
image: wurstmeister/kafka:2.12-2.2.1
hostname: kafka
# 配置参数见:hub.docker/r/wurstmeister/kafka
restart: always
ports:
-
9092:9092
environment:
KAFKA_BROKER_ID: 1
# KAFKA_ADVERTISED_HOST_NAME: kafka
KAFKA_ADVERTISED_PORT: 9092 # 端⼝
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://10.203.8.28:9092 #配置宿主机ip或容器和宿主机都能识别的ip ,这个配置是kafka发布到zk上的,供外部使⽤,默认使⽤容器的id,容器id和容器的ip会在hosts⾥做映射
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092 # 全⽹监听,定义kafka的服务监听
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_CREATE_TOPICS: "zipkin:1:1,zipkin.log.info:1:1,zipkin.log.warn:1:1,:1:1"
KAFKA_MESSAGE_MAX_BYTES: 6000000
KAFKA_REPLICA_FETCH_MAX_BYTES: 6000000
KAFKA_GROUP_MAX_SESSION_TIMEOUT_MS: 60000
KAFKA_NUM_PARTITIONS: 2
volumes:
- "/etc/localtime:/etc/localtime"
# - "/Users/yxk/docker/kafka/docker.sock:/var/run/docker.sock"
# - "/Users/yxk/docker/kafka/data:/kafka" # kafka数据
depends_on:
- zookeeper
kafka-manager:
container_name: kafka_manager_dev
image: sheepkiller/kafka-manager
restart: always
links:
- zookeeper
- kafka
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论