你还在使用docker run -v /opt:/opt -it centos /bin/bash这样运行容器吗?你还在使用docker stop停止容器服务器?你还在使用docker rm 容器服务吗?这样运行容器服务也没有什么问题,但是如果参数过多或者很多变量的时候就容器出错或者遗漏,有些时候也记不住超长的一串命令,为什么不试试简单好用的docker-compose运行和编排容器服务呢?下面我们简单介绍一下使用方便易用的docker-compose(以下简单称dc)来运行容器服务。 什么是docker-compose?docker-Compose项目是Docker官方的开源项目,负责实现对Docker容器单机运行的容器服务的配置快速编排(或者称运行模板),dc可以很方便的管理容器服务的运行、停止、移除、查看等等功能,dc可以替代大部分的docker命令。 如果文章对你的帮助,欢迎关注和点赞,我们后续还会有更多关于docker的内容。 第一步:dc的安装,这里版本是1.29.2 curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/bin/dc 第二步:赋予运行权限 chmod +x /usr/bin/dc 第三步:编排服务的yml文件,我们以redis 5.0.8说明 cat docker-compose.yml 单机运行redis 5.0.8 yml配置 第四步:配置redis.conf配置文件 附:本文最后面 第五步:创建redis相关目录和目录属主 mkdir redis-6381 cd redis-6381 && mkdir conf data log pid chown -R 999:999 data log pid 整个redis目录结构 第四步:下载官方redis 5.0.8镜像 docker pull redis:5.0.8 下载官方redis 5.0.8镜像 第五步:后台运行redis 5.0.8服务 dc up -d -d后台运行redis 5.0.8服务 第六步:将redis-cli命令复制到宿主机 docker cp redis:/usr/local/bin /usr 第七步:测试redis是否运行成功 redis-cli -h 127.0.0.1 -p 6381 -a his123 redis-cli能登陆,查看info,运行成功 经过上面几步如果在使用过程中redis运行不起来,可以使用以下两种方法来错误日志: dc logs redis tail -f redis-6381/log/redis.log 提示没有权限写入log目录,需要上面第五步chown一下 其它相关命令: 停止redis容器服务 dc stop 移除redis容器服务 dc rm -f 查看redis运行log 查看dc版本 ./dc --version docker-compose version 1.29.2, build 5becea4c 查看dc帮助 dc --help 查看编排的配置 dc config 进入容器内部 dc exec redis /bin/bash 附:redis.conf配置 bind 0.0.0.0 protected-mode no port 6381 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile /opt/redis/redis-6381/pid/redis.pid logfile /opt/redis/redis-6381/log/redis.log dir /opt/redis/redis-6381/data loglevel notice databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no replica-priority 100 requirepass his123 masterauth his123 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no appendonly yes appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble yes lua-time-limit 5000 cluster-enabled no cluster-config-file nodes.conf cluster-node-timeout 5000 slowlog-log-slower-than 10000 cluster-announce-ip 172.18.95.158 cluster-announce-port 6381 cluster-announce-bus-port 16381 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes 如果文章对你的帮助,欢迎关注和点赞,我们后续还会有更多关于docker的内容。