我有以下 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 '%s\n' "$line" | grep -q -e '='; then
varname=$(printf '%s\n' "$line" | sed -e 's/=.*//')
varvalue=$(printf '%s\n' "$line" | sed -e 's/^[^=]*=//')
fi
# Read value of current variable if exists as Environment variable
value=$(printf '%s\n' "${!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應該可以解決這個問題,但在這種情況下卻沒有。
關于如何修復的任何想法?
uj5u.com熱心網友回復:
您應該能夠/bin/sh用作標準的 Bourne shell;此外,您應該能夠避免行中的sh -c包裝器CMD。
首先,使用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}
您可以嘗試使用具有更多限制的 shell的alpine或busybox影像來測驗它,或者POSIXLY_CORRECT使用 bash設定環境變數。
其次,有一個合理的標準模式使用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包裝器。您幾乎不需要sh -c在 Dockerfile 中寫出。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/323977.html
