I am new to docker. I am trying to run a docker container running Apache and PHP or PHP-FPM.
Like that
docker run -p 80:80 --name my-apache-php-app -v "$PWD":/var/www/html php:7.0-apache
And it attaches STDIN/STDOUT to my host machine
docker run -d -p 80:80 --name my-apache-php-app -v "$PWD":/var/www/html php:7.0-apache
Seems to work fine, but I read that
By design, containers started in detached mode exit when the root
process used to run the container exits.
Does it mean if I start container from remote ssh connection and then close it, the docker container will stop ?
I am building my custom docker image based on the
library/php/
And I need my image to start automatically in background, not in foreground.
How can I get this behavior ?
By design, containers started in detached mode exit when the root process used to run the container exits.
The root process in this context is the root process inside the container, not the root process used to docker run the container. This process is typically an ENTRYPOINT or CMD in the Dockerfile. For Nginx, often you see something like:
ENTRYPOINT ['nginx', '-g', 'daemon off']
This container would exit when the nginx process exits (which shouldn't be immediately unless your nginx configuration is bad, or some other error kills it).
If you were to have an ENTRYPOINT like this:
ENTRYPOINT ['nginx', '-t']
Then the container would test any nginx confs it found, give you the result in STDOUT, and exit.
Related
I have a php container which needs php-fpm to be started everytime I start the container . Now because of a wrong configuration in php-fpm config file , fpm does not gets started and so , container cannot start. Is there anyway that I can start the container without php-fpm so that I can fix the config file?
The container error is as follows :
[04-Sep-2020 13:47:30] ERROR: [/usr/local/etc/php-fpm.conf:7] value is NULL for a ZEND_INI_PARSER_ENTRY
[04-Sep-2020 13:47:30] ERROR: failed to load configuration file '/usr/local/etc/php-fpm.conf'
[04-Sep-2020 13:47:30] ERROR: FPM initialization failed
There are two ways to fix the image. Since I can't find image digitalocean/php, I'll use php:7.4-fpm in my example.
First way:
Copy file from the container and use it to build your own image:
Create Dockerfile:
FROM php:7.4-fpm
COPY ./php-fpm.conf /usr/local/etc/php-fpm.conf
Then:
docker run --detach --name php php:7.4-fpm tail -f /dev/null
docker cp php:/usr/local/etc/php-fpm.conf php-fpm.conf
docker stop php
docker rm -v php
# Edit php-fpm.conf
docker build --tag myphp-fm .
docker run --detach --name php myphp-fpm
and you get running container based on the fixed image.
Second way:
Run a shell using the broken image, fix the file and create a new image using the shell container
docker run -it --name php php:7.4-fpm bash
# Edit /usr/local/etc/php-fpm.conf
# If you install any additional tools remember to remove them afterwards
# and clean any cache's
# Once you're done exit the shell, thus stopping the container
docker commit -a "you" -m "/usr/local/etc/php-fpm.conf fix" php myphp-fpm
docker stop php
docker rm -v php
docker run --detach --name php myphp-fpm
and again you get running container based on the fixed image.
Of course, you can run your new image in whatever way you run the original image in the beginning.
I recommend the first way as it's way easier to edit the file outside the container.
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?
Can we change the CMD of the container.
this is what i did run
$ docker run -it stock_image sh
sh#docker # did some changes here
$ # exited container
$ docker commit [commit-id] new_image
I looked into docker inpsect new_image the CMD variable has been lost, I know it can be solved using build process using Dockerfile but I'm curious can we set docker image CMD any how?
Reason I want to do this, docker-compose.yml cannot set the CMD (i'm not sure, hoping it can do), since I have to do few changes entering the container using docker exec then commit it rather than re-doing the whole process.
P.S. i was adding pdo pdo_mysql mysqli to php:7.2.5-fpm-alpine3.6 image
Actually, you can use Dockerfile commands upon "docker commit".
docker commit -c "CMD /my/new/app" [commit-id] new_image
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.
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)