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?
Related
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.
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]
Please bear with me. Pretty new to Docker.
I'm deploying Docker containers (detached) to an AWS EC2 registry using CodeDeploy. On deploy, the following command is run after setting some environmental variables etc:
exec docker run -d ${PORTS} -v cache-${CACHE_VOLUME} --env-file $(dirname $0)/docker.env --tty "${IMAGE}:${TAG}"
The container runs an image located and tagged in EC2 Container Service. No problems so far.
Since this is a PHP application (specifically a Symfony2 application) I would normally need to issue the following command to execute database migrations on deployment:
php app/console doctrine:migrations:migrate --no-interaction
Now, is there any to run this command during "docker run..." while keeping the container running, or do I need to run another container specifically for this command?
Many thanks!
You need create entrypoint. This script runs at the container startup.
entrypoint.sh file:
#create or update db
./waitforit.sh <DB_HOST>:<DP_PORT> -t 30
php app/console doctrine:migrations:execute
# start apache
apache2-foreground
wait for it it is a script waited when database is started up
Just leaving this here for the next one that searches for this... ;-)
When using a recent version of Doctrine, there is a pretty handy parameter for this:
php bin/console doctrine:migrations:migrate --no-interaction --allow-no-migration
The "allow-no-migration" parameter instructs doctrine not to throw an exception, when there is nothing to do...
I do as follows:
docker-compose exec [containerID] ./app/console migrations:migrate --no-interaction
I'm running a TeamCity agent that spawns a docker container, running several tasks inside that (php) container. Such as phpunit, phplint and composer. I zipped the content inside the container if all tests pass, it will create a phpproject.zip.
After it's done I would like to push that phpproject.zip as an Artifact back to the TeamCity server from inside the docker container.
My docker container is running with the --rm parameters to remove the container after the script is done.
Is this possible?
Tim
You can map a volume of the Docker daemon to the container with the -v parameter and publish the artifacts to the daemon:
...
# Your build path and build command here
VOLUME /foo/build
ENTRYPOINT make
In TeamCity, configure a Docker build step to build the Dockerfile, and name:tag the resulting image. Add a second step in which you configure an other... Docker command as run, with the arguments:
-v /tmp/build:/foo/build --rm <name of image>
The result is then available in /tmp/build on the agent, and you can configure that as artifact path in the project's settings, or alternatively echo "##teamcity[publishArtifacts '/tmp/build']" somewhere.
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)