N
N
NkDev2018-03-13 12:25:05
linux
NkDev, 2018-03-13 12:25:05

How to start a container via docker-compose.yml so that it doesn't stop immediately after creation?

There are 2 files in the folder:
1 file: docker-compose.yml

nginx:
    build: ./

2 file: Dockerfile
FROM centos

RUN rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
RUN yum -y install nginx

CMD nginx && bash

if i run commands
docker build -t img_nginx .
docker run --name cont_nginx -i -t img_nginx

then the container is created and launched
. However, if I run the
docker-compose up -d --build
Then command, the container is also created, but does not start. Moreover, I can't connect to it using docker exec -i -t cont_nginx bash
Tell me what's the matter and how can I make the container start immediately after running docker-compose up -d
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mureevms, 2018-03-13
@NkDev

expected behavior. This is a docker feature. If the process terminates, then the container stops because its task is finished. If you want the container to work, then you need to start the service inside it, for example, just try it CMD ping 127.0.0.1and then check if the container is running. docker-compose ps
In your case, you need to start nginx like this. You can CMD ["nginx", "-g", "daemon off;"]
connect to a running container like this docker exec -it NAME bash, where NAME is the name of the container from docker-compose ps.
Better yet, style your docker-compose.yml properly

E
evgesha3215, 2018-03-19
@evgesha3215

What does CMD ["nginx", "-g", "daemon off;"] mean in this case?
Is it possible to do the same for apache2 by analogy?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question