我有以下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 react-scripts@1.1.1 -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
[...]
我想念什么?
答案 0 :(得分:0)
您应该编写一个执行所需设置的入口点脚本,然后运行传递给容器的命令。
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
文件,然后启动调试外壳程序(而不是nginx)。