PHP-FPM wont start from a Dockerfile - php

Hello dear community,
I am trying to accomplish something very simple, I want to start a php-fpm service from a docker container using a dockerfile. My dockerfile content is posted here below:
FROM debian
RUN apt-get update && apt-get install php -y && apt-get install php7.3-fpm -y && service php7.3-fpm start
When I build this image from the dockerfile and run it as a container, the php-fpm service is not active.
I even tried it with using docker's "interactive mode" (-i arg) to ensure that the container was not exiting in the case that the service was running as a daemon.
I am confused because the command RUN service php7.3-fpm start from my dockerfile should have started the service.
To successfully start the service inside my container I actually have to manually log into it using the command docker exec -it #containerID bash and run the command service php7.3-fpm start myself, and then the service works and becomes active.
I don't understand why the php-fpm service is not starting automatically from my Dockerfile, any help would be very much appreciated. Thanks in advance!

To a first approximation, commands like service don't work in Docker at all.
A Docker container runs only a single foreground process. That's not usually an init system, or if it is, it's just enough to handle some chores like zombie process cleanup. Conversely, a Docker image only contains a filesystem image and some metadata on how to start that process, but it does not persist any running processes. So for example if you
RUN service php7.3-fpm start
it might record in some file that the service was supposed to have been started, but once the RUN command completes, the running process doesn't exist at all any more.
The easiest way to get a running PHP-FPM setup is to use the Docker Hub php image:
FROM php:7.3-fpm
This should do all of the required setup, including arranging for the FPM server to run as the main container command; you just need to COPY your application code in.
If you really want to run it yourself, you need to make it be the main command of your custom image
CMD ["php-fpm"]
as is done in php:7.3-fpm's Dockerfile.

Related

Docker - Call PHP script on web container from a CRON container

I have a web app running in a docker container (apache, php).
I've been looking for solutions to install a cron job in order to regularly perform some actions on my web app (execute php files etc).
I found multiple answers (How to run a cron job inside a docker container?), all based on creating a separated container which will be responsible to run the cron jobs.
Now, how do I make this cron container communicate with my web container ?
I found multiple solutions :
Install and use CURL on my cron container
Install cron on web container and run it in the background (against good practices)
I'm also wondering if I could use a shared network in my compose file between my web and my cron container, but I'm unsure on how to make it work.
Do you guys have other ideas or code samples to help me achieve this ?
Managed to make it work using a shared network between my web container and my cron container (thanks #David Maze)
Cron Dockerfile :
FROM ubuntu:18.04
RUN apt-get update
RUN apt-get install -y systemd
RUN apt-get install -y nano
RUN apt-get install -y cron
RUN apt-get install -y curl
RUN systemctl enable cron
RUN (crontab -l -u root; echo "* * * * * curl web:80 -d 'action=cron.run'") | crontab
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Run the command on container startup
CMD cron && tail -f /var/log/cron.log
Docker compose
version: '3.7'
services:
# Web container
web:
# [...]
ports:
- "0001:80"
- "0002:443"
networks:
- cron_network
# Cron container
cron:
# [...]
depends_on:
- web
networks:
- cron_network
networks:
cron_network:

Run symfony console on container start

I'm looking for a way, to execute symfonys console after a docker container is started, such as a database migration. I'm using a alpine php-fpm image, which has the following command at the end of its Dockerfile:
CMD ["php-fpm"]
When i try to override this in my docker-compose file, either php-fpm won't start or the symfony console command won't run. Ideally the execution should wait a few seconds, to ensure that the database is started.
What would be a good solution for this scenario?

How to run docker-compose with php-fpm and php-cli?

I need to run docker-compose with two containers,- php-fpm and php-cli. Although I need another container with composer.
When I run docker-compose up -d - container with php-cli become always restarting and composer container just stops.
PHP cli is not running in daemon mode. You run it, and then it stops. Next, Docker tries to restart it (you've set restart: always policy for php-cli). :)
IMO php-cli and composer services are redundant. You can use php service for your needs. Simply run docker-compose run php php [path to script]

How to know when composer install finishes in docker container

I am using docker to deploy java and php components.
From jenkins I run something like docker run --name my_php_component -d -t my_php_image.
Inside of the container deploy.sh script will be executed. This script runs composer install.
Jenkins needs to know that/when this has finished successfully and then it can run end-to-end tests.
What is the best way for it to check that composer install has successfully installed all packages inside the docker container?

Binaries inside Docker container images not being started when not explicitly called

I'm building my own Dockerfile with official images as it's base which I'm adjusting with Ansible for simple configuration changes. Relevant portion of the dockerfile:
FROM php:7.0-fpm
MAINTAINER hyperfocus
# Ansible cmds
EXPOSE 9000
CMD [“php-fpm”]
Whenever the image is built and I try to start it with docker run php_fpm_prod:v0.1 it gives me the error: /bin/sh: 1: [“php-fpm”]: not found.
But whenever I try to start it with docker run php_fpm_prod:v0.1 php-fpm it starts succesfully:
[03-Nov-2015 10:24:38] NOTICE: fpm is running, pid 1
[03-Nov-2015 10:24:38] NOTICE: ready to handle connections
How can I make docker run php_fpm_prod:v0.1 behave like docker run php_fpm_prod:v0.1 php-fpm?
Thanks.
The CMD of a php fpm Dockerfile is already CMD ["php-fpm"] (overriding the debian-jessie CMD), so you shouldn't need to specify it again.
Those debian or php fpm Dockerfile don't define an ENTRYPOINT which means thedefault one applies /bin/sh -c.
First, make sure, as in "Dockerfile CMD command not found" to use the right quotes:
CMD ["php-fpm"]
(Or don't specify the CMD at all, since it will be inherited from the base image)

Categories