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)
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.
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
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]
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.