0

I want to restart my docker container to apply new changes made. I dont want to create a new image each time i make changes rather that it will overwrite the old one and then restart the container using the rewritten one.

I have tried stopping the container, then building an image with the same name as the old one. i then write docker -compose up -d. Problem is this creates a new container called like -web

pvpb0t
  • 33
  • 6
  • 2
    Is this what you are looking for https://stackoverflow.com/questions/26734402/how-to-upgrade-docker-container-after-its-image-changed ? – ShubhamWagh Dec 27 '22 at 14:58
  • why you don't want to create a new image, if data is related, You can mount data to a volume, and creating a new image will make no changes for the user or for you, just apply a new configuration. maybe [this](https://docs.docker.com/storage/) can be helpful, And @WandererAboveTheSea link should be helpful as well, Thats exactly what you need. – George Dec 27 '22 at 15:03

1 Answers1

0

The standard approach is to build a new image, stop the existing server container, and then start a new container based on a new image. If you're using Docker Compose, this can be as little as

docker-compose build
docker-compose up -d

Compare this to running a server in a compiled language like Java or Go. Here you have the exact same workflow: compile your updated sources into a new jar file or binary, stop the existing server, and start a new one. There's not usually a reason to try to preserve the existing process and update it with new code.

In the same way, a container is a (temporary) wrapper around a single (temporary) process. There's no particular reason that you need the same container; in normal operation you should be able to delete and recreate a container without losing anything. In Docker you need to recreate a container fairly routinely to change various startup-time-only options, and also to update the underlying image. If you move on to clustered environments like Kubernetes this becomes even more important: you'll have multiple replicas of a container so they can't have persistent state, and it's possible for a container to be deleted and recreated outside of your immediate control.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • I figuered it was just better to link volumes and then just restart the docker container. Thanks anyways. – pvpb0t Dec 28 '22 at 15:44
  • Volumes are for keeping persistent _data_; your _code_ generally belongs in an immutable image. In either case, again, there's nothing especially valuable about the container that you can't delete and recreate it. – David Maze Dec 29 '22 at 10:59