How to upgrade the docker container after its image changed?

Better Stack Team
Updated on April 7, 2023

To upgrade a Docker container after its image has changed, you can follow these steps:

  1. Stop the container using the following command:

     
    docker stop <container_name_or_id>
    

    Replace <container_name_or_id> with the name or ID of the container you want to upgrade.

  2. Remove the container using the following command:

     
    docker rm <container_name_or_id>
    

    This will remove the container and any data stored inside the container will be lost.

    Note that when you remove a container, any data stored inside the container is lost. To avoid losing data, you can use Docker volumes to store data outside the container. This way, when you create a new container using an updated image, you can attach the same volume to the new container and the data will still be available. To learn more about Docker volumes, you can check the official documentation.

  3. Pull the latest version of the image using the following command:

     
    docker pull <image_name>
    

    Replace <image_name> with the name of the Docker image you want to use to create the new container.

  4. Create a new container using the updated image and any desired options. For example, you can use the following command to create a new container from the updated image and map a port:

     
    docker run -p 8080:80 --name <new_container_name> <image_name>
    

    Replace <new_container_name> with the name you want to use for the new container and <image_name> with the name of the Docker image you want to use to create the container.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →