A
A
Alexander Bulatov2021-01-22 12:56:44
PHP
Alexander Bulatov, 2021-01-22 12:56:44

How to make a Docker container always running?

Good day!

The situation is this.

Wrote a Dockerfile with the following content:

FROM php:apache
COPY . /app
WORKDIR /app
ENTRYPOINT [ "php", "./index.php" ]
CMD [ "apache2ctl", "-D FOREGROUND" ]


And I placed the index.php file in the directory with this file, just with the phpinfo command:
<?php
phpinfo();


Launched the construction of this image via the console: Then launched the container
docker build -t test-php .

docker run -d -p 6000:6000 --name test-php test-php


And then, I would like to open the page localhost:6000 in the browser , but the page is not displayed. I'm just learning it (Docker) and I would like to understand how to start the container correctly so that it is constantly turned on and I can open the page.

I would be grateful for any thought on this matter or an indication of where to look.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Bulatov, 2021-02-01
@alexanderbulatov

The solution turned out to be this.
Create the following file structure:
Public-html directory with a single index.php
file Dockerfile
Dockerfile contents:

FROM php:apache
COPY ./public-html/ /var/www/html/

index.php content:
<?php

phpinfo();

And run the commands in the console:
docker build -t test-php . && docker run -d -P --name test-php test-php

It's just that the default directory in the parent image where the input script should be is empty. The solution is to simply place your entry script in this directory.

K
Konstantin Lizunov, 2021-01-22
@LKIkost

With this command, you created a container and launched it at the same time.
docker run -d -p 6000:6000 --name test-php test-php
I suspect you've created a bunch of containers already. The command is run only once.
This command will start the already created container and that's it - it works and works all the time until it is stopped.
docker start test-php
Next, we need to enter the container to set it up.
docker exec -it test-php bin/bash
Here you don’t have php running, it would be nice to add autostart to the build. If php, then I work with nginx, you need Apache and mysql, I also add it here, it looks like this for me, you need to fix it a little. Again, I'm setting up my docker from under Ubuntu and I don't know what image comes with php:apache, maybe I'm fundamentally wrong.
This is an autostart, write it at the end of the docker file:

RUN touch /start
RUN chmod +x /start
RUN echo '#!/bin/bash' >> /start
RUN echo "service nginx start" >> /start
RUN echo "service mysql start" >> /start
RUN echo "service php7.4-fpm start" >> /start

You can just start php with the service php7.4-fpm start command if you have a 7.4 version, as in a normal system, if I understand everything correctly. I don’t know what kind of image you have, that’s the thing, I have never worked with this, I wrote for ubunta, commands for it actually.

R
Ruslan, 2021-01-24
@Tiasar

The container lives exactly as long as the process running in it with the ENTRYPOINT [ "php", "./index.php" ] command is alive, as soon as your index.php script finishes running, the container stops.
What problem do you want to solve? It depends on how to do it right.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question