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

Asked

Viewed 1,756 times

0

I have the following need: when creating a new container I would like to automatically run the script /usr/local/bin/start inside the image, this script basically checks whether the variable TIPO_AMBIENTE is equal to Prod, whether to change 2 file parameters.

Obs: initially would like to run this script only when creating the container, however, it is no 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/en/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 I do this? After solving this problem, I need to check if passing this variable in the container creation will have the expected effect.

2 answers

1

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

You said you prefer a method that runs only in creation, not in every 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 check this.

I agree that it would be cool to have a native solution, but this is the subject of much debate in the Docker community. Was implemented the docker-compose events, allowing you to create a script like this, but I’m not sure how it would help to do what you want.

0

To be able to run any command after the container is created simply add it after the image name:

docker container run nome-da-imagem comando-aqui

You can also add more options to the above command. 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
  • would not have had to include this in script form in Dockerfile, to be executed in the container creation, ie when "start" the image?

  • Ah, yes. If you want it to always run and be embedded in the image, just use CMD at the end of Dockerfile. So whenever the image is used to generate a container, at the end the command will run inside the container. For example, at the end of Dockerfile: CMD ["/usr/local/bin/start"]

  • I even did something like this, however, I was unable to run the container with the -d option

  • @Fábiojânio may be because Voce needs to use Tini to run the script. https://github.com/krallin/tini

Browser other questions tagged

You are not signed in. Login or sign up in order to post.