1

I am using docker-compose for a development project. I have 6 services defined in my docker compose file. I have been using the below script to rebuild the images whenever I make a change.

#!/bin/bash
# file: rebuild.sh
docker-compose down
docker-compose build
docker-compose up

I am looking for a way to reduce the build time as building and restarting all the services seems unnecessary as I am usually only changing one module. I see in the docker-compose docs you can run commands for individual services by specifying the service name after e.g. docker-compose build myservice.

In another terminal window I tried docker-compose build myservice && docker-compose restart myservice while leaving the other ./rebuild.sh command open in the original terminal. In the ./rebuild.sh terminal window I see all the initialization messages being reprinted to the stdout so I know it is restarting that service but the code changes aren't there. What am I doing wrong? I just want to rebuild and restart a single service.

7
  • You rebuild the image to feed container with data? Why don’t you mount volumes instead? Commented Feb 12, 2019 at 18:47
  • I know about bind volumes where you can point to your source code directory but I for simple projects I just wanted to know if there was a simple way to rebuild/restart a single service. Trying not to get too complicated. Commented Feb 12, 2019 at 18:53
  • Did you think about use arguments in your rebuild script? Something like ./rebuild.sh myservice. If you want to do that you can use $1 variable to read the name of your service received as parameter. Commented Feb 12, 2019 at 18:58
  • The docker-compose up and down commands don't take a service name as an argument. That's why I was trying to use docker-compose restart. I also tried docker-compose stop/start but it's effectively the same as restart. Commented Feb 12, 2019 at 19:11
  • Try docker-compose up -d --force-recreate --build myservice . Note that -d is for Detached mode, -d is for force recreation even is your code did not change, -build is for build your images before starting and at least the name of your service. Read more here docs.docker.com/compose/reference/up Commented Feb 12, 2019 at 19:16

1 Answer 1

2

Try:

docker-compose up -d --force-recreate --build myservice

Note that: -d is for Detached mode, -force-recreate will recreate containers even is your code did not change, -build is for build your images before starting containers. At least the name of your service.

Take a look here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.