This is a huge oversight. I'm not sure when the use-case would be that you want some variables within your project that build cannot access.
I'm running Laravel, which of course uses .env files. docker-composer passes these along nicely and the system runs.
Until I want to build an image and then composer (php) cannot generate autoload files because it can't find the .env file and therefore can't build an image.
TL;DR
My project needs some .env values and the Dockerfile has no way of knowing about them.
Compose is not a build tool, and has no vocation to become one. That being said, it should be simple enough to pass those variables you need as build args and declare them as ENV
variables in your Dockerfile
, like so:
docker-compose.yml
version: '2.3'
services:
foobar:
build:
context: .
args:
FOO: ${FOO}
BAR: ${BAR}
Dockerfile
FROM busybox:latest
ARG FOO
ARG BAR
ENV FOO $FOO
ENV BAR $BAR
...
Yes, I was using a Dockerfile for the build and had "build" and "context" within the compose file.
I appear to be missing this bit
ENV FOO $FOO
ENV BAR $BAR
I'll give that a shot tomorrow. The docs are not really clear on this and I cannot imagine my case is an edge case.
This is annoying...
Yes, I was using a Dockerfile for the build and had "build" and "context" within the compose file.
I appear to be missing this bit
ENV FOO $FOO ENV BAR $BAR
I'll give that a shot tomorrow. The docs are not really clear on this and I cannot imagine my case is an edge case.
Unfortunately, not work without:
ARG FOO
ARG BAR
docker-compose --version
docker-compose version 1.24.0-rc1, build 0f3d4dda
docker --version
Docker version 18.03.1-ce, build 9ee9f40
Some people like me are stupid and need clear names, so I will just make it a little more clear for myself and others.
docker-compose.yml
args:
TOMCAT_ADMIN_PASSWORD: ${TOMCAT_PASSWORD}
Dockerfile
ARG TOMCAT_ADMIN_PASSWORD
ENV TOMCAT_ADMIN_PASSWORD $TOMCAT_ADMIN_PASSWORD
So, Compose passes the args to the Dockerfile
the ARG TOMCAT_ADMIN_PASSWORD
contains TOMCAT_PASSWORD
and then I'm passing ARG TOMCAT_ADMIN_PASSWORD
to ENV TOMCAT_ADMIN_PASSWORD
This is not a practical solution whatsoever if you have a big environment file. This makes development tedious and unnecessarily annoying where if you can just read environment variables passed from the compose file. Hard-coding variables is never good, because if you would then want to add more environment variables you have to edit multiple files.
I guess the only way not to hardcode variables is to source your .env
before running docker-compose
like
. .env && docker-compose up --build -d
Keep in mind that .
and source
are the same command, yet I had problems with source
in Makefile
, so I stick with .
Also consider using Makefile
as it provides cleaner interface and will execute source
in the separate shell which will not load variables in your shell.
Most helpful comment
Compose is not a build tool, and has no vocation to become one. That being said, it should be simple enough to pass those variables you need as build args and declare them as
ENV
variables in yourDockerfile
, like so:docker-compose.yml
Dockerfile