Search code examples
pythondockerdocker-composeenvironment-variablescontainers

Docker compose obtain env variables from .env file and pass to docker build


I'd like to be able to configure env variables for my docker containers and use them in build process with .env file

I currently have the following .env file:

SSH_PRIVATE_KEY=TEST
APP_PORT=8040

my docker-compose:

version: '3'
services:
  companies:
    image: companies8
    environment:
      - SSH_PRIVATE_KEY=${SSH_PRIVATE_KEY}
    ports:
      - ${APP_PORT}:${APP_PORT}
    env_file: .env
    build:
      context: .
      args:
        - SSH_PRIVATE_KEY=${SSH_PRIVATE_KEY}

my Dockerfile:

FROM python:3.7

# set a directory for the app

COPY . .

#Accept input argument from docker-compose.yml
ARG SSH_PRIVATE_KEY=abcdef
ENV SSH_PRIVATE_KEY $SSH_PRIVATE_KEY
RUN echo $SSH_PRIVATE_KEY

# Pass the content of the private key into the container
RUN mkdir -p /root/.ssh
RUN chmod 400 /root/.ssh
RUN echo "$SSH_PRIVATE_KEY" > /root/.ssh/id_rsa
RUN echo "$SSH_PUBLIC_KEY" > /root/.ssh/id_rsa.pub
RUN chmod 400 /root/.ssh/id_rsa
RUN chmod 400 /root/.ssh/id_rsa.pub
RUN eval $(ssh-agent -s) && ssh-add /root/.ssh/id_rsa && ssh-keyscan bitbucket.org > /root/.ssh/known_hosts
RUN ssh -T [email protected]

#Install the packages
RUN pip install -r v1/requirements.txt

# Tell the port number the container should expose
EXPOSE 8040

# run the command
CMD ["python", "v1/__main__.py"]

and i have the same SSH_PRIVATE_KEY environment variable set on my windows with value "test1" and the build log gives me the result 'test1' from

ENV SSH_PRIVATE_KEY $SSH_PRIVATE_KEY
RUN echo $SSH_PRIVATE_KEY

not the value that's in the .env file.

I need this because some of the libraries listed in my requirements.txt are in an internal repository and I need ssh to access them, therefore the ssh private key. There might be another proper way to use this, but its the general scenario i want to achieve - to pass env variables values from .env file to my docker build


Solution

  • There's a certain overlap between ENV and ARG as shown in the image below:

    enter image description here

    Since you are having the variable already exported in the operating system, its value will be present in the image from the ENV instruction.

    But if you do not really need the variable in the image and only in the build step (as far as I see from the docker-compose file), then the ARG instruction is enough.