I have the next configuration of my prod env:
docker-compose-prod.yml
version: '3.3'
services:
nginx:
build:
context: nginx
dockerfile: Dockerfile
args:
LANDING_PAGE_DOMAIN: ${LANDING_PAGE_DOMAIN}
container_name: nginx
networks:
- app-network
Dockerfile
FROM nginx:alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY production/* /etc/nginx/conf.d/
ARG LANDING_PAGE_DOMAIN
RUN sed -i s/{LANDING_PAGE_DOMAIN}/${LANDING_PAGE_DOMAIN}/g /etc/nginx/conf.d/landing.conf
EXPOSE 80 443
So when I try do build it I got next warning:
docker-compose -f docker-compose.production.yml build --build-arg LANDING_PAGE_DOMAIN="test.com" nginx
WARNING: The LANDING_PAGE_DOMAIN variable is not set. Defaulting to a blank string.
Where I made a mistake?
Docker compose uses the Dockerfile if you add the build command to your project's docker-compose. yml. Your Docker workflow should be to build a suitable Dockerfile for each image you wish to create, then use compose to assemble the images using the build command.
The ARG directive in Dockerfile defines the parameter name and defines its default value. This default value can be overridden by the --build-arg <parameter name>=<value> in the build command docker build .
Docker-compose command doesn't override Dockerfile CMD.
There are 2 options how to make it work:
Use existing code in docker-compose-prod.yml
and set environment
variable LANDING_PAGE_DOMAIN
:
export LANDING_PAGE_DOMAIN=test.com
then run build step w/o passing the build args:
docker-compose -f docker-compose.production.yml build nginx
Comment / delete 2 lines from docker-compose-prod.yml
file:
args:
LANDING_PAGE_DOMAIN: ${LANDING_PAGE_DOMAIN}
Then you'll be able to build it with passing arguments at build time:
docker-compose -f docker-compose-prod.yml build --build-arg
LANDING_PAGE_DOMAIN="test.com" nginx
The reason why it currently doesn't work is because those 2 lines in docker-compose-prod.yml
file explicitly sets the LANDING_PAGE_DOMAIN
argument to be populated by ${LANDING_PAGE_DOMAIN}
environment variable.
And when you run docker-compose build
with --build-arg
option it doesn't set any env vars but literally passes arguments for build
step.
the key word ARG
has a different scope before and after the FROM
instruction
Try using ARG twice in your Dockerfile, and/or you can try the ENV variables
ARG LANDING_PAGE_DOMAIN
FROM nginx:alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY production/* /etc/nginx/conf.d/
ARG LANDING_PAGE_DOMAIN
ENV LANDING_PAGE_DOMAIN=${LANDING_PAGE_DOMAIN}
RUN sed -i s/{LANDING_PAGE_DOMAIN}/${LANDING_PAGE_DOMAIN}/g /etc/nginx/conf.d/landing.conf
EXPOSE 80 443
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With