Set CMD docker container without rebuilding - php

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

Related

Is there anyway to start docker container without a service?

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.

Website hosted in docker container is not updating live

I am developping a website which I want to host inside a php-apache docker container.
I use the following command to run the container:
docker run -dit --restart unless-stopped --name my_www -p 8080:80 -v /path/to/repo:/var/www/html/ php:7.4-apache
Since I bind the repository containing the code as a volume to the container, I expect the website to "update live" when I change the code locally. I had this right behaviour last time I tried but I am now unable to get it back.
When I check the website locally at 127.0.0.1 everything is ok and changes are taken into account normaly, but they do not propagate into the docker container...
For some reason, the files in the docker are stuck to an old version of the code, an old "stat" of the repository...
Any ideas how I can manage to fix this and preview changes live ?
Credits to #Don't Panic for helping to debug.
The browser was caching everything so I couldn't see changes live.
The solution was to enable the "expires" apache module inside the docker container:
$ docker exec -it <container_id> bash
root#<container_id>:# a2enmod expires
root#<container_id>:# exit
$ docker restart my_www
Et voilà :)

Run apache/php/nginx in background Docker container

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.

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)

Running package manager inside the docker

I've built an image for the purpose of PHP development, and it became clear to me that I didn't really thought about how to access the tools that I need for every day development. For example: composer, package manager for PHP, I need it to run whenever composer.json updates. I thought it is worth installing those tools inside the same image, but then I don't have a way to access them. So, I can:
Create separate image for composer and run it in different container
Install composer on my host machine.
I'd like to avoid option 2), but then, does it have sense having a setup like 1) ? How did you guys solved this issue ?
Unless you have some quite specific requirements there is a third option:
Connect to the container using docker exec command:
docker exec -it CONTAINER-NAME/ID COMMAND [ARG...]
Here is the example:
1: Create your application:
echo "<?php phpinfo();" > index.php
2: Start container:
docker run -it --rm --name my-apache-php-app -p 80:80 -v "$PWD":/var/www/html php:5.6-apache
3: Open another terminal window and exec required commands inside running container:
docker exec -it my-apache-php-app curl -sS https://getcomposer.org/installer | php
docker exec -it my-apache-php-app ls
If you need shell inside running container - run:
docker exec -it my-apache-php-app bash
That's it!

Categories