我有以下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;"]
我使用以下docker-compose.yml
文件来构建我的Docker镜像
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
请注意,文件.env
包含环境变量API_URL=http://0.0.0.0:5433/graphql
所有这一切都很好,但是当我使用Docker Compose运行我的容器时:
$ docker-compose up app
我想覆盖容器中的文件app.cfg
,以便将API_URL
的值替换为文件.env
中的当前值。
我试图在ENTRYPOINT
中添加以下Dockerfile
但它不起作用:
[...]
# Create config file to from environment variables
RUN echo "API_URL = $API_URL" > ./app.cfg
ENTRYPOINT echo "API_URL = $API_URL" > ./app.cfg
[...]
我错过了什么?
您应该编写一个执行所需设置的入口点脚本,然后运行传递给容器的命令。
entrypoint.sh
:
#!/bin/sh
if [ -n "$API_URL" ]; then
echo "API_URL = $API_URL" > app.cfg
fi
exec "$@"
Dockerfile
(最后阶段):
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;"]
您可以通过运行类似的东西来调试它
docker build -t mynginx .
docker run --rm -it -e API_URL=http://test mynginx sh
将运行入口点脚本,将其作为命令传递给“sh”;这将设置app.cfg
文件,然后启动调试shell(而不是nginx)。