简体   繁体   English

在容器启动时,运行脚本来设置全局环境变量

[英]On container Start, run a script to set a global environmental variable

I am trying to create a docker image that will run a script when it starts.我正在尝试创建一个 docker 图像,它将在启动时运行脚本。 I want that script to reach out and get the public IP address of the container (simple curl call) and then set the returned address as an environmental variable that other programs can access.我希望该脚本能够获取容器的公共 IP 地址(简单的 curl 调用),然后将返回的地址设置为其他程序可以访问的环境变量。

I had tried setting the script to run on launch using a CMD in the Dockerfile, and while that executed the script it did not pass the variable out of the script into the system environment.我曾尝试使用 Dockerfile 中的 CMD 将脚本设置为在启动时运行,并且在执行脚本时它没有将脚本中的变量传递到系统环境中。 I had tried using CMD./startup.sh as well as CMD. /var/www/html/startup.sh I also tried using variations of the the exec form CMD ["sh" "startup.sh"] but even when sourcing the script it would not pass the variable to the system.我曾尝试使用CMD./startup.sh以及CMD. /var/www/html/startup.sh我也尝试使用 exec 形式的变体CMD ["sh" "startup.sh"]但即使在采购时它不会将变量传递给系统的脚本。

Doing some reading people suggested trying to set the script as the entry point so I tried ENTRYPOINT [ "sh", "/var/www/html/s3_pull.sh" ] and ENTRYPOINT. /var/www/html/s3_pull.sh阅读一些人建议尝试将脚本设置为入口点,所以我尝试ENTRYPOINT [ "sh", "/var/www/html/s3_pull.sh" ]ENTRYPOINT. /var/www/html/s3_pull.sh ENTRYPOINT. /var/www/html/s3_pull.sh

And while these run the script and I can see that the curl call is working and the variable is being set in the script.当这些运行脚本时,我可以看到 curl 调用正在运行并且正在脚本中设置变量。 It does not pass that variable to the system.它不会将该变量传递给系统。

The script Startup.sh is脚本Startup.sh

#!/bin/bash

export FQDN=$(curl checkip.amazonaws.com)
echo $FQDN

exec "$@"

Output from the script (resulting ip address is censored here but comes back correct)来自脚本的 Output(生成的 ip 地址在此处被审查但返回正确)

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    14  100    14    0     0    103      0 --:--:-- --:--:-- --:--:--   103
108.---.---.---

currently my Dockerfile is目前我的Dockerfile

FROM mediawiki:latest
WORKDIR /var/www/html/
ADD Image-mods-AWS.sh ./
ADD startup.sh ./
RUN bash /var/www/html/Image-mods-AWS.sh
ENTRYPOINT [ "/bin/bash", "/var/www/html/startup.sh" ]
CMD apache2-foreground

The image-mods-AWS script installs Nano and the AWS CLI and sets the startup.sh as executable image-mods-AWS 脚本安装 Nano 和 AWS CLI 并将 startup.sh 设置为可执行文件

The container runs on Debian 11 and the main process is apache2-foreground.容器运行在Debian 11,主进程是apache2-foreground。

I'm fairly new to Linux and containers but my understanding of the problem is that the shell used to execute the script is a child process and as such cant pass its variables up to the parent shell. So I need to have the parent shell execute it.我对 Linux 和容器相当陌生,但我对问题的理解是用于执行脚本的 shell 是一个子进程,因此无法将其变量传递给父进程 shell。所以我需要让父进程 shell 执行它。 Which lead me to trying the dot space slash ". /" execution which works if I log into the container and run it manually from the command line, but doesn't work when used in the Dockerfile as either a CMD or ENTRYPOINT.这导致我尝试点空格斜杠“./”执行,如果我登录到容器并从命令行手动运行它,它会起作用,但在 Dockerfile 中用作 CMD 或 ENTRYPOINT 时不起作用。

Is what I want to do even possible?我想做的事有可能吗? and does anyone have any suggestions on how to get this to run and pass the variable to the system on launch.并且有没有人对如何让它运行并在启动时将变量传递给系统有任何建议。


EDIT:编辑:

When running the command运行命令时

docker run --rm [container-name:tag] env

The output is output 是

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    14  100    14    0     0     97      0 --:--:-- --:--:-- --:--:--    97
108.---.---.---

HOSTNAME=----
PHP_VERSION=7.4.29
APACHE_CONFDIR=/etc/apache2
PHP_INI_DIR=/usr/local/etc/php
GPG_KEYS=----
PHP_LDFLAGS=-Wl,-O1 -pie
PWD=/var/www/html
HOME=/root
MEDIAWIKI_MAJOR_VERSION=1.37
MEDIAWIKI_VERSION=1.37.2
PHP_SHA256=----
PHPIZE_DEPS=autoconf dpkg-dev file g++ gcc libc-dev make pkg-config re2c
PHP_URL=https://www.php.net/distributions/php-7.4.29.tar.xz
SHLVL=0
PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
APACHE_ENVVARS=/etc/apache2/envvars
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PHP_ASC_URL=https://www.php.net/distributions/php-7.4.29.tar.xz.asc
PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
FQDN=108.---.---.---

