346

There are scope of services which defined in docker-compose.yml. These service have been started. I need to rebuild only one of these and start it without up other services. I run the following commands:

docker-compose up -d # run all services
docker-compose stop nginx # stop only one. but it still running !!!
docker-compose build --no-cache nginx 
docker-compose up -d --no-deps # link nginx to other services

At the end i got old nginx container. By the way docker-compose doesn't kill all running containers !

yuklia
  • 4,387
  • 4
  • 16
  • 24

10 Answers10

380

docker-compose up

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

--no-deps - Don't start linked services.

--build - Build images before starting containers.

phoenix
  • 3,988
  • 1
  • 29
  • 33
denov
  • 7,064
  • 2
  • 23
  • 36
  • 33
    `docker-compose build service1 service2 --no-cache` does this job better (https://github.com/docker/compose/issues/1049). I had issue with cached containers that were built before using `docker-compose up -d --no-deps --build ` – evgpisarchik May 31 '17 at 20:37
  • 59
    @evgpisarchik The orders a bit out, it should be `docker-compose build --no-cache service1 service2` – Matthew Wilcoxson Mar 27 '18 at 08:10
284

With docker-compose 1.19 up

docker-compose up --build --force-recreate --no-deps [-d] [<service_name>..]

Without one or more service_name arguments all images will be built if missing and all containers will be recreated.

From the help menu

Options:
    -d, --detach        Detached mode: Run containers in the background,
                        print new container names. Incompatible with
                        --abort-on-container-exit.
    --no-deps           Don't start linked services.
    --force-recreate    Recreate containers even if their configuration
                        and image haven't changed.
    --build             Build images before starting containers.

Without cache

To force a rebuild to ignore cached layers, we have to first build a new image

docker-compose build --no-cache [<service_name>..]

From the help menu

Options:
    --force-rm              Always remove intermediate containers.
    -m, --memory MEM        Set memory limit for the build container.
    --no-cache              Do not use cache when building the image.
    --no-rm                 Do not remove intermediate containers after a successful build.

Then recreate the container

docker-compose up --force-recreate --no-deps [-d] [<service_name>..]
Scott Stafford
  • 40,202
  • 22
  • 116
  • 163
HarlemSquirrel
  • 6,098
  • 4
  • 26
  • 30
  • 4
    I'm confused how this answers the question. How do we rebuild just the one container? – Duncan Jones Feb 08 '19 at 20:58
  • 14
    You can rebuild only one container by appending it's name to the end of the command. `docker-compose up -d --force-recreate --build container_name` – HarlemSquirrel Feb 09 '19 at 02:25
  • Wait a sec, this *recreates **container***, it is not equivalent to `build --no-cache` option, unless the command description is completely wrong – Slav Mar 28 '20 at 05:58
77

This should fix your problem:

docker-compose ps # lists all services (id, name)
docker-compose stop <id/name> #this will stop only the selected container
docker-compose rm <id/name> # this will remove the docker container permanently 
docker-compose up # builds/rebuilds all not already built container 
tristanbailey
  • 3,945
  • 1
  • 22
  • 29
ArgonQQ
  • 1,671
  • 1
  • 8
  • 12
  • 16
    if you want to `stop` and `rm` all containers then you can use the command `docker-compose down`. Your solution is better if you only want to get rid of some. – hirowatari Oct 01 '16 at 17:43
  • 1
    This solution is a good fit for the setup where you don't want to stop the entire docker application and just recreate one of the services – bluesummers Aug 13 '19 at 13:00
  • 3
    I needed to actually run `docker-compose build ` before `up` to rebuild the changed Dockerfile. – Vajk Hermecz Feb 20 '20 at 22:58
60

As @HarlemSquirrel posted, it is the best and I think the correct solution.

But, to answer the OP specific problem, it should be something like the following command, as he doesn't want to recreate ALL services in the docker-compose.yml file, but only the nginx one:

docker-compose up -d --force-recreate --no-deps --build nginx

Options description:

Options:
  -d                  Detached mode: Run containers in the background,
                      print new container names. Incompatible with
                      --abort-on-container-exit.
  --force-recreate    Recreate containers even if their configuration
                      and image haven't changed.
  --build             Build images before starting containers.
  --no-deps           Don't start linked services.
Slavik Meltser
  • 7,008
  • 2
  • 37
  • 37
21

Maybe these steps are not quite correct, but I do like this:

stop docker compose: $ docker-compose down

remove the container: $ docker system prune -a

start docker compose: $ docker-compose up -d

Sergey Ivchenko
  • 872
  • 9
  • 8
  • 12
    `docker system prune -a` will delete all images, even ones for other projects. I would recommend against using this. – aL_eX Sep 22 '20 at 17:58
  • aL_eX, flag -a (means all) you can select only the one you need by specifying its id – Sergey Ivchenko Sep 25 '20 at 10:27
  • 2
    @aL_eX I agree this method is somewhat dangerous. But this is working for sure. Other answers do not remove images. – glinda93 Dec 04 '20 at 01:01
7
docker-compose stop nginx # stop if running
docker-compose rm -f nginx # remove without confirmation
docker-compose build nginx # build
docker-compose up -d nginx # create and start in background

Removing container with rm is essential. Without removing, Docker will start old container.

Nex
  • 303
  • 3
  • 9
5

Simply use :

docker-compose build [yml_service_name]

Replace [yml_service_name] with your service name in docker-compose.yml file. You can use docker-compose restart to make sure changes are effected. You can use --no-cache to ignore the cache.

FarhadMohseni
  • 166
  • 2
  • 6
4

The problem is:

$ docker-compose stop nginx

didn't work (you said it is still running). If you are going to rebuild it anyway, you can try killing it:

$ docker-compose kill nginx

If it still doesn't work, try to stop it with docker directly:

$ docker stop nginx

or delete it

$ docker rm -f nginx

If that still doesn't work, check your version of docker, you might want to upgrade.

It might be a bug, you could check if one matches your system/version. Here are a couple, for ex: https://github.com/docker/docker/issues/10589

https://github.com/docker/docker/issues/12738

As a workaround, you could try to kill the process.

$ ps aux | grep docker 
$ kill 225654 # example process id
user2707671
  • 1,475
  • 12
  • 11
3

For me it only fetched new dependencies from Docker Hub with both --no-cache and --pull (which are available for docker-compose build.

# other steps before rebuild
docker-compose build --no-cache --pull nginx # rebuild nginx
# other steps after rebuild, e.g. up (see other answers)
kmindi
  • 3,346
  • 3
  • 26
  • 44
-12

Only:

$ docker-compose restart [yml_service_name]
  • 5
    this will not build new changes. from the manual - If you make changes to your docker-compose.yml configuration these changes will not be reflected after running this command. – denov Mar 10 '17 at 22:00