06月12, 2018

使用 docker-compose 来管理开发环境

在这之前,我的开发环境都直接使用 docker run 命令来启动各开发环境的容器(https://github.com/xlanger/docker),修改运行参数后重建环境操作还是略显麻烦,比如对某个容器参数调整并重建以后,IP并不是固定(开发环境中没有使用docker网络模式),--link 相关依赖的容器中已经绑定的了之前的IP,也需要相应的调整。

现在采用 docker-compose 来管理环境,定制好 docker-compose.yml 配置文件之后,只需要在配置文件所在目录下执行 docker-compose up 变创建好整个环境了,参数调整之后采用 docker-compose down 便可以移除整个环境,up 重建环境。

$ docker-compose up -d
Creating network "docker_default" with the default driver
Creating redis     ... done
Creating memcached ... done
Creating mysql     ... done
Creating php       ... done
Creating php5      ... done
Creating gunicorn  ... done
Creating nginx     ... done
$
$ curl -I ltest.x.com
HTTP/1.1 200 OK
Server: nginx/1.12.1
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.2.5
Cache-Control: no-cache, private
Date: Tue, 12 Jun 2018 04:00:11 GMT
Set-Cookie: XSRF-TOKEN=eyJpdiI6IjluWEZlRVd1SGZPbm5oSkdyUUFWY0E9PSIsInZhbHVlIjoieGdCdkJleG42Syt4ZGZDSTE4QVZiaUlGaWR6NFdrclQ2eGxVTGRBS3J1VjIzeHlZa1FsblQwR2NYd0dUdlBSWW1qd2J2MWlMbGhwcENYeSt3ZjNBS1E9PSIsIm1hYyI6ImE3ZjA0OTY3YjllZTQxNzM4NmQ0ZWJhYWNmNjk0NzA5ZTQwYTUzZjFjZDZhNWM3MTU3ZjJkZTNmYWUyNjY3OTkifQ%3D%3D; expires=Tue, 12-Jun-2018 06:00:11 GMT; Max-Age=7200; path=/
Set-Cookie: laravel_session=eyJpdiI6ImxUVXJNXC9wNUVNRGpyQnZGYm9cL2pyUT09IiwidmFsdWUiOiI3ck02b0R1b3RvWmNHWEYycHJGMnBTTlRsK3h4SitETldcL3pudGxcL3gyMlwvNEpSMHFnUWk5UTd0T2o4VjlEYThodWhJM01aeHptS0FvUFFqY0lheXBnZz09IiwibWFjIjoiZWExN2M2ZmFhZjE2ZGY0NjJkOGI4ZjAwNTZlNGQ0Zjg2ZDJjOTQ2ZjcwYmZiNjI0YzFlMWQ2ZTJjZTExYTUyNCJ9; expires=Tue, 12-Jun-2018 06:00:11 GMT; Max-Age=7200; path=/; httponly
$
$ docker-compose down 
Stopping nginx     ... done
Stopping php       ... done
Stopping php5      ... done
Stopping gunicorn  ... done
Stopping mysql     ... done
Stopping memcached ... done
Stopping redis     ... done
Removing nginx     ... done
Removing php       ... done
Removing php5      ... done
Removing gunicorn  ... done
Removing mysql     ... done
Removing memcached ... done
Removing redis     ... done
Removing network docker_default

以下是 docker-compose.yml 内容:

version: '3'
services:
    php:
        hostname: php
        container_name: php
        image: xphp:7.2.5-fpm-alpine
        command: php-fpm -F
        volumes:
            - ~/docker/php/php-fpm.conf:/usr/local/etc/php-fpm.conf
            - ~/docker/php/php-fpm.d:/usr/local/etc/php-fpm.d
            - ~/docker/php/php.ini:/usr/local/etc/php/php.ini
            - ~/docker/log/php:/var/log/php
            - ~/workspace:/wwwdir
            - ~/proj:/proj
        links:
            - "memcached:memcached"
            - "redis:redis"
            - "mysql:mysql"
    php5:
        hostname: php5
        container_name: php5
        image: xphp:5.6.36-fpm-alpine
        command: php-fpm -F
        volumes:
            - ~/docker/php5/php-fpm.conf:/usr/local/etc/php-fpm.conf
            - ~/docker/php5/php-fpm.d:/usr/local/etc/php-fpm.d
            - ~/docker/php5/php.ini:/usr/local/etc/php/php.ini
            - ~/docker/log/php5:/var/log/php
            - ~/workspace:/wwwdir
            - ~/proj:/proj
        links:
            - "memcached:memcached"
            - "redis:redis"
            - "mysql:mysql"
    gunicorn:
        hostname: gunicorn
        container_name: gunicorn
        image: xpython:alpine3.6
        command: gunicorn -c /wwwdir/pydir/flasktest/gunicorn.cfg.py app:app
        volumes:
            - ~/docker/log/python:/var/log/python
            - ~/workspace:/wwwdir
            - ~/proj:/proj
        links:
            - "memcached:memcached"
            - "redis:redis"
            - "mysql:mysql"
    nginx:
        hostname: nginx
        container_name: nginx
        image: xnginx:1.12.1-alpine
        command: nginx -g 'daemon off;'
        volumes:
            - ~/docker/nginx:/etc/nginx
            - ~/docker/log/nginx:/var/log/nginx
            - ~/workspace:/wwwdir
            - ~/proj:/proj
        links:
            - "php:phpfpm"
            - "php5:php5fpm"
        ports:
            - "80:80"
            - "443:443"
    mysql:
        hostname: mysql
        container_name: mysql
        image: my-mysql:5.7.17
        command: mysqld
        volumes:
            - ~/docker/mysql/conf.d:/etc/mysql/conf.d
            - ~/docker/data/mysql:/var/lib/mysql
            - ~/docker/log/mysql:/var/log/mysql
    memcached:
        hostname: memcached
        container_name: memcached
        image: my-memcached:1.4.34-alpine
        command: memcached -u memcache -vv
    redis:
        hostname: redis
        container_name: redis
        image: my-redis:3.0.7-alpine
        command: redis-server /etc/redis.conf
        volumes:
            - ~/docker/redis/redis.conf:/etc/redis.conf
            - ~/docker/data/redis:/var/lib/redis
            - ~/docker/log/redis:/var/log/redis

本文链接:https://xlange.com/post/docker-compose-for-dev

-- EOF --

Comments

?