Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass args to the Dockerfile from docker-compose

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?

like image 994
user137 Avatar asked Nov 04 '19 14:11

user137


People also ask

Can Dockerfile work with Docker compose?

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.

What is ARG command in Dockerfile?

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 .

Does Docker compose override Dockerfile?

Docker-compose command doesn't override Dockerfile CMD.


2 Answers

There are 2 options how to make it work:

  1. 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

  2. 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.

like image 58
esboych Avatar answered Sep 29 '22 20:09

esboych


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
like image 28
Nathanael Avatar answered Sep 29 '22 20:09

Nathanael