Answer the question
In order to leave comments, you need to log in
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" ]
<?php
phpinfo();
docker build -t test-php .
docker run -d -p 6000:6000 --name test-php test-php
Answer the question
In order to leave comments, you need to log in
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/
<?php
phpinfo();
docker build -t test-php . && docker run -d -P --name test-php test-php
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
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 questionAsk a Question
731 491 924 answers to any question