简体   繁体   中英

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

I have an assessment in which I have to create a Docker container with CakePHP. I already have a working Docker container with CakePHP and I run the following commands for my container:

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

The goal is to reduce the process down to only running the single command docker-compose up to be able to start testing the container. I'm very new to both Docker and CakePHP so I'm not sure how to do this.

Any help is greatly appreciated!

Dockerfile

#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:

Option 1

What I usually do to reduce my commands when handling Docker and Docker Compose in general is using a Makefile .

So, in your case, you could write something like:

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

All you would have to do then is putting this file in your project folder and run make start .

(The $(SUDO) part ensures that you can run this comfortably even with a user that is not in the docker group.)

Option 2

To really just run docker-compose up (perhaps with --build flag), you would have to write a little script that you COPY into the Docker image (you're already doing that with COPY . $APP_HOME – provided that you put this script at where your Docker build context points to) and then use it as ENTRYPOINT .

Something like this should work for you.

entrypoint.sh:

#!/bin/sh
set -e

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

exec "$@"

In your Dockerfile, you would then have to put ENTRYPOINT ["/bin/sh", "entrypoint.sh"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM