tryingHard
tryingHard

Reputation: 2104

Force container to stay alive when using docker start command

For example i use nginx container and i go inside this container to change config of it. Then i stop and start it to let it reload but it immediately shuts down.

For example logs give me:

nginx: [emerg] unknown log format "upstream_time" in /etc/nginx/conf.d/main.conf:17

Now i know how can i fix it, but i have to access this container, to be able to access it this container must be running.

How can i force docker to start container using docker start command (not docker run) and force it to stay alive?

Upvotes: 3

Views: 749

Answers (2)

David Maze
David Maze

Reputation: 159965

A Docker container wraps a single process. If you do something to cause that process to exit, the container will always exit, and there's not really any way around this.

In general you shouldn't be directly editing files inside containers. Most critically, any change you make this way will be lost as soon as the container is deleted, and deleting containers is an extremely routine operation. Many containers also just don't contain an editor you can use.

In between @LinPy's answer and @Adiii's comment is a best practice. Start by copying the existing configuration file out of the container, if you don't already have it.

docker cp my_CON:/etc/nginx/conf.d/main.conf .

Now make whatever edits you need to this file. Stop and delete the existing container, then restart it with this modified config file pushed in as a bind-mounted volume.

docker stop my_CON
docker rm my_CON
docker run \
  --name my_CON \
  -v $PWD/main.conf:/etc/nginx/conf.d/main.conf \
  -p ... \
  nginx:alpine

In principle you can directly edit the file on your host and changes will be reflected in the container. (There are many SO reports of this not working reliably.) If the server live-reloads the config and exits when it's wrong, you can fix the config and re-run the same docker run command to get it running again.

Volume mounts like this are good for pushing config files in. You can mount an empty directory over /var/log or somewhere similar to get log files out. They're required to keep any data that needs to persist across container runs. In the case of nginx it's reasonable enough to call the static HTML/JS/CSS/... content you need to serve "data" and inject it this way.

Upvotes: 1

LinPy
LinPy

Reputation: 18618

you can copy the file to your localhost then edit it :

docker cp my_CON:/etc/nginx/conf.d/main.conf .

after edit re-copy to container:

docker cp  main.conf my_CON:/etc/nginx/conf.d/main.conf

then start it again

Upvotes: 2

Related Questions