41

I have a container in my stack that needs to be recreated each time I docker-compose up. I can docker-compose up --force-recreate but this recreates all my containers. Is there syntax (maybe for the docker-compose.yml file) for specifying this kind of flag per-service?

2
  • Is this in swarm mode? docker-compose up doesn't work with swarm mode. Commented Nov 2, 2017 at 18:38
  • No, this is just straight docker-compose, sorry for the confusion, I've edited the question. Commented Nov 3, 2017 at 10:58

3 Answers 3

62

If your service depends on (or links to) other services, you can try:

docker-compose up --force-recreate --no-deps service-name

This will only recreate the specified service, linked or depended services will keep untouched.

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

2 Comments

Note - service-name is name defined in docker-compose.yml (so php, not project_php_1).
docker compose up -d --force-recreate --no-deps service-name Worked for me thanks.
0

to rebuild only 'mysql' service, run this command

docker-compose up --force-recreate --no-deps mysql

2 Comments

What does it add to the earlier, most upvoted answer?
Its just add some more explanation with an example service i-e "mysql".
0

You can simply remove the old container before running services, which means that new container will be created.

docker container rm <container-name> && docker-compose up

You can find <container-name> by running docker ps -a. As far as you don't change directory name or project name, container name should stay the same.

Note that after removing the container its named volumes will still be preserved.

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.