1

.env file is in project root. I am using a docker file as follows

FROM alpine:3.7
WORKDIR /app
COPY . /app
RUN apk update && apk add build-base python3 python3-dev --no-cache bash &&  \
   pip3 install --upgrade pip && \
   pip3 install --trusted-host pypi.python.org --no-cache-dir -e. && \
   ./scripts/install.sh

EXPOSE 5000 3306
CMD ["myserver", "run"]

and the install.sh file as follows

#!/usr/bin/env bash

source .env

When I log in to the docker container I noticed that the .env file not getting sourced. How ever .env file is in the folder. How to source the .env file in docker container by using the docker file?

not 0x12
  • 19,360
  • 22
  • 67
  • 133

1 Answers1

2

RUN just effect in build stage, so you source will never affect container which is run time. You should do source in CMD or ENTRYPOINT which will run in container run time, write a entrypoint.sh for your project. Something like this

Dockerfile:

ENTRYPOINT ["docker-entrypoint.sh"]

docker-entrypoint.sh:

source .env
my_server run

And, use ENV in dockerfile is another way which will affect the runtime container.

atline
  • 28,355
  • 16
  • 77
  • 113
  • better to add stripped down example in your answer than link to a file – lacostenycoder May 06 '19 at 03:30
  • Still I didnt see the variables from .env file even after executing printenv from inside the docker container @atline – not 0x12 May 06 '19 at 03:58
  • 1
    I guess you use `docker exec` to enter into the container, but this cannot prove that it not work. As `source` in `docker-entrypoint.sh` just affect this shell session & its subprocess. So when you use `docker exec -it xxx /bin/bash` to enter the container, you can not see it. You should add `printenv` in docker-entrypoint.sh, then use docker logs to see it, and it will affect your `my_server run`. If you want also see it when exec, then explicit use ENV in dockerfile is the way, these variable will persist into container. – atline May 06 '19 at 04:54