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]
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'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?
I'm following this guide to set up a PHP development environment with Docker.
I have created a folder on my desktop docker-php and added a docker-compose.yml file into it, with this content:
nginx:
image: nginx:latest
ports:
- 80:80
On my terminal:
$ cd /home/my-username/Desktop/docker-php/
$ docker-compose up -d
I get this error:
ERROR: Couldn't connect to Docker daemon at
http+docker://localunixsocket - is it running?
If it's at a non-standard location, specify the URL with the
DOCKER_HOST environment variable.
I'm on Xubuntu 16.04.
Or perhaps I should put the folder in the specific location that is required by Docker? If so, which is it?
The most common reason for this error is that you ran 'docker-compose up' without sudo. As long as there is docker installed and is up and running, you are likely missing sudo in the docker command.
You could use native Docker
One option is to abandon docker-machine and use a native Docker setup on your system. Since you are on Linux (Xubuntu), this is an option for you. docker-machine is most often used by people who can't run Docker natively (Mac or Windows), and use it to install a Docker-capable VM and some local commands on their OS to talk to it.
You can find install docs for Docker on Linux here.
However, you already have docker-machine installed, so this may be the most disruptive option for you.
Your docker-machine may not be running
The error you are getting is saying the Docker client cannot talk to the server. One potential reason for this is that your docker-machine VM isn't running. You should verify it is running, and if not, start it.
To get a list of your docker-machines (may be one or more):
docker-machine ls
You will probably have one machine named default, but you may have more, depending on how you did your setup.
You can get the current status with:
docker-machine status <machine-name>
And you can use stop, start, restart to manage the docker-machine.
(More in the Docker Machine CLI reference.)
You need the proper environment set
docker-machine relies on environment variables to work properly. Because you may have multiple docker-machine setups, you have to tell the client which one to use.
To set the environment, you can get it from the docker-machine command.
docker-machine env <machine-name>
And you can automatically inject it into the environment (this may be a useful thing to put into your shell startup file).
eval "$(docker-machine env <machine-name>)"
You should end up with env vars similar to these:
DOCKER_HOST=tcp://192.168.99.101:2376
DOCKER_CERT_PATH=/Users/nathanleclaire/.docker/machines/.client
DOCKER_TLS_VERIFY=1
DOCKER_MACHINE_NAME=dev
Keep in mind you should use the eval form here, not just run the env command and paste the output into your shell setup; it may change on a docker-machine restart, etc, so you can't rely on an old setup to still work later.
If your docker-machine is running, and these env vars are set, your docker and docker-compose commands should work.
Solution:
sudo usermod -a -G docker USERNAME
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)