Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting build args in docker-compose.yml using env_file

I'm trying to use Docker and Docker Compose to create a containerized app. I have a PubNub account, which allows me to use different API keys for different environments (dev, test, prod). To help me build images for this, I am trying to use build args set with an env_file.

It's not working.

WARNING: The PUB_KEY variable is not set. Defaulting to a blank string.
WARNING: The SUB_KEY variable is not set. Defaulting to a blank string.

Questions:

  • What mistake am I making in setting the build args?
  • How do I fix it?
  • Is this a good way to set ENV variables for the containers scan and flask?

At the very bottom is an IntelliJ IDE screenshot, or the text code is just below.

Here is the docker-compose.yml content:

version: '3.6'

services:

  scan:
    env_file:
      - sample.env
    build:
      context: .
      dockerfile: Dockerfile
      args:
        pub_key: $PUB_KEY
        sub_key: $SUB_KEY
      target: scan
    image: bt-beacon/scan:v1

  flask:
    env_file:
      - sample.env
    build:
      context: .
      dockerfile: Dockerfile
      args:
        pub_key: $PUB_KEY
        sub_key: $SUB_KEY
      target: flask
    image: bt-beacon/flask:v1
    ports:
      - "5000:5000"

And the Dockerfile:

# --- BASE NODE ---
FROM python:3.6-jessie as base
ARG pub_key
ARG sub_key

RUN test -n "$pub_key"
RUN test -n "$sub_key"

# --- SCAN NODE ---
FROM base as scan

ENV PUB_KEY=$pub_key
ENV SUB_KEY=$sub_key

COPY app/requirements.scan.txt /

RUN apt-get update
RUN apt-get -y install bluetooth bluez bluez-hcidump python-bluez python-numpy python3-dev libbluetooth-dev libcap2-bin
RUN pip install -r /requirements.scan.txt
RUN setcap 'cap_net_raw,cap_net_admin+eip' $(readlink -f $(which python))

COPY app/src /app
WORKDIR /app

CMD ["./scan.py", "$pub_key", "$sub_key"]


# -- FLASK APP ---
FROM base as flask

ENV SUB_KEY=$sub_key

COPY app/requirements.flask.txt /
COPY app/src /app

RUN pip install -r /requirements.flask.txt

WORKDIR /app

EXPOSE 5000

CMD ["flask", "run"]

Finally, sample.env:

# PubNub app keys here
PUB_KEY=xyz1
SUB_KEY=xyz2

enter image description here

like image 568
PANDA Stack Avatar asked May 29 '18 20:05

PANDA Stack


People also ask

How do I set an environment variable in Docker?

Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.

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 use .ENV file?

The .env file feature only works when you use the docker-compose up command and does not work with docker stack deploy . Both $VARIABLE and ${VARIABLE} syntax are supported.


1 Answers

env_file can only set environment variables inside a service container. Variables from env_file cannot be injected into docker-compose.yml itself.

You have such options (described there in detail):

  • inject these variables into the shell, from which you run docker-compose up
  • create .env file containing these variables (syntax identical to your sample.env)

Personally I would separate image building process and container launching process (take away image building responsibility from docker-compose to external script, then building process can be configured easily).

like image 90
Andrii Maletskyi Avatar answered Sep 20 '22 20:09

Andrii Maletskyi