Answer a question

I have an application with a production Dockerfile that copies our source code into the container, i.e:

COPY . /app

I would like to make this copy conditional, so that passing in a CONTEXT= environment variable prevents/allows this line to execute. Is that possible?

I saw related posts on here but no clear answer. Thanks!

Answers

From this post I discovered that Docker-Compose 3.7 supports targeting individual build stages.

So I created a Dockerfile with 2 stages, a base layer that sets up the container, then a deploy that copies in the codebase:

# BUILD STAGE 1 - BASE
FROM webdevops/php-apache:7.2-alpine as base

# Do stuff

# BUILD STAGE 2 - DEPLOY
FROM base as deploy

# Add project
COPY . /app

# Do other stuff

Then I configured my dev docker-compose.yml to target only the base layer, and to mount a volume for the code.

version: "3.7"

services:
  app.base.img:
    build:
      context: .
      target: base
    volumes:
      - ./:/app
    container_name: app.base

My production pipeline builds the image normally (with both layers).

Note the version 3.7 declaration, apparently it's required.

This does what I want, thanks for all the comments and suggestions on this! If you think this is unwise feel free to comment.

Logo

云原生社区为您提供最前沿的新闻资讯和知识内容

更多推荐