仅使用 docker-compose up 命令设置 Docker 容器

Setup a Docker container with only the docker-compose up command

我有一项评估,其中我必须使用 CakePHP 创建一个 Docker 容器。我已经有一个带有 CakePHP 的工作 Docker 容器,我 运行 我的容器的以下命令:

docker-compose build

docker-compose run cakephp composer install --no-interaction

docker-compose run cakephp bin/cake migrations migrate

docker-compose run cakephp bin/cake migrations seed

docker-compose up

目标是将流程缩减为仅 运行 执行单个命令 docker-compose up 以便能够开始测试容器.我对 Docker 和 CakePHP 都很陌生,所以我不知道该怎么做。

非常感谢任何帮助!

Docker文件

#start with our base image (the foundation) - version 7.1.29
FROM php:7.1.29-apache

#install all the system dependencies and enable PHP modules 
RUN apt-get update && apt-get install -y \  
      gcc \
      make \
      autoconf \
      libc-dev \
      pkg-config \
      libicu-dev \
      libpq-dev \
      libmcrypt-dev \
      mysql-client \
      git \
      zip \
      unzip \
    && rm -r /var/lib/apt/lists/* \
    && docker-php-ext-configure pdo_mysql --with-pdo-mysql=mysqlnd \
    && docker-php-ext-install \
      intl \
      mbstring \
      mcrypt \
      pcntl \
      pdo_mysql \
      pdo_pgsql \
      pgsql \
      opcache

RUN set -eux; apt-get update; apt-get install -y libzip-dev zlib1g-dev; docker-php-ext-install zip

#install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer

ENV COMPOSER_ALLOW_SUPERUSER=1

#set our application folder as an environment variable
ENV APP_HOME /var/www/html

#change uid and gid of apache to docker user uid/gid
RUN usermod -u 1000 www-data && groupmod -g 1000 www-data

#change the web_root to laravel /var/www/html/public folder
RUN sed -i -e "s/html/html\/webroot/g" /etc/apache2/sites-enabled/000-default.conf

# enable apache module rewrite
RUN a2enmod rewrite && \
        echo "ServerName localhost" >> /etc/apache2/apache2.conf

#copy source files and run composer
COPY . $APP_HOME

# install all PHP dependencies
RUN composer install --no-interaction

#change ownership of our applications
RUN chown -R www-data:www-data $APP_HOME

docker-compose.yml

version: '2'
services:
  cakephp:
    build: .
    depends_on:
      - mysql
    links:
      - "mysql"
    ports:
      - "4000:80"  
    volumes:
      - .:/var/www/html/
    environment:
      - SECURITY_SALT= *some salt here*  
      - MYSQL_URL=mysql
      - MYSQL_USERNAME=root
      - MYSQL_PASSWORD=root
  mysql:
    image: mysql:5.6
    volumes:
      - mysql-data:/var/lib/mysql
    environment:      
      - MYSQL_DATABASE=cakephp
      - MYSQL_ROOT_PASSWORD=root
volumes:
  mysql-data:

选项 1

在处理 Docker 和 Docker Compose 时,我通常会使用 Makefile.

来减少命令

所以,在你的情况下,你可以这样写:

生成文件

SUDO := $(shell groups | grep -q docker || echo sudo)

.PHONY: start

start:
    $(SUDO) docker-compose build \
    && $(SUDO) docker-compose run cakephp composer install --no-interaction \
    && $(SUDO) docker-compose run cakephp bin/cake migrations migrate \
    && $(SUDO) docker-compose run cakephp bin/cake migrations seed \
    && $(SUDO) docker-compose up

然后您所要做的就是将此文件放入您的项目文件夹中,然后 运行 make start.

$(SUDO) 部分确保您可以 运行 即使用户不在 docker 组中也能轻松做到这一点。)

选项 2

要真正做到 运行 docker-compose up(可能带有 --build 标志),您必须编写一个小脚本,将 COPY 放入 Docker 图像(你已经用 COPY . $APP_HOME 这样做了——前提是你把这个脚本放在你的 Docker 构建上下文指向的地方)然后将它用作 ENTRYPOINT.

像这样的东西应该适合你。

entrypoint.sh:

#!/bin/sh
set -e

cakephp composer install --no-interaction
cakephp bin/cake migrations migrate
cakephp bin/cake migrations seed

exec "$@"

在您的 Docker 文件中,您必须将 ENTRYPOINT ["/bin/sh", "entrypoint.sh"]