How to force-recreate and start only one container under docker-compose?

After I make any code change, my typical development steps are:

  1. Change code and build the jar file
  2. Build docker image that consumes the jar file
  3. Kill current "docker-compose up" command.
  4. Run "docker-compose up" again.

My docker-compose file has five services. On step 3, all the containers go down. Ideally, I just need to re-run my app container.

I am looking for a way to do the following in a batch script:

  1. Bring down the app container. The other four containers should continue to run.
  2. Build a new docker image
  3. Force docker-compose to recreate my app container and start it.

For step 1, looks like I can use "docker-compose kill myappname."

Step 2 is also straightforward.

I am wondering how I can accomplish step 3. Thanks.

>Solution :

You don’t need to stop the current container explicitly.

If your image has changed, docker compose will recognize it and recreate your container, when you run the up command again.

So, rebuild your image and run docker compose up.

If you use compose to build the image, add the --build flag to let it rebuild your image, after which the container is also recreated.

You cann also add the name off a specific service to your up command, i.e. docker compose up -d --build app

If the image hasnt changed, you cann add the --force-recreate flag.

Leave a Reply