我希望在一个链接的容器启动之后运行容器中的命令。
假设我想要运行php app/console doctrine:schema:create
(或任何其他用于预填充数据库模式的应用程序……不一定是php/symfony,但不像sql脚本那么简单,可以在数据库容器中运行),只要我的容器构建完成,但显然需要在数据库容器已构建并且应用程序容量已挂载之后运行。
初始化脚本是:
- 复杂 -> 因此需要比简单的sql或bash脚本更复杂的支持
- 慢 -> 因此它只需要在设置容器时运行一次
这是我的docker-compose.yml
:
application:
build: code
volumes:
- ./symfony:/var/www/symfony
- ./logs/symfony:/var/www/symfony/app/logs
tty: true
db:
image: mysql
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: root
MYSQL_USER: root
MYSQL_PASSWORD: longtake
php:
build: php-fpm
expose:
- 9000:9000
volumes_from:
- application
links:
- db
nginx:
build: nginx
ports:
- 80:80
links:
- php
volumes_from:
- application
volumes:
- ./logs/nginx/:/var/log/nginx
相关的Dockerfile
是php-fpm/Dockerfile,可能是这样的:
FROM debian:jessie
RUN apt-get update && apt-get upgrade && apt-get install -y php5-common php5-cli php5-fpm php5-mcrypt php5-mysql php5-apcu php5-gd php5-imagick php5-curl php5-intl
# This should be executed after the application volume is ready
RUN cd /var/www/symfony && php app/console doctrine:schema:create
ADD symfony.ini /etc/php5/fpm/conf.d/
ADD symfony.ini /etc/php5/cli/conf.d/
ADD symfony.pool.conf /etc/php5/fpm/pool.d/
RUN usermod -u 1000 www-data
CMD ["php5-fpm", "-F"]
EXPOSE 9000
docker-compose build
返回的结果如下:
Step 4 : RUN cd /var/www/symfony && php app/console doctrine:schema:create
---> Running in 629ab2771cb3
/bin/sh: 1: cd: can't cd to /var/www/symfony
Service 'php' failed to build: The command '/bin/sh -c cd /var/www/symfony && php app/console doctrine:schema:create' returned a non-zero code: 2