Subscribe
How to Use Environment Variables with Dockerized Node.js Applications
3 mins read

By: vishwesh

How to Use Environment Variables with Dockerized Node.js Applications

If you're building a Node.js application that will be deployed using Docker, you'll likely need to make use of environment variables to provide configuration details such as database URLs, API keys, and other sensitive data. In this article, we'll explore how to use environment variables with Dockerized Node.js applications.

What Are Environment Variables?

Environment variables are variables that are available to your application at runtime and can be used to store configuration information, such as database connection strings, API keys, and other sensitive data. They are set outside of the application code, usually in the operating system or a configuration file, and are accessed using the process.env object in Node.js.

Why Use Environment Variables?

Using environment variables to store configuration information has a number of benefits, including:

  • Security: sensitive information can be kept separate from the application code, reducing the risk of data breaches and unauthorized access.
  • Flexibility: environment variables can be changed without having to modify the application code, making it easier to deploy your application across different environments.
  • Reproducibility: environment variables can be used to ensure that the application runs consistently across different machines and environments.

Setting Environment Variables in Docker

Docker provides several ways to set environment variables for your containerized applications. The most common ways are:

Using the ENV Instruction in the Dockerfile

The ENV instruction in the Dockerfile is used to set environment variables that will be available to the container at runtime. For example, to set the NODE_ENV environment variable to "production", you would add the following line to your Dockerfile:

ENV NODE_ENV=production

Using the --env Flag When Running the Container

You can also set environment variables when running the container using the --env flag followed by the variable name and value. For example, to set the PORT environment variable to "8080", you would run the container with the following command:

docker run --env PORT=8080 my-app

Using a .env File

You can also use a .env file to set environment variables for your Dockerized application. To use this method, create a file called .env in the root directory of your project and add the variable name and value pairs, separated by an equals sign:

DATABASE_URL=mongodb://localhost:27017/mydb
API_KEY=abc123

Then, in your Dockerfile, include the following line to copy the .env file into the container:

COPY .env .

Accessing Environment Variables in Node.js

Once you've set environment variables for your Dockerized Node.js application, you can access them using the process.env object in your Node.js code. For example, to access the PORT environment variable, you would use the following code:

const port = process.env.PORT || 3000;

This code sets the port variable to the value of the PORT environment variable, or 3000 if the variable is not set.

Conclusion

Using environment variables to store configuration information is a best practice when building Dockerized Node.js applications. In this article, we've explored how to set environment variables in Docker using the ENV instruction, the --env flag, and a .env file. We've also seen how to access environment variables in Node.js using the process.env object. By using environment variables, you can keep your application configuration separate from your application code, making it easier to deploy and more secure.

Recent posts

Don't miss the latest trends

    Popular Posts

    Popular Categories