How can I automatically execute a script when creating a container?

0

I have the following need: when creating a new container I would like to automatically run the /usr/local/bin/start script that is inside the image, this script basically verifies that the variable ENVIRONMENTAL TYPE is equal to prod , if yes change 2 file parameters.

Obs : I would like to run this script only when creating the container, however, it does not have a problem if the only alternative is to run it whenever the container is started.

FROM php:7.2-apache

MAINTAINER Fabio J L Ferreira <[email protected]>

# Instala e configura componentes essenciais
RUN apt-get update && \
    a2enmod rewrite && \
    apt-get install -y --no-install-recommends unzip git && \
    curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer; \
    echo "America/Sao_Paulo" > /etc/timezone; \
    \
    # Configura componentes do Apache e PHP
    { \
        echo '<VirtualHost *:80>'; \
        echo '\tServerAdmin webmaster@localhost'; \
        echo '\tDocumentRoot /var/www'; \
        echo; \
        echo '\tErrorLog ${APACHE_LOG_DIR}/error.log'; \
        echo '\tCustomLog ${APACHE_LOG_DIR}/access.log combined'; \
        echo; \
        echo '\t<Directory /var/www>'; \
        echo '\t\tOptions Indexes FollowSymLinks'; \
        echo '\t\tAllowOverride None'; \
        echo '\t\tRequire all granted'; \
        echo '\t</Directory>'; \
        echo '</VirtualHost>'; \
    } | tee /etc/apache2/sites-enabled/000-default.conf; \
    docker-php-source extract; \
    cp /usr/src/php/php.ini-development /usr/local/etc/php/php.ini; \
    sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 50M/' /usr/local/etc/php/php.ini; \
    docker-php-source delete

# Aqui instalamos algumas extensões comumente utilizadas
RUN apt-get install -y --no-install-recommends libjpeg-dev libpng-dev && \
    docker-php-ext-configure gd --with-jpeg-dir=/usr/include/ && \
    docker-php-ext-install gd; \
    \
    # Instala a extensão PHP "exif" => http://php.net/manual/pt_BR/intro.exif.php
    apt-get install -y --no-install-recommends libexif-dev && \
    docker-php-ext-install exif; \
    \
    # Instala as extensões PHP "mysqli pdo_mysql pgsql pdo_pgsql"
    apt-get install -y --no-install-recommends libpq-dev && \
    docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql && \
    docker-php-ext-install mysqli pdo_mysql pgsql pdo_pgsql

# Limpa repositório local
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

WORKDIR /var/www

EXPOSE 80

COPY start /usr/local/bin/

How could you do this? After solving this problem, I need to check if passing this variable in the container creation will have the expected effect.

    
asked by anonymous 16.01.2018 / 00:58

2 answers

1

You can do this using the ENTRYPOINT command. With it, you specify a command to execute when the container starts, so you do not always have to specify the command on the command line as suggested in another response.

You said that you prefer a method that only runs at creation, not at all execution. But it is relatively simple to put a rule in the script to check whether it has already been executed or not. You can use a configuration file, for example, to verify this.

I agree that a native solution would be legal, but this is subject to much debate in the docker community . docker-compose events has been implemented, allowing you to create a script like that , but I'm not sure how that would help do what you want.

    
16.01.2018 / 02:58
0

In order to run any command after the container is created, just add it after the image name:

docker container run nome-da-imagem comando-aqui

You can also add more options to the command above. Example using the Alpine Linux distribution:

$ docker container run -e TIPO_AMBIENTE=prod alpine env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=8483090bf7ec
TIPO_AMBIENTE=prod
HOME=/root

So, for example, to run your script:

docker container run -e TIPO_AMBIENTE=prod sua-imagem /usr/local/bin/start
    
16.01.2018 / 02:01