且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在我的CMD脚本中设置环境变量

更新时间:2023-09-26 15:09:22

这里有几件事。

首先, docker-compose up 不会在您以 docker-compose up 开始的容器内运行命令。它启动一个 new 容器以运行一次性命令。您可能想要 docker-compose exec

First, docker-compose run doesn't run a command inside the container you started with docker-compose up. It starts a new container to run a one-off command. You probably want docker-compose exec.

使用 docker-compose run 是通过提供新命令( bash $来覆盖 CMD c $ c>)在 docker-compose run 命令行上。

The reason you don't see the variable when using docker-compose run is that you are overriding your CMD by providing a new command (bash) on the docker-compose run command line.

您可以考虑:


  • Dockerfile 中使用 ENV 语句

  • docker-compose.yml 环境键/ li>
  • Using ENV statements in your Dockerfile.
  • Using the environment key in your docker-compose.yml

前者会将信息嵌入到您的图片中,而后者则意味着如果您未明确设置,则该变量将不会被设置它放在您的 docker-compose.yaml 文件中(或在 docker运行中使用 -e 命令行)。

The former will embed the information into your image, while the latter would mean that the variable would be unset if you didn't explicitly set it in your docker-compose.yaml file (or using -e on the docker run command line).

您可以使用 ENTRYPOINT 实现目标脚本并在那里设置值,但这不会影响y可见的环境ou使用 docker exec (或 docker-compose exec )时。

You may be able to accomplish your goal using an ENTRYPOINT script and setting the value there, but that won't impact the environment visible to you when using docker exec (or docker-compose exec).