3

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?

tryingHard
  • 1,794
  • 4
  • 35
  • 74
  • 2
    why you are not mounting the config? `docker run -it -v $PWD/myconfig:/etc/nginx/conf.d/main.conf nginx:alpine` – Adiii Nov 12 '19 at 10:39
  • I am creating custom image (copying files) configuration comes from external source. But to do fastly some testing i just wanted to test it inside the container only. Mabe you are right that the config should be from volume here. – tryingHard Nov 12 '19 at 10:49
  • So update config inside container and rerun the container will tak less time, you do not need to build the container, just kill old one start new with new config `docker run -it -v $PWD/myconfig:/etc/nginx/conf.d/main.conf nginx:alpine` – Adiii Nov 12 '19 at 10:51
  • Additionaily i load two directories `upstream` and `locations` so it's not that easy as changing one file but maybe changing whole folder would do the job. – tryingHard Nov 12 '19 at 10:54

2 Answers2

2

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

LinPy
  • 16,987
  • 4
  • 43
  • 57
  • Can you copy file from/to not running container? – tryingHard Nov 12 '19 at 10:40
  • Thanks, is there possibility to start container (via `docker start`) and force it to remain running? – tryingHard Nov 12 '19 at 10:43
  • if the image using entrypoint , then yes using the same method above – LinPy Nov 12 '19 at 10:44
  • I am not sure what do you mean. Can you clarify how can I force for example nginx container to stay alive if it has wrong configuration? Can you edit your answer elaborating about it? For example you execute `docker run nginx` then you go inside container make some mistake in configuration. Then you make docker stop/start nginx. And now how can I force it to stay alive? – tryingHard Nov 12 '19 at 10:46
  • 1
    see this https://stackoverflow.com/questions/58500866/how-to-restart-an-existing-mongodb-docker-container-with-a-new-flags-to-daemon/58501700#58501700 – LinPy Nov 12 '19 at 10:47
  • then you can add maybe tail -f /dev/null instaead of exec – LinPy Nov 12 '19 at 10:48
1

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.

David Maze
  • 130,717
  • 29
  • 175
  • 215