Home > Enterprise >  How to Recreate a Docker Container Without Docker Compose
How to Recreate a Docker Container Without Docker Compose

Time:05-26

TLDR: When using docker compose, I can simply recreate a container by changing its configuration and/or image in the docker-compose.yml file along with running docker-compose up. Is there any generic equivalent for recreating a container (to apply changes) which was created by a bare docker create/run command?


Elaborating a bit:

The associated docker compose documentation states:

If there are existing containers for a service, and the service’s configuration or image was changed after the container’s creation, docker-compose up picks up the changes by stopping and recreating the containers (preserving mounted volumes).

I'm having troubles to understand which underlaying steps are actually performed during this recreation, as e.g. the docker (without compose) documentation doesn't really seem to use the recreate term at all.

Is it safe to simply run docker container rm xy and then docker container create/run (along with passing the full and modified configuration)? Or is docker compose actually doing more under the hood?

I already found answers about applying specific configuration changes like e.g. this one about port mappings, but I'm still wondering whether there is a more general answer to this.

CodePudding user response:

I'm having troubles to understand which underlaying steps are actually performed during this recreation, as e.g. the docker (without compose) documentation doesn't really seem to use the recreate term at all.

docker-compose is a high level tool; it performs in a single operation what would require multiple commands using the docker cli. When docker-compose says, "docker-compose up picks up the changes by stopping and recreating the containers", it means it is doing the equivalent of:

docker stop <somecontainer>
docker rm <somecontainer>
docker run ...

(Where ... represents whatever configuration is implied by the service definition in your docker-compose.yaml).

CodePudding user response:

Let's say it recognizes a change in container1 it does (not really, working via API):

docker compose rm -fs container1
docker compose create (--build) container1
docker compose start container1

What is partially close to (depending on your compose-config):

docker rm -f projectname_container1
(docker build --flags)
docker create --allDozensOfAttributes projectname_container1
docker start  projectname_container1
docker network connect (--flags) projectname_networkname projectname_container1
and maybe more..

so i would advise to use the docker compose commands for single services instead of docker cli if suitable..

  • Related