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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github