2

I want to generate a .env file and push it to the docker image during the docker build with Dockerfile. The problem is that the .env file is not copy to the docker image. I tried to add COPY .env to /website/.env but it can't find it. I execute a nodejs script during the build process who get envs from AWS and then create a .env file.

FROM node:10.15.3-alpine

ARG SERVER_ENV

# Create app directory

RUN mkdir /website
WORKDIR /website

COPY . /website/
RUN node aws ${SERVER_ENV}
COPY .env /website/.env
COPY package.json yarn.lock ./
RUN ls -al
RUN pwd

# Install Yarn
RUN npm install -g [email protected]

# Install app dependencies
RUN yarn install

# Build source files
RUN yarn run build

UPDATE I finally found a way to fix it. I separate task in my Dockerfile like this :

FROM mhart/alpine-node:10 as env
ARG SERVER_ENV

WORKDIR /usr/src
COPY aws.js /usr/src
RUN yarn add aws-sdk
COPY . .
RUN node aws ${SERVER_ENV}

FROM mhart/alpine-node:10 as base
WORKDIR /usr/src
COPY package.json yarn.lock /usr/src/
RUN yarn install
COPY . .
COPY --from=env /usr/src .
RUN yarn build


FROM mhart/alpine-node:10
WORKDIR /usr/src
COPY --from=base /usr/src .
1
  • 1
    Side note, typically .env contains credentials. Not something you want to share around. Typically I see projects contain a .env.dist with the var names (keys) in it. The consumer has to 'cp .env.dist .env' and populate the credentials. The .env is also ignored in any version control system to prevent credentials from being committed. Food for thought. Commented Apr 1, 2019 at 14:23

1 Answer 1

2

Maybe you have a .dockerignore file that blocks the .env file from being copied to the image?

Sign up to request clarification or add additional context in comments.

2 Comments

My dockerignore only contains .git But I have a really strange problem. When I ls -ail during my docker build process it shows me the .env and no .git file. And after launch the docker image, I have no .env file and I have the .git folder
saved my day, haven't noticed it's ignored

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.