在docker container中运行docker-compose

时间:2024-01-04 13:41:26

为了保持宿主主机的环境干净,因此将docker-compose安装到一个基于centos7.3的容器之中,因为tianchao屏蔽了amazonaws,最后选择了通过pip方式来安装,这也是官方推荐的一种安装方式。https://pip.pypa.io/en/stable/installing/

第一步:

安装CentOS7.3的image创建container并进入container之中,因为平时linux是选择centos作为开发机的。

sudo docker pull centos:7.3.1611
sudo docker run --name devCentos73 -it -v /var/run/docker.sock:/var/run/docker.sock -v /mnt/xvdb1/dockerDisk/centos7.3:/data -d centos:7.3.1611
sudo docker exec -it <containerIDOrName> bash

第二步:

在容器之中安装pip,因为CentOS7.3已经自带了Python 2.7.5,并且安装docker-compose

curl -L https://bootstrap.pypa.io/get-pip.py > get-pip.py
python get-pip.py
pip install docker-compose

卸载docker-compose可以使用pip uninstall docker-compose

验证是否安装成功

[root@132ddea267ef data]# docker-compose --version
docker-compose version 1.11.2, build dfed245

第三步:

新建一个docker-compose.yml测试是否可以连上宿主docker server。

mkdir composeTest
cd composeTest/
vi docker-compose.yml
version: '2'
services:
redis:
image: redis:alpine

运行docker-compose up &

[root@132ddea267ef composeTest]# docker-compose up &
[1] 223
[root@132ddea267ef composeTest]# Creating composetest_redis_1
Attaching to composetest_redis_1
redis_1 | 1:C 28 Mar 02:24:51.130 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | _._
redis_1 | _.-``__ ''-._
redis_1 | _.-`` `. `_. ''-._ Redis 3.2.8 (00000000/0) 64 bit
redis_1 | .-`` .-```. ```\/ _.,_ ''-._
redis_1 | ( ' , .-` | `, ) Running in standalone mode
redis_1 | |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
redis_1 | | `-._ `._ / _.-' | PID: 1
redis_1 | `-._ `-._ `-./ _.-' _.-'
redis_1 | |`-._`-._ `-.__.-' _.-'_.-'|
redis_1 | | `-._`-._ _.-'_.-' | http://redis.io
redis_1 | `-._ `-._`-.__.-'_.-' _.-'
redis_1 | |`-._`-._ `-.__.-' _.-'_.-'|
redis_1 | | `-._`-._ _.-'_.-' |
redis_1 | `-._ `-._`-.__.-'_.-' _.-'
redis_1 | `-._ `-.__.-' _.-'
redis_1 | `-._ _.-'
redis_1 | `-.__.-'
redis_1 |
redis_1 | 1:M 28 Mar 02:24:51.134 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 28 Mar 02:24:51.134 # Server started, Redis version 3.2.8
redis_1 | 1:M 28 Mar 02:24:51.134 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1 | 1:M 28 Mar 02:24:51.134 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1 | 1:M 28 Mar 02:24:51.134 * The server is now ready to accept connections on port 6379

第四步:

验证是否安装成功,进入宿主主机执行命令

grj@GrjServer ~/command $ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b4a04bf9cdef redis:alpine "docker-entrypoint.sh" 8 seconds ago Up 7 seconds 6379/tcp composetest_redis_1

美中不足的是,系统启动的时候,container不能自动启动,这个问题我是使用systemd来解决的,只是觉得还是麻烦。

注:没有把踩的坑给列出来,如遇到解决不了的问题欢迎留言咨询。