Docker: Change Container Run Options After Creation
In this article
Docker containers are immutable at the configuration level: you cannot edit the run options of an existing container. This guide shows you the two reliable ways to apply new run options — recreate the container with the same image and volumes, or use a Docker Compose file to manage the container declaratively. You'll need Docker installed and a basic understanding of the command line.
What You'll Need
- Docker installed on your system (Docker Engine 19.03 or later is recommended).
- Access to a terminal or command prompt.
- The name or ID of the container you want to modify.
- The original
docker runcommand you used (if available) or a Docker Compose file if you used one. - Knowledge of the new options you want to apply (e.g., port mappings, environment variables, volumes, restart policies).
Step-by-Step
There is no docker update command that can change mount, port, or environment settings. The only way to apply new run options is to recreate the container. Here's how to do it safely.
Method 1: Recreate the Container (Manual)
- Inspect the current container to capture its existing configuration. Run:
docker inspect <container-name>
Look for theConfigsection (environment variables, image, entrypoint) andHostConfig(ports, volumes, restart policy). Note the image name and any volumes. - Stop the container to avoid conflicts:
docker stop <container-name> - Rename the old container (optional) so you can keep it as a backup:
docker rename <container-name> <container-name>-old - Create a new container with the same image and the new options. For example, if you originally ran:
docker run -d --name myapp -p 8080:80 nginx
and you want to change the port to 9090, run:docker run -d --name myapp -p 9090:80 nginx
Include all the options you need:-vfor volumes,-efor environment variables,--restartfor restart policy, etc. Do not forget the image name and tag. - Verify the new container is running correctly:
docker ps
Check that the status is Up and the port mapping is as expected. - Remove the old container once you're satisfied:
docker rm <container-name>-old
If you skipped renaming, you can remove the old container withdocker rm <container-name>after you've stopped it, but be careful: you won't have a backup.
Method 2: Use Docker Compose (Recommended for Complex Configs)
- Create a
docker-compose.ymlfile that defines your service. For example:version: '3' services: web: image: nginx ports: - "9090:80" environment: - FOO=bar restart: unless-stopped - Start the container with Compose:
docker-compose up -d
This creates a container named<project>_<service>_1(e.g.,myproject_web_1). - To change options, edit the
docker-compose.ymlfile and run:docker-compose up -d
Compose will detect the changes and recreate the container automatically with the new options. - To stop and remove the container (without deleting volumes):
docker-compose down
Usedocker-compose down -vonly if you want to delete named volumes as well.
Common Mistakes
- Assuming
docker updatecan change everything. Thedocker updatecommand only adjusts resource limits (CPU, memory) and restart policies. It cannot change ports, volumes, or environment variables. - Forgetting to include all original options when recreating. If you omit a volume or environment variable, the new container will lose that configuration. Always inspect the old container first.
- Reusing the same container name without removing the old one. Docker will throw an error like Conflict. The container name "/myapp" is already in use. You must remove or rename the old container first.
- Not backing up data. If your container uses a named volume, the data persists even after container removal. But if you used an anonymous volume (without a name), it may be lost when you remove the container. Use named volumes or bind mounts to protect data.
Tips
- Use named volumes (e.g.,
myvol:/data) instead of anonymous volumes so data survives container recreation. - Export the current container's config to a YAML file for reference:
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock \ ghcr.io/red5d/docker-autocompose \ <container-name> > docker-compose.yml
This generates a Compose file from an existing container, which you can then edit and use. - Use Docker Compose from the start for any container with more than a couple of options. It makes future changes trivial.
- Test the new container before removing the old one. You can run the new one on a different port or with a different name to verify it works.
- Keep your
docker runcommand in a script or a Makefile so you can easily recreate the container with modifications.
Troubleshooting
- Port already in use: If you try to map a port that's already taken, Docker will fail. Use
docker psto see what's using the port, or change the host port. - Container starts but immediately exits: Check the logs with
docker logs <container-name>. This often indicates a missing environment variable or volume path. - Volume data appears missing: If you used an anonymous volume, the data is lost when the container is removed. Recover it by inspecting the old container's
Mountssection and copying the data out before removal. - Cannot remove the old container: If it's still running, stop it first with
docker stop. If it's stuck, usedocker rm -fto force removal. - Compose says "container name already in use": This happens when a container with the same name exists outside of Compose. Remove it with
docker rm <name>or use a differentcontainer_namein the Compose file.
Frequently Asked Questions
Can I change the port mapping of an existing container?
No, port mappings are set at creation time. You must recreate the container with the new -p option.
Can I add a new environment variable to a running container?
No. Environment variables are immutable after creation. You need to recreate the container with the new -e flags.
Does docker update allow me to change memory or CPU limits?
Yes, docker update can change memory, CPU shares, and restart policy on a running container. For example: docker update --memory 512m myapp.
Will I lose my data if I recreate the container?
It depends on how you store data. If you use a named volume or a bind mount, data persists. If you use an anonymous volume, it will be lost when the container is removed unless you back it up first.
What is the difference between docker-compose up -d and docker run?
Compose is a declarative tool that reads a YAML file and manages the container lifecycle. It automatically recreates containers when the file changes, making it easier to update options. docker run is imperative and requires manual recreation.
Can I change the image of a running container?
No, the image is fixed at creation. You must create a new container from the new image and migrate your data.