繁体   English   中英

如何在构建过程中将 ENTRYPOINT 传递给 Dockerfile?

[英]How to pass ENTRYPOINT to Dockerfile during build process?

为什么示例 1 有效,但示例 2 和示例 3 均无效? 如何在构建时传递用作入口点的 ARG,并且仍然能够将命令行 arguments 传递给入口点脚本?

示例 1 - 按预期工作

docker-compose.yaml

services:
    linux:
        image: linux_admin
        build:
            context: ./
            dockerfile: ./linux.Dockerfile

Dockerfile

FROM debian:buster as linux_admin
ARG SRC_ROOT
ENV SRC_ROOT=${SRC_ROOT:-/opt/app}
ENV SCRIPTS_DIR=$SRC_ROOT/scripts
ENV ENTRYPOINT_SH=$SCRIPTS_DIR/linux-entrypoint.sh
RUN mkdir -p $SRC_ROOT
RUN apt-get update -y
RUN apt-get install -y openssl certbot=0.31.0-1+deb10u1 curl
RUN mkdir -p $SRC_ROOT 2>/dev/null
WORKDIR $SRC_ROOT
COPY ./scripts/*.sh $SCRIPTS_DIR/
RUN chmod +x $SCRIPTS_DIR/*.sh
CMD ["echo", "hello from Dockerfile"]
ENTRYPOINT ["/opt/app/scripts/linux-entrypoint.sh"]

脚本/linux-entrypoint.sh

#!/bin/bash
set -e

# Start the Docker container.
echo "Starting up container with the following command: "
echo '${@}'"=${@}"
echo "0=$0"
echo "1=$1"
echo "2=$2"
exec "$@"

output 来自docker-compose build && docker-compose up linux

Starting www_linux_1 ... done
Attaching to www_linux_1
linux_1  | Starting up container with the following command: 
linux_1  | ${@}=echo hello from Dockerfile
linux_1  | 0=/opt/app/scripts/linux-entrypoint.sh
linux_1  | 1=echo
linux_1  | 2=hello from Dockerfile
linux_1  | hello from Dockerfile
www_linux_1 exited with code 0

示例 2 - 未按预期工作

如果我只更改 Dockerfile 的最后一行,如下所示:

ENTRYPOINT [$ENTRYPOINT_SH]

output 来自docker-compose build && docker-compose up linux

Attaching to www_linux_1
linux_1  | echo: 1: echo: [/opt/app/scripts/linux-entrypoint.sh]: not found
www_linux_1 exited with code 127

示例 3 - 未按预期工作

如果我只更改 Dockerfile 的最后一行,如下所示:

ENTRYPOINT ["$ENTRYPOINT_SH"]

output 来自docker-compose build && docker-compose up linux

Recreating 126e2f64265c_www_linux_1 ... error

ERROR: for 126e2f64265c_www_linux_1  Cannot start service linux: OCI runtime create failed: container_linux.go:370: starting container process caused: exec: "$ENTRYPOINT_SH": executable file not found in $PATH: unknown

ERROR: for linux  Cannot start service linux: OCI runtime create failed: container_linux.go:370: starting container process caused: exec: "$ENTRYPOINT_SH": executable file not found in $PATH: unknown
ERROR: Encountered errors while bringing up the project.

参考

我已阅读以下内容,以及其他一些内容:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM