Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Docker: Change Container Run Options After Creation

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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 run command 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)

  1. Inspect the current container to capture its existing configuration. Run:
    docker inspect <container-name>
    Look for the Config section (environment variables, image, entrypoint) and HostConfig (ports, volumes, restart policy). Note the image name and any volumes.
  2. Stop the container to avoid conflicts:
    docker stop <container-name>
  3. Rename the old container (optional) so you can keep it as a backup:
    docker rename <container-name> <container-name>-old
  4. 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: -v for volumes, -e for environment variables, --restart for restart policy, etc. Do not forget the image name and tag.
  5. Verify the new container is running correctly:
    docker ps
    Check that the status is Up and the port mapping is as expected.
  6. Remove the old container once you're satisfied:
    docker rm <container-name>-old
    If you skipped renaming, you can remove the old container with docker 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)

  1. Create a docker-compose.yml file that defines your service. For example:
    version: '3'
    services:
      web:
        image: nginx
        ports:
          - "9090:80"
        environment:
          - FOO=bar
        restart: unless-stopped
  2. Start the container with Compose:
    docker-compose up -d
    This creates a container named <project>_<service>_1 (e.g., myproject_web_1).
  3. To change options, edit the docker-compose.yml file and run:
    docker-compose up -d
    Compose will detect the changes and recreate the container automatically with the new options.
  4. To stop and remove the container (without deleting volumes):
    docker-compose down
    Use docker-compose down -v only if you want to delete named volumes as well.

Common Mistakes

  • Assuming docker update can change everything. The docker update command 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 run command 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 ps to 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 Mounts section 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, use docker rm -f to 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 different container_name in 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.