How to configure environment variables in a Dockerfile?

Configuring environment variables in a Dockerfile can be achieved by using the ENV instruction. Below is an example Dockerfile demonstrating how to configure environment variables.

FROM ubuntu:latest

# 设置环境变量
ENV MY_VARIABLE="Hello, World!"

# 执行其他操作,例如安装软件包、复制文件等

# 定义容器启动时执行的命令
CMD echo $MY_VARIABLE

In this example, we’re using the ENV directive to set an environment variable named MY_VARIABLE with the value of Hello, World!. In the CMD directive, we reference this environment variable using $MY_VARIABLE and print it to the console.

To build and run this Docker image, you can use the following command:

docker build -t my_image .
docker run my_image

Upon running, the output “Hello, World!” will be seen, indicating that the environment variables have been successfully configured.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds