5

This is a section of my docker-compose file:

version: "3.2"
services:
  cachable_service:
    image: my/cachable_service:x.x
    build:
      context: .
      dockerfile: cachable_service_Dockerfile

  service_in_development:
    image: my/service_in_development:latest
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      - cachable_service

How do I force only the image for the service_in_development to be always build from scratch so I get my latest code in there?

I've looked into the cache_from option in the docker-compose file reference, but I couldn't find any clear explanation on what exactly it does.

Clarifications:

In this question, I was asking about an option to force rebuild only specific services using an option in the docker-compose.yml file itself. But, if a commandline option let's me do this, I'd be happy with that as well for now.

Also, I'm talking about forcing a 'rebuild' of images. Not just 'recreation' of service.

nemo
  • 1,504
  • 3
  • 21
  • 41
  • Possible duplicate of [How to rebuild docker container in docker-compose.yml?](https://stackoverflow.com/questions/36884991/how-to-rebuild-docker-container-in-docker-compose-yml) – Krzysztof Raciniewski Mar 28 '19 at 20:01

1 Answers1

1

You can use command below to rebuild all images:

docker-compose up --build --force-recreate

to force rebuild one service:

docker-compose up --build --force-recreate service-name-here

More useful parameters in documentation

Usage: up [options] [--scale SERVICE=NUM...] [SERVICE...]

Options:
    -d, --detach               Detached mode: Run containers in the background,
                               print new container names. Incompatible with
                               --abort-on-container-exit.
    --no-color                 Produce monochrome output.
    --quiet-pull               Pull without printing progress information
    --no-deps                  Don't start linked services.
    --force-recreate           Recreate containers even if their configuration
                               and image haven't changed.
    --always-recreate-deps     Recreate dependent containers.
                               Incompatible with --no-recreate.
    --no-recreate              If containers already exist, don't recreate
                               them. Incompatible with --force-recreate and -V.
    --no-build                 Don't build an image, even if it's missing.
    --no-start                 Don't start the services after creating them.
    --build                    Build images before starting containers.
    --abort-on-container-exit  Stops all containers if any container was
                               stopped. Incompatible with -d.
    -t, --timeout TIMEOUT      Use this timeout in seconds for container
                               shutdown when attached or when containers are
                               already running. (default: 10)
    -V, --renew-anon-volumes   Recreate anonymous volumes instead of retrieving
                               data from the previous containers.
    --remove-orphans           Remove containers for services not defined
                               in the Compose file.
    --exit-code-from SERVICE   Return the exit code of the selected service
                               container. Implies --abort-on-container-exit.
    --scale SERVICE=NUM        Scale SERVICE to NUM instances. Overrides the
                               `scale` setting in the Compose file if present.
Krzysztof Raciniewski
  • 4,735
  • 3
  • 21
  • 42
  • When I use `docker-compose up --build --force-recreate service-name-here`, it's building all the containers despite my specifying a service name. – nemo Mar 30 '19 at 19:16
  • Maybe you have linked services in the docker-compose.yml file. Add `--no-deps` option to avoid build linked services – Krzysztof Raciniewski Apr 01 '19 at 05:52
  • You're right. In my effort to simplify the question, I had removed a `depends_on`. So, when I try `--no-deps`, it does indeed not build 'cachable_service'. However, it doesn't bring it up either. It just builds and brings up only 'service_in_development'. I would like it to still bring up 'cacheable_service'. – nemo Apr 02 '19 at 17:07
  • These are the two commands I have tried: `docker-compose up --build --no-deps --force-recreate service_in_development` and `docker-compose up --build --no-deps service_in_development`. Both of them had similar results. – nemo Apr 02 '19 at 17:11
  • Read this: https://github.com/docker/compose/issues/6332#issuecomment-437143989. Docker Compose build images procedure takes into dependencies(that's how it works), so you should look for other solutions that will allow you to build single images. I read that `dobi` can do it: `dist-img - building a minimal image (that doesn’t contain build dependencies) for distributing an application.`. I have never used it so I can't help in this topic - test it :) – Krzysztof Raciniewski Apr 02 '19 at 18:50
  • Thank you! Perhaps this feature is just not available currently, but I think, given docker-compose is really intended for local development, this feature makes a lot of sense. I will try to open a feature request. – nemo Apr 03 '19 at 19:28