Docker Community Forums

Share and learn in the Docker community.

How to rebuild docker container in docker-compose.yml

Hi, I am having trouble rebuilding my container with this command even after I updated one of the Dockerfiles. It could successfully built. Does anyone know what I can do to rebuild it?

docker-compose run --rm myService

When I re-ran the same command, it just returned the created network.
I tried again after removing the network but it was still the same.

[+] Running 1/0
 ⠿ Network myService_default  Created   

it seems to be able to rebuild with this command that I found on How to rebuild docker container in docker-compose.yml? - Stack Overflow?

docker-compose up -d --no-deps --build <service_name>

These were the commands that I ran:

1 docker-compose run --rm myService

I tried with these 2 commands to remove the network
2.1 docker-compose down
[+] Running 1/0
 ⠿ Network myService_default  Removed 
  
2.2 docker network rm networkName

3 docker-compose run --rm myService

It seems to me you have already solved it so what is the question?. You can also try docker-compose build

@rimelek
I am new to docker-compose and would like to learn the proper way to rebuild containers after docker-compose run.

Questions:

  1. Is the situation I faced is an expected situation?
  2. Is it the standard / expected way to rebuild the container after the first docker-compose?
docker-compose build
# or 
docker-compose up -d --no-deps --build <service_name>

Reason / Expectation:
I thought I should be able to run the same docker-compose run --rm myService command after running docker-compose down or docker remove networkName. Note that I also modified one of the Dockerfile before running docker-compose run --rm myService

After running docker-compose down or docker remove networkName, docker network ls doesn’t have that network. However, when I run docker-compose run --rm myService again, it just says

[+] Running 1/0
 ⠿ Network myService_default  Created   

docker-compose run is supposed to run a container. If the container requires to build an image, the run command also builds the container but it doesn’t have to rebuild the image. docker run does not build either. docker-compose up does everything so you have the option to rebuild.

When you run docker-compose down it doesn’t delete the image. You can delete that manually if you want. When you run docker network rm networkname it deletes the network but not the image. Docker Compose then creates the network because the container needs it but it is not required to rebuild the image which already exists.

I can see in your output that you use Docker Compose v2. V1 would also show you that it creates the container. I guess v2 doesn’t do it because it is obvious (well, I could argue).

So everything seems to work properly.

1 Like

Thank you for your explanation