M
M
medoedoff2019-01-16 14:48:14
Nginx
medoedoff, 2019-01-16 14:48:14

Running services in docker container?

Hello everyone, I'm rolling my nginx + php image. I just started learning docker. I created my own docker image with nginx + php, registered all the configurations. I start the docker image with the command
docker run -p 8080:80 -ti nginx-php-1 bash
, I start the nginx and php services there, I go through the host to 127.0.0.1:8080, the page with phpinfo is rendered, everything works.
Now, through the dockerfile, I want these services to start automatically and the container to run in the background. dockerfile

FROM nginx-php-1

MAINTAINER I am

CMD /etc/init.d/php7.2-fpm start && /etc/init.d/nginx start

I'm building an image, I'm trying to run it, but it doesn't start, please help me how to start the services correctly so that the container works.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
ivankomolin, 2019-01-19
@medoedoff

Doing so is fundamentally wrong.
A Docker container is alive as long as the first process running in it is alive.
Theoretically, you can start several processes, but only from the first one. And when the first process is finished, the container will stop its work.
In order to start both services, you can use for example supervisor.
Run it as the first process, and nginx and fpm will already launch the supervisor itself and at the same time control their work.
More information about running multiple services in one container is written here:
https://docs.docker.com/config/containers/multi-se...
But there is an even more correct way - to run each service in a separate container.
To do this, dig towards docker-compose

S
Saboteur, 2019-01-16
@saboteur_kiev

IMHO the CMD format is not the same as that of RUN, adding two commands to it is problematic.
It is better to add a shell script to the container that will launch fpm and nginx, and write in CMD:
CMD [ "/etc/init.d/myscript" , "start"]

D
Dmitry, 2019-01-16
@q2digger

grab the official nginx Dockerfile and see how it's done right.
https://github.com/nginxinc/docker-nginx/blob/mast...
In short. nginx is run as a process, not as a service.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question