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 .