docker compose build single container

Using Compose, if I run docker-compose build, it will rebuild all the containers :

> docker-compose build
Building elasticsearch
Step 1 : FROM elasticsearch:2.1
 ---> a05cc7ed3f32
Step 2 : RUN /usr/share/elasticsearch/bin/plugin install analysis-phonetic
 ---> Using cache
 ---> ec07bbdb8a18
Successfully built ec07bbdb8a18
Building search
Step 1 : FROM php:5.5.28-fpm
 ---> fcd24d1058c0
...

Even when rebuilding using cache, this takes time. So my question is:

Is there a way to rebuild only one specific container?


Solution 1:

Yes, use the name of the service:

docker-compose build elasticsearch

Solution 2:

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

Source

Solution 3:

if you want to run and recreate a specific service inside your docker-compose file you can do it the same way as @dnephin proposed, like

$ docker-compose up -d --force-recreate --no-deps --build service_name

Suppose your docker-compose.yml file is like

version: '3'
services:
  service_1:
      .....
  service_2:
      .....

Solution 4:

You could added --no-start flag to docker-compose, and start later since you will only build one service.