我有以下Dockerfile:
# => Build container
FROM node:14-alpine AS builder
WORKDIR /app
# split the dependecies from our source code so docker builds can cache this step
# (unless we actually change dependencies)
COPY package.json yarn.lock ./
RUN yarn install
COPY . ./
RUN yarn build
# => Run container
FROM nginx:alpine
# Nginx config
WORKDIR /usr/share/nginx/html
RUN rm -rf ./*
COPY ./nginx/nginx.conf /etc/nginx/conf.d/default.conf
# Default port exposure
EXPOSE 8080
# Copy .env file and shell script to container
COPY --from=builder /app/dist .
COPY ./env.sh .
COPY .env .
USER root
# Add bash
RUN apk update && apk add --no-cache bash
# Make our shell script executable
RUN chmod +x env.sh
# Start Nginx server
CMD ["/bin/bash", "-c", "/usr/share/nginx/html/env.sh && nginx -g "daemon off;""]
但是当我尝试运行它时,我在尝试添加bash
时得到以下错误:
------
> [stage-1 8/9] RUN apk update && apk add --no-cache bash:
#19 0.294 fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
#19 0.358 140186175183688:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed:ssl/statem/statem_clnt.c:1914:
#19 0.360 fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/x86_64/APKINDEX.tar.gz
#19 0.360 ERROR: https://dl-cdn.alpinelinux.org/alpine/v3.14/main: Permission denied
#19 0.360 WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.14/main: No such file or directory
#19 0.405 140186175183688:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed:ssl/statem/statem_clnt.c:1914:
#19 0.407 ERROR: https://dl-cdn.alpinelinux.org/alpine/v3.14/community: Permission denied
#19 0.407 WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.14/community: No such file or directory
#19 0.408 2 errors; 42 distinct packages available
------
executor failed running [/bin/sh -c apk update && apk add --no-cache bash]: exit code: 2
我需要添加它来执行这个脚本:
#!/bin/bash
# Recreate config file
rm -rf ./env-config.js
touch ./env-config.js
# Add assignment
echo "window._env_ = {" >> ./env-config.js
# Read each line in .env file
# Each line represents key=value pairs
while read -r line || [[ -n "$line" ]];
do
# Split env variables by character `=`
if printf '%sn' "$line" | grep -q -e '='; then
varname=$(printf '%sn' "$line" | sed -e 's/=.*//')
varvalue=$(printf '%sn' "$line" | sed -e 's/^[^=]*=//')
fi
# Read value of current variable if exists as Environment variable
value=$(printf '%sn' "${!varname}")
# Otherwise use value from .env file
[[ -z $value ]] && value=${varvalue}
# Append configuration property to JS file
echo " $varname: "$value"," >> ./env-config.js
done < .env
echo "}" >> ./env-config.js
从我所读到的添加USER root
应该解决这个问题,但在这种情况下没有。
对如何修复有什么想法吗?
你应该能够使用/bin/sh
作为一个标准的伯恩shell;此外,您应该能够避免在CMD
行中使用sh -c
包装器。
首先,使用POSIX shell语法重写脚本。浏览脚本,似乎它几乎是可以接受的;将第一行更改为#!/bin/sh
,并纠正非标准的${!varname}
扩展(参见Bash中的动态变量名)
# Read value of current variable if exists as Environment variable
value=$(sh -c "echo $$varname")
# Otherwise use value from .env file
[[ -z $value ]] && value=${varvalue}
您可以尝试使用alpine
或busybox
映像与更受限制的shell进行测试,或者使用bash设置POSIXLY_CORRECT
环境变量。
其次,同时使用ENTRYPOINT
和CMD
有一个合理的标准模式。CMD
作为参数传递给ENTRYPOINT
,所以如果ENTRYPOINT
以exec "$@"
结尾,它将用该命令替换自己。
#!/bin/sh
# ^^ not bash
# Recreate config file
...
echo "window._env_ = {" >> ./env-config.js
...
echo "}" >> ./env-config.js
# Run the main container CMD
exec "$@"
现在,在你的Dockerfile中,你不需要安装GNU bash,因为你不使用它,但是你需要正确地分离ENTRYPOINT
和CMD
。
ENTRYPOINT ["/usr/share/nginx/html/env.sh"] # must be JSON-array syntax
CMD ["nginx", "-g", "daemon off;"] # can be either syntax
顺便说一句,cmd1 && cmd2
语法是基本的shell语法,而不是bash扩展,因此您可以编写CMD ["/bin/sh", "-c", "cmd1 && cmd2"]
;但是,如果您编写的命令没有JSON数组语法,Docker将为您插入sh -c
包装器。你几乎不需要在Dockerfile中写出sh -c
。