Paweł Lis
Paweł Lis

Reputation: 315

docker-compose does not start the specified stage

I am trying to create a multi stage Dockerfile, where the first stage is meant to be started in development environment (simple dev server) and the second stage is meant for production (full size web server).

FROM python:3.9-alpine3.14 as develop
...
CMD python manage.py runserver 0.0.0.0:8000


FROM python:3.9-alpine3.14 as production
...
CMD gunicorn -b 0.0.0.0:8000 backend.wsgi

I am trying to specify the target stage in docker-compose.yml like so:

version: '3.7'

services:
  backend:
    build:
      target: develop
      ...

But it does not have any effect. Even when I set the target to OohEehOohAhAahTingTangWallaWallaBingBang, there is no error and the production server (last stage in Dockerfile) starts.

So, how to specify which target should be started in docker-compose.yml ?

EDIT: I am using WSL (Windows Subsystem for Linux) with Ubuntu-20.04

$ docker-compose --version
Docker Compose version v2.0.0-rc.1

Upvotes: 6

Views: 3756

Answers (2)

Paweł Lis
Paweł Lis

Reputation: 315

Ok, it seems that the behaviour of docker-compose is buggy. It DOES respect the target but only when building - when running docker-compose up, it just starts the image that was built last.

So if I set target: develop, then docker-compose build then docker-compose up (or ... up --build), then it will start the target develop (simple dev server).

If I then set target: production, then docker-compose build then docker-compose up (or ... up --build), then it will start the target production (full size server).

If I then set target: develop again, then docker-compose build then docker-compose up (or ... up --build), then it will start the target develop (simple dev server).

Also, it only complains that the target does not exist when building images, not starting containers:

failed to create LLB definition: target stage OohEehOohAhAahTingTangWallaWallaBingBang could not be found

Upvotes: 3

Antonio Petricca
Antonio Petricca

Reputation: 10996

The target keywork is supported since docker compose file format 3.4 as stated here.

The 3.4 file format is implemented since docker compose version 1.17.0 as stated here.

Which docker compose release are you using?

Edit

I suggest you to install manually the last official release as describe in the official guide by:

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

NB:: first of all remove the one installed by apt.

Upvotes: -1

Related Questions