When doing this the container starts, prints the environment, then exits.执行此操作时,容器启动,打印环境,然后退出。 it does however show that it is running the script and assigning the variable.但是它确实表明它正在运行脚本并分配变量。

When running the container with the command使用命令运行容器时

docker run --rm [container-name:tag]

The output is output 是

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    14  100    14    0     0     57      0 --:--:-- --:--:-- --:--:--    57
108.---.---.---

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Thu Jul 14 00:13:39.367797 2022] [mpm_prefork:notice] [pid 9] AH00163: Apache/2.4.53 (Debian) PHP/7.4.29 configured -- resuming normal operations
[Thu Jul 14 00:13:39.367861 2022] [core:notice] [pid 9] AH00094: Command line: 'apache2 -D FOREGROUND'

Then Running the command 'env' gives the following output然后运行命令“env”给出以下 output

MEDIAWIKI_VERSION=1.37.2
APACHE_CONFDIR=/etc/apache2
HOSTNAME=----
PHP_INI_DIR=/usr/local/etc/php
MEDIAWIKI_MAJOR_VERSION=1.37
HOME=/root
PHP_LDFLAGS=-Wl,-O1 -pie
PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
PHP_VERSION=7.4.29
GPG_KEYS=----
PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
PHP_ASC_URL=https://www.php.net/distributions/php-7.4.29.tar.xz.asc
PHP_URL=https://www.php.net/distributions/php-7.4.29.tar.xz
TERM=xterm
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PHPIZE_DEPS=autoconf dpkg-dev file g++ gcc libc-dev make pkg-config re2c
PWD=/var/www/html
PHP_SHA256=----
APACHE_ENVVARS=/etc/apache2/envvars

Which does not show the variable as part of the enviroment哪个不将变量显示为环境的一部分

Finally pushing the image up to AWS and running on ECS-Fargate and using the 'env' command gives the following output最后将图像推送到 AWS 并在 ECS-Fargate 上运行并使用“env”命令给出以下 output

S3_CONFIG_BUCKET=----
MEDIAWIKI_VERSION=1.37.2
APACHE_CONFDIR=/etc/apache2
PHP_INI_DIR=/usr/local/etc/php
HOSTNAME=----.compute.internal
MEDIAWIKI_MAJOR_VERSION=1.37
HOME=/root
PHP_LDFLAGS=-Wl,-O1 -pie
AWS_CONTAINER_CREDENTIALS_RELATIVE_URI=/v2/credentials/----
PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
AWS_EXECUTION_ENV=AWS_ECS_FARGATE
PHP_VERSION=7.4.29
GPG_KEYS=----
PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
PHP_ASC_URL=https://www.php.net/distributions/php-7.4.29.tar.xz.asc
AWS_DEFAULT_REGION=----
PHP_URL=https://www.php.net/distributions/php-7.4.29.tar.xz
ECS_CONTAINER_METADATA_URI_V4=----
TERM=xterm-256color
ECS_CONTAINER_METADATA_URI=----
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
S3_LOGO_FILE=wiki.png
LANG=C.UTF-8
AWS_REGION=----
PHPIZE_DEPS=autoconf dpkg-dev file g++ gcc libc-dev make pkg-config re2c
PWD=/var/www/html
PHP_SHA256=----
APACHE_ENVVARS=/etc/apache2/envvars
S3_CONFIG_FILE=----

which also does not show the variable being saved to the environment这也没有显示保存到环境中的变量

try using source when running your script like this:像这样运行脚本时尝试使用 source :

$ source ./startup.sh

I'm not sure how it should be done under Dockerfile but try this:我不确定在 Dockerfile 下应该如何完成,但试试这个:

ENTRYPOINT [ "source", "/var/www/html/startup.sh" ]

暂无
暂无

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

相关问题 如何在 Jenkins 中将 AWS 凭证设置为环境变量 - How to set AWS credentials as Environmental variable in Jenkins 无法使用使用 jq 的环境变量呈现 groovy sh 脚本 - Cannot render groovy sh script with environmental variable that uses jq “Cloud Run 错误:容器启动失败。无法启动然后侦听 PORT 环境变量定义的端口。” - "Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable." Cloud Run 错误:容器无法启动。 无法启动然后侦听 PORT 环境变量定义的端口。 - Django - Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. - Django Google Cloud Run - 容器无法启动解决方法 - Google Cloud Run - Container failed to start workarounds 是否可以将环境变量解析为 REST API 的响应? - Is it possible to parse an environmental variable as a response for a REST API? 无法在 M1 Macbook 上启动 Cloud Run 容器 - Unable to start a Cloud Run container on M1 Macbook Cloud Run:用户提供的容器启动失败,监听PORT=8080提供的端口 - Cloud Run: The user-provided container failed to start and listen on the port defined provided by the PORT=8080 容器未能启动。 无法启动然后侦听 PORT 环境变量定义的端口 - Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable Docker ECS Fargate 中的容器在运行脚本时以代码 0 退出。 无法运行容器以到达 /bin/sh - Docker container in ECS Fargate exits with code 0 when running script. Unable to run container to get to /bin/sh
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM