DOCKERFILE: Running multiple CMD. (Starting NGINX and PHP) [duplicate] - php

This question already has answers here:
Why can't I use Docker CMD multiple times to run multiple services?
(5 answers)
Closed 4 years ago.
I have a dockerfile that sets up NGINX, PHP, adds a Wordpress Repository. I want at boot time, to start PHP and NGINX. However, I am failing to do so. I tried adding the two commands in the CMD array, and I also tried to put them in a shell file and starting the shell file. Nothing worked. Below is my Dockerfile
FROM ubuntu:16.04
WORKDIR /opt/
#Install nginx
RUN apt-get update
RUN apt-get install -y nginx=1.10.* php7.0 php7.0-fpm php7.0-mysql
#Add the customized NGINX configuration
RUN rm -f /etc/nginx/nginx.conf
RUN rm -f /etc/nginx/sites-enabled/*
COPY nginx/nginx.conf /etc/nginx/
COPY nginx/site.conf /etc/nginx/sites-enabled
#Copy the certificates
RUN mkdir -p /etc/pki/nginx
COPY nginx/certs/* /etc/pki/nginx/
RUN rm -f /etc/pki/nginx/placeholder
#Copy the build to its destination on the server
RUN mkdir -p /mnt/wordpress-blog/
COPY . /mnt/wordpress-blog/
#COPY wp-config.php
COPY nginx/wp-config.php /mnt/wordpress-blog/
#The command to run the container
CMD ["/bin/bash", "-c", "service php7.0-fpm start", "service nginx start"]
I tried to put the commands in the CMD in a shell file, and run the shell file in the CMD command. It still didn't work. what am i missing?

start.sh
#!/bin/bash
/usr/sbin/service php7.0-fpm start
/usr/sbin/service nginx start
tail -f /dev/null
Dockerfile
COPY ["start.sh", "/root/start.sh"]
WORKDIR /root
CMD ["./start.sh"]
With this, you can put more complex logic in start.sh.

You can replace the CMD line for some like ...
CMD ["/bin/bash", "-c", "/usr/sbin/service php7.0-fpm start && nginx -g 'daemon off;'"]

TL;DR: You don't have an entry point.
Main idea in the Docker is to have one responsibility per container. So, in order to keep running a Docker container you have to start a program in foreground upon container boot.
However, in your Dockerfile, there is no entrypoint to start a program in foreground. So, just after your container boot, your container exits.
So, in order to prevent your container from exiting, just start a program in foreground.
Nginx for instance.
Example scenario:
entrypoint.sh content:
#!/bin/bash
service php7.0-fpm start
nginx -g 'daemon off;
somewhere in Dockerfile:
COPY [ "./entrypoint.sh", "/root/entrypoint.sh" ]
at the end of the Dockerfile:
ENTRYPOINT /root/entrypoint.sh

Related

Is it possible to run a bash script through Dockerfile and keep the container running after docker-compose up -d?

The following install.sh script file automate the installation of my Laravel dependencies inside a container:
#!/bin/bash
LOGFILE=/tmp/install_diario_$(date +"%Y%m%d%H%M%S").log
[ -f ../.env ] || cp ../.env.docker ../.env
function error {
echo -e "\e[31m\e[1m[ERROR]"
echo -e 'See' $LOGFILE 'to more information\e[0m'
exit 1
}
function ok {
echo -e "\t\e[32m\e[1m[OK]\e[0m"
}
function installed {
echo -e "\t\e[29m\e[1m[OK]\e[0m"
}
echo '[+] Installing PHP packages'
composer install -d "/var/www/html" 2>> $LOGFILE >> $LOGFILE
if [ $? -eq 1 ]; then
echo '[!] Configuration Aborted. Exiting...'
fi
echo '[+] Generating app keys'
php ../artisan key:generate #2>> $LOGFILE >> $LOGFILE
php ../artisan passport:install #2>> $LOGFILE >> $LOGFILE
echo '[+] Populating database'
# cd .. && make resetdb
echo '[+] Backend installation sucessfull.'
echo ""
php ../artisan passport:show
echo '[+] Front-end install'
npm install 2>> $LOGFILE >> $LOGFILE
However, I don't want to run this manually, but while the container is starting. So I tried using the following commands in my Dockerfile:
WORKDIR /var/www/html/docker
ADD install.sh .
RUN chmod +x ./install.sh
CMD ./install.sh
Obs.: the script is inside a folder called docker
But when I run docker-compose up --build -d my container exits after a few seconds (when the script is done).
I tried looking for solutions but none worked for me (e.g. including /bin/bash in the end of my script).
Does anyone know if this is actually possible to be done, or should I just tell my workmates to run this script manually with docker exec -it <app_id> install.sh?
The problem here is the following line in your Dockerfile:
CMD ./install.sh
Indeed, your script overrides (ie is called in place of) the default php-fpm command.
Solution 1: add a line at the end of your install.sh script to invoke php-fpm
exec "php-fpm"
CAVEAT: php-fpm MUST NOT be launched as a service, it must run in the foreground to keep the container up and running.
Solution 2: implement a custom entrypoint that launches the install script
Remove/comment the CMD line from the Dockerfile
Implement the customized entrypoint script.
Eg:
In the Dockerfile:
# Don't override COMMAND, use the default one
#CMD ./install.sh
COPY entrypoint /usr/bin/
RUN chmod +x /usr/bin/entrypoint
ENTRYPOINT /usr/bin/entrypoint
And the entrypoint script:
# Run the install script
/path/to/install.sh
# Execute the default command, ie php-fpm
exec "$#"
*NB: Here is the minimum basic working code, feel free to customize/enrich this example - using the official docker php entrypoint for instance.
To a docker container, there is no distinction between its first start up from the consequence start unless you put some kind of file in a mounted volume.
What you probably want is a start up script that knows if this is the first start, run the install script, then remember to not run it again.
So besides your install script, you should probably have a start up script like this:
#!/bin/bash
# Assuming you mount this folder to your host system
MOUNTED_VAR=/var/run/laravel
# Only run the install on first run
if [ ! -f $MOUNTED_VAR/installed ]; then
./install.sh
fi
# Some command to start the service, for example
systemctl start php-fpm
In your docker file, you need to also include this
script to start.
WORKDIR /var/www/html/docker
ADD install.sh .
RUN chmod +x ./install.sh
ADD start.sh .
RUN chmod +x ./start.sh
CMD ./start.sh

PHP-FPM is not being restarted properly from a Dockerfile

I am having this weird problem with PHP-FPM. First of all, I am not an expert on PHP. I'm builing an app, and PHP will only communicate with MySQL to push and pull data.
The problem:
I have a Dockerfile where I'm making some changed in PHP-FPM config, and one of the lines is to reload PHP-FPM. Actually when you install PHP-FPM, it is not running, so I do:
RUN service php7.3-fpm start
When the app starts and I check PHP-FPM, it is not running. I had this issue before (with php7-0-fpm), I had resolved it by doing:
service php7.0-fpm stop && service php7.0-fpm start
But now, when I do it, it is still stopped.
So, I started reading and someone advised to put it in the CMD command, along with the main command. I did, and it worked:
CMD service php7.3-fpm start && nginx -g "daemon off;"
Now, the problem is that it is not taking the new configuration. When I access the machine, and I manually do service php7.3-fpm reload it starts working.
I tried putting it in the Dockrfile as well, but no luck. Any idea? I would like to resolve the first issue as well (I don't want to restart PHP-FPM from the CMD. It would be preferable to do it with a RUN layer in docker).
EDIT
Another weird thing is that when I do service php7.3-fpm start manually, it doesn't work, but it works when I do /etc/init.d/php7.3-fpm start. Seems to work, when I do it in the CMD line:
CMD /etc/init.d/php7.3-fpm start && nginx -g "daemon off;"
Dockerfile
FROM debian:buster
LABEL maintainer="me"
RUN apt-get update && apt-get install -y \
nginx \
default-mysql-client \
php7.3-fpm \
php7.3-mysql
RUN sed -i.bak "s/;clear_env = no/clear_env = no/g" /etc/php/7.3/fpm/pool.d/www.conf && \
sed -i.bak "s/;php_flag\[\display_errors\]\ = off/php_flag\[\display_errors\]\ = on/g" /etc/php/7.3/fpm/pool.d/www.conf
COPY ./html/ /var/www/html/
RUN rm /var/www/html/index.nginx-debian.html
WORKDIR /var/www/html/
EXPOSE 80
CMD service php7.3-fpm start && nginx -g "daemon off;"
Execute a command or restart php-fpm using RUN directive will not effect because each layer runs in a separate shell.
The best way is to copy the config file from host, rebuild the image and then run the container.
for Example
FROM debian:buster
COPY config/php7.ini /usr/local/etc/php/conf.d/
COPY config/fpm/php-fpm.conf /usr/local/etc/
COPY config/fpm/pool.d /usr/local/etc/pool.d
Also better to run the separate container for each process, rule of thumb single process per container.
You CMD seem fine it starts PHP and Nginx both.
To verify process inside your container add
RUN apt-get update && apt-get install procps -y
then run
docker exec -it your_container_id bash -c "ps -aux"

Docker php5-fpm service exited (code 0)

Read a lot of topics, but couldn't understand what's going on. All is working fine before I add ENTRYPOINT to my Dockerfile. Container stops immediately without demonizing php-fpm:
FROM php:5.6-fpm
// ..Some installation instructions
# Entrypoint script
COPY ./run.sh /run.sh
RUN chmod +x /run.sh
ENTRYPOINT ["/run.sh"]
CMD ["php-fpm"]
The content of run.sh:
# Install all dependencies
php -d allow_url_fopen=on /usr/local/bin/composer install
As I understand my entrypoint will be executed with run.sh and then exited. If I will remove it then default entrypoint will be starting nginx in background. What is the best solution to run shell scripts without redefining entrypoint? Or maybe I'm talking wrong things..
ENTRYPOINT and CMD are combined to create the final COMMAND that is run when the container is started. In your case this gives:
["/run.sh", "php-fpm"]
which means that php-fpm acts as an argument to the /run.sh script. That's obviously not what you want.
You can fix this by starting php-fpm inside your script AND making sure that it runs as PID1 using exec. Running the main process as Process ID 1 assures that it will receive SIGKILL and SIGTERM interrupts (Ctrl-C for instance) and exit gracefully if possible.
# Install all dependencies
php -d allow_url_fopen=on /usr/local/bin/composer install
exec php-fpm
Your CMD should then be empty (or removed, as specifying an ENTRYPOINT also resets the CMD):
CMD []
Then in your container you can specify arguments to php-fpm via the command. Eg:
docker run -d my_php_fpm_image --help
The problem is this:
starting nginx in background
You need a process running in the foreground. If there is none, the container will exit. I think you should keep nginx running in the foreground.
To do this, use:
php-fpm -F -R
From php-fpm help:
-F, --nodaemonize force to stay in foreground, and ignore daemonize option from config file
-R, --allow-to-run-as-root Allow pool to run as root (disabled by default)
Found the way to put composer in a separate container. So i will not touch my php-fpm at all, as the best practice is one process per container.
My app container that contains all project files (composer.json, .git etc.) will provide Dockerfile:
FROM composer/composer:php5
# Set application directory
WORKDIR /var/www/html
ENTRYPOINT /usr/local/bin/composer install
CMD ["true"]
After initiating docker-compose up -d this will bring all the dependencies from composer.json into mapped directory.

How can I start php-fpm in a Docker container by default?

I have this Docker image -
FROM centos:7
MAINTAINER Me <me.me>
RUN yum update -y
RUN yum install -y git https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
RUN yum install -y ansible
RUN git clone https://github.com/.../dockerAnsible.git
RUN ansible-playbook dockerFileBootstrap.yml
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
EXPOSE 80 443 3306
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
Basically, I want it so that php-fpm starts when the docker container starts. I have php-fpm working if I manually go into the container and turn it on with /usr/sbin/php-fpm.
I tried it inside of my ansible file with this command (it didn't work). I tried using the service module as well with no luck.-
- name: Start php fpm
command: /usr/sbin/php-fpm
How can I have php-fpm running along with apache?
You should use supervisor in order to launch several services
In your dockerfile, install supervisor, then you launch
COPY ./docker/supervisord.conf /etc/supervisord.conf
....
CMD ["/usr/bin/supervisord", "-n"]
And your docker/supervisord.conf contains all the services you want to start, so you can have something like that
[program:php-fpm]
command=/opt/remi/php70/root/usr/sbin/php-fpm -c /etc/php-fpm.conf
;command=/usr/sbin/php70-fpm -c /etc/php-fpm.d
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
[program:nginx]
command=/usr/sbin/nginx
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
Of course you should adapt with your path and php-fpm versions and your services (nginx in my example, apache for you etc...), but basically supervisor is the best way to manage the start of several services from one start point.
Here you can find the official doc of docker about supervisor
https://docs.docker.com/engine/admin/using_supervisord/
I came here looking for how to run php-fpm in the foreground so it could be PID 1 in a docker container. The solution is
php-fpm -F -R
Explanation
We can check the available options with php-fpm --help
-F, --nodaemonize
force to stay in foreground, and ignore daemonize option from config file
If you are running php-fpm in a docker container, there is a good chance you are running the process as root. php-fpm won't start as root without an extra flag:
-R, --allow-to-run-as-root
Allow pool to run as root (disabled by default)
I needed similar thing recently. For alpine linux images it sufficed to run both php-fpm and a web server as the container command. Defined in Dockerfile somewhat like this:
CMD /usr/bin/php-fpm -D; nginx
ie. to daemonize php-fpm and then run nginx in foreground.
On ubuntu/debian images there is also necessary to allow starting recently installed packages by running a Dockerfile RUN command like this:
RUN echo "exit 0" > /usr/sbin/policy-rc.d
and then restart the php-fpm within a CMD command
CMD /etc/init.d/php7.0-fpm restart && nginx -g "daemon off;"
More on policy-rc.d to be found in this askubuntu question
You may find this config useful if you'd like to run php-fpm and Apache (usually using MPM Event) in the same container:
[supervisord]
[program:php-fpm]
command=php-fpm -F -R
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
[program:apachectl]
command=apachectl -D "FOREGROUND" -k start
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
This works for me on Docker running Ubuntu 20.04 with nginx and php-fpm.
CMD /etc/init.d/php7.4-fpm start -F && nginx -g "daemon off;"
No supervisors or cron jobs are required.
I use rc.local to start services inside a container, and then run a command inside that container to execute rc.local. So here's a sample run command:
sudo docker run --restart=always -itd --name mycontainer -p 80:80 -p 443:443 myimage/name /bin/bash -c "/etc/rc.local && while true; do echo hello world; sleep 100; done"
And this is one of the rc.local files from /etc
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
sleep 5
service mysql start
sleep 5
service php5-fpm start
sleep 5
service nginx start
exit 0
What this does is launch a container called "mycontainer" using the image "myimage/name" with ports 80 and 443 exposed. Once launched it calls mysql, php5-fpm, and nginx to start up, pausing between each for 5 seconds. In this manner you can call any service to start that you would want to run inside that container. Remember to add open ports for those services though.
This run command will also append "Hello World" into your log file every 100 seconds as a "pulse" that will let you know when it was running and when it was stopped.
I believe #ckeeney's answer above could be accepted as the correct answer but I was unable to get it working with my particular case.
use dumb-init
I have been able to proxy PID1 through dumb-init, and daemonize PHP-FPM with the following command : dumb-init /usr/sbin/php-fpm7.2 -F -R
https://engineeringblog.yelp.com/2016/01/dumb-init-an-init-for-docker.html

Nginx in Docker container won't run PHP

I'm beginner with Docker, and I'm trying to build my own image: Ubuntu + Nginx + PHP.
So, I have a directory called test. Inside directory two other directories, app and sites-enabled. Also, there's a Dockerfile, with content:
FROM ubuntu:trusty
RUN apt-get update && \
apt-get install -y nginx php5-fpm php5-mysql php-apc php5-imagick php5-imap php5-mcrypt php5-gd libssh2-php && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
ADD sites-enabled/ /etc/nginx/sites-enabled/
ADD app/ /app/
EXPOSE 80
CMD ["php5-fpm", "-c", "/etc/php5/fpm"]
CMD ["/usr/sbin/nginx"]
I build this image successfully. Then I create container with docker run -d image_name. I get the ID, and then I run docker inspect -f "{{.NetworkSettings.IPAddress}}" ID in order to get the IP address of the container.
I need this IP address, because I also run HAProxy in another container, so I can configure it to point to the right localtion.
So, both HAProxy and container with PHP app are running OK. HAProxy is pointing at the right application. PHP application files are uploaded at the right location inside the container.
But, Nginx doesn't execute PHP. Instead, when I try to access the application, I just get a downloaded file with my index.php PHP code.
What could be the problem? Please help.
My first guess was that I'm doing something wrong in Dockerfile when I run php5-fpm. I've tried few different ways, but non of them seem to work.
One CMD only.
If you need "services", look into supervisord or runit.

Categories