1

I have the following Dockerfile

FROM node:10.8.0-alpine as builder

# Set working directory
RUN mkdir /usr/src
RUN mkdir /usr/src/app
WORKDIR /usr/src/app

# Add /usr/src/app/node_modules/.bin to $PATH
ENV PATH /usr/src/app/node_modules/.bin:$PATH

# Get build arguments coming from .env file
ARG API_URL
ENV API_URL "$API_URL"

# Create config file from environment variables
RUN echo "API_URL = $API_URL" > ./app.cfg

# Install and cache app dependencies
COPY package.json /usr/src/app/package.json
RUN npm install
RUN npm install [email protected] -g
COPY . /usr/src/app
RUN npm run build

# Production environment
FROM nginx:1.15.2-alpine
COPY --from=builder /usr/src/app/build /usr/share/nginx/html
CMD ["nginx", "-g", "daemon off;"]

And I use the following docker-compose.yml file to build my Docker image

version: '3'
services:

  app:
    container_name: data-quality-app
    restart: always
    image: data-quality-app
    build:
      context: ./app
      dockerfile: Dockerfile
      args:
        - API_URL=${API_URL}
    env_file:
      - ./.env
    ports:
      - 80:80
    networks:
      - default

networks:
  default:
    external:
      name: data-quality-network

volumes:
  data-quality-db-volume:
    external: true

Note the file .env contains the environment variable API_URL=http://0.0.0.0:5433/graphql

All this works pretty well, but when I run my container with Docker Compose:

$ docker-compose up app

I would like to overwrite the file app.cfg in the container in order to replace the value of API_URL by its current value in the file .env.

I have tried to add the following ENTRYPOINT in Dockerfile but it did not work:

[...]
# Create config file to from environment variables
RUN echo "API_URL = $API_URL" > ./app.cfg
ENTRYPOINT echo "API_URL = $API_URL" > ./app.cfg
[...]

What am I missing?

1 Answer 1

2

You should write an entrypoint script that does the required setup, then runs the command passed to the container.

entrypoint.sh:

#!/bin/sh
if [ -n "$API_URL" ]; then
  echo "API_URL = $API_URL" > app.cfg
fi
exec "$@"

Dockerfile (final stage):

FROM nginx:1.15.2-alpine
COPY entrypoint.sh /
COPY --from=builder /usr/src/app/build /usr/share/nginx/html
ENTRYPOINT ["/entrypoint.sh"]
CMD ["nginx", "-g", "daemon off;"]

You can debug this by running something like

docker build -t mynginx .
docker run --rm -it -e API_URL=http://test mynginx sh

which will run the entrypoint script passing it "sh" as the command; that will set up the app.cfg file and then launch the debug shell (and not nginx).

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

Comments

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.