[Docker] Use Environment Variables for Docker Containers
Containers can be very useful to emulate various environments. You can also use much more complex containers. In this case, we had a container with a simple operating system but a lot of containers will include a bunch of executables and runtimes. You could, for example, start a container that has the latest version of NodeJs installed by running the image called "node". Just like in our previous examples, you can specify a command to be executed once our container is started. In this case, you will ask NodeJs to evaluate the following statement. Console.log(process.version).
docker run node:latest node -e "console.log(process.version)"
This will start the container, execute that command and you should see a version number as the output. Note that if you run
node -v
in your local environment, this might be a different version number. This is because the runtime for NodeJs that ran that first command is not the same as the one you have installed on your machine. In fact, you don’t need to have NodeJs installed at all to run Node, as long as you have docker.
Another neat thing that you can do with containers is to pass environment variables to your container. This way, you could pass it something like the base URL for your API. Your development team would then be able to use a different API than your production server. To pass an environment variable to a container, you can use the -e argument. In this next example, you can use the -e to pass an environment variable than will then be outputted by NodeJs.
docker run -e NAME=World node:latest node -e "console.log(process.env.NAME)" ## World
First -e NAME=World
: set an env variable called NAME, value is World
Second -e "console
: mean exec a command.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2020-01-30 [Algorithm] 88. Merge Sorted Array
2019-01-30 [Angular] Improve Server Communication in Ngrx Effects with NX Data Persistence in Angular
2019-01-30 [Webpack] Detect Unused Code with Webpack and unused-files-webpack-plugin
2019-01-30 [Functional Programming Monad] Combine Stateful Computations Using Composition
2019-01-30 [Functional Programming Monad] Combine Stateful Computations Using A State Monad
2019-01-30 [Algorithm] Find Max Items and Max Height of a Completely Balanced Binary Tree
2019-01-30 [Angular] AfterContentChecked && AfterViewChecked