简体   繁体   中英

Run Bash script once Docker container is loaded

I am running a Docker through docker-composer up. Its a LAMP environment and works fine. I have a docker file where i am installing few Apache modules and loading them. After the Docker container is loaded i want to run a bash script to install a php library and to start a service. My bash script is below

#!/bin/bash

cd var/www/html/gearman-1.1.2
./configure
make
make install

service apache2 restart

gearmand -d

echo "German should be working now"

I want to run this script every time a docker-composer up -d command is executed so i don't have to manually do the process.

I am adding the below lines in my Docker file

RUN chmod +x /var/www/html/run.sh
RUN run.sh

The above commands are throwing errors which results in Apache server not starting. Can you please tell me what is the right way to execute a bash script after a docker is up. Location of my bash script is in var/www/html and i am mounting this location to my local drive as well.

Bash script runs fine when i run it manually through the command link after logging into the container.

Complete docker file looks like below

FROM php:5.6-apache

RUN apt-get -y update && apt-get upgrade -y

# Install tools && libraries
RUN apt-get -y install --fix-missing apt-utils nano wget dialog \
    build-essential git curl libcurl3 libcurl3-dev zip \
    libmcrypt-dev libsqlite3-dev libsqlite3-0 mysql-client \
    zlib1g-dev libicu-dev libfreetype6-dev libjpeg62-turbo-dev libpng-dev libgearman-dev \
    gearman-job-server \
    && rm -rf /var/lib/apt/lists/*

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

# PHP5 Extensions
RUN docker-php-ext-install curl \
    && docker-php-ext-install tokenizer \
    && docker-php-ext-install json \
    && docker-php-ext-install mcrypt \
    && docker-php-ext-install pdo_mysql \
    && docker-php-ext-install pdo_sqlite \
    && docker-php-ext-install mysqli \
    && docker-php-ext-install zip \
    && docker-php-ext-install -j$(nproc) intl \
    && docker-php-ext-install mbstring \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd \
    && pecl install xdebug-2.5.5 && docker-php-ext-enable xdebug \
    && echo "xdebug.remote_enable=1" >> /usr/local/etc/php/php.ini


# Enable apache modules
RUN a2enmod rewrite headers

EXPOSE 80

ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

#RUN /bin/bash "chmod +x /var/www/html/run.sh"
#RUN run.sh

It will be worth mentioning that i am using docker toolkit and i am on windows 10 home

It looks like you need to launching two processes in your container, gearman and apache . And both of them need to be configured(compiled). So you need to split your run.sh to two part. The first one is used for compiling the gearman and the second one is used for launching the apache and gearman . This is a sample Dockerfile and a sample entrypoint file for you:

entrypoint.sh :

#!/bin/bash

gearmand -d
status=$?
if [ $status -ne 0 ]; then
  echo "Failed to start gearmand: $status"
  exit $status
fi

/usr/sbin/apache2ctl -D FOREGROUND;

Dockerfile :

FROM php:5.6-apache

RUN apt-get -y update && apt-get upgrade -y

# Install your PHP extensions and dependencies ...

# Enable apache modules
RUN a2enmod rewrite headers

# compile gearman and install it
RUN cd /var/www/html/gearman-1.1.2 && ./configure && make && make install

EXPOSE 80

ADD entrypoint.sh /var/www/html/
RUN chmod +x /var/www/html/entrypoint.sh
ENTRYPOINT ["/var/www/html/entrypoint.sh"]

And your can get more information about launching multiple processes in a container from this Docker document

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