Docker - Change existing containers settings without RUN command

Viewed 78

Is it possible to change the settings of docker container like entrypoint, ports or memory-limits without having to delete the container and run using docker run command? Example: docker stop <container_id>, change settings and then docker start <container_id>?

When you use docker run -d image_name, some images tries to initialize from start and as a result I can't use the same volume.

Is it possible to change the settings by stopping the container instead of re-run?

1 Answers

You need to stop, delete, and recreate the container.

# this is absolutely totally 100% normal and routine
docker stop my_container
docker rm my_container
# docker build -t image_name .
docker run -d -p 12345:8000 --name my_container image_name

This isn't specific to Docker. If you run any command in any Unix-like environment and you want to change its command-line parameters or environment variables, you need to stop the process and create a new one. A Docker container is a wrapper around a process with some additional isolation features, and for a great many routine things you're required to delete the container. In cluster container environments like Kubernetes, this is routine enough that changing any property of a Deployment object will cause all of the associated containers (Kubernetes Pods) to get recreated automatically.

There are a handful of Docker commands that exist but are almost never used in normal operation. docker start is among these; just skip over it in the documentation.

When you use docker run -d image_name, some images tries to initialize from start and as a result I can't use the same volume.

In fact, the normal behavior of docker run is that you're always beginning the program from a known "clean" initial state; this is easier to set up as an application developer than trying to recover from whatever state the previous run of the application might have been left in.

If you need to debug the image startup, an easy thing to do is to tell the container to run an interactive shell instead of its default command

docker run --rm -it image_name /bin/sh

(Some images may have bash available which will be more comfortable to work in; some images may require an awkward docker run --entrypoint option.) From this shell you can try to manually run the container startup commands and see what happens. You don't need to worry about damaging the container code in any particular way, since anything you change in this shell will get lost as soon as the container exits.

Related