I'm working on a Docker project where I want to set all the parameters in my Dockerfile dynamically, without passing parameters during the build process. Instead, I'd like to specify all the required variables in my Docker Compose file. Specifically, I want to set the base image for my Docker image using a variable within the Docker Compose file, something like:
Dockerfile:
# Set base image
FROM ${BASE_IMAGE}
Here's my Docker Compose file:
version: "3.8"
services:
my-service:
build:
context: .
dockerfile: Dockerfile
args:
BASE_IMAGE: python:3.6.6-alpine3.7
However, it doesn't seem to work as expected. The BASE_IMAGE
variable set in the Docker Compose file does not affect the FROM
statement in the Dockerfile during runtime.
Is there a way to achieve this dynamic configuration using Docker Compose? How can I define and use variables within my Docker Compose file to set values in the Dockerfile without having to pass parameters during the build process?
From https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact,
FROM instructions support variables that are declared by any ARG instructions that occur before the first FROM.
You need to set a default for BASE_IMAGE first:
ARG BASE_IMAGE=python:3.6.6-alpine3.7
FROM ${BASE_IMAGE}
And you can dynamically change it using your compose file etc.