How to Configure Environment Variables in Docker Deployed to EC2: A Step-by-Step Guide by OpsNexa
Deploying Docker containers on AWS EC2 is a powerful and scalable solution for running applications. When managing Docker containers, it’s often necessary to configure environment variables to pass configuration data, such as database credentials, API keys, and other sensitive information, to your containers. These variables help to configure the runtime behavior of your applications and ensure they run seamlessly in different environments.
In this comprehensive guide by OpsNexa, we’ll walk you through the process of configuring environment variables in Docker containers that are deployed to EC2. Whether you’re using Docker Compose or plain Docker commands, this step-by-step guide will show you how to set up environment variables correctly to improve your containerized application’s flexibility and security.
Why Use Environment Variables in Docker?
Environment variables in Docker provide a convenient and secure way to manage configuration data and pass it to containers at runtime. Using environment variables allows you to:
-
Separate configuration from code: By setting environment variables, you can keep sensitive data, like database passwords, out of your source code.
-
Adapt containers to different environments: Using different sets of environment variables, you can easily configure your containers for various environments (development, staging, production) without modifying the Docker image.
-
Enhance security: Avoid hardcoding sensitive information within your application code by using environment variables securely.
Let’s take a deeper dive into the practical steps for configuring environment variables in Docker deployed to EC2.
Prerequisites for Configuring Environment Variables in Docker on EC2
Before you start configuring environment variables in your Docker containers on EC2, ensure that you have the following in place:
1. AWS EC2 Instance Set Up
You should have an EC2 instance up and running with the Amazon Linux 2 or Ubuntu operating system installed. Make sure you have SSH access to the instance, and Docker is installed. You can follow these steps to install Docker on your EC2 instance:
Alternatively, if you’re using Ubuntu:
2. AWS EC2 Security Group Configuration
Ensure that your EC2 instance’s security group allows inbound access to the necessary ports for your application, like 80 for HTTP, 443 for HTTPS, or any custom ports your application may use. You can do this by modifying the EC2 security group settings in the AWS console.
3. Docker Image Ready for Deployment
Ensure that the Docker image you want to deploy on EC2 is ready. You can either build your Docker image locally or pull it from a Docker registry like Docker Hub.
How to Set Environment Variables in Docker on EC2
There are several ways to set environment variables when deploying Docker containers on EC2. The method you choose depends on how you’re managing your containers. Below, we’ll explore the most common ways to configure environment variables in Docker.
Method 1: Using docker run
Command to Set Environment Variables
The most straightforward way to set environment variables in Docker is by using the -e
flag with the docker run
command. This method is suitable for running single Docker containers.
Steps to Set Environment Variables Using docker run
:
-
SSH into your EC2 instance:
-
Run your Docker container with the desired environment variables:
In this command:
-
-d
runs the container in detached mode. -
-e
allows you to pass environment variables to the container. You can add as many as needed. -
-p
maps ports from the container to the host machine (e.g., port 8080 to port 80). -
my-docker-image
is the name of your Docker image.
Example:
In this example, we pass DATABASE_URL
and API_KEY
as environment variables to the container when it runs.
Method 2: Using Docker Compose to Set Environment Variables
If you are managing multiple containers or using Docker Compose for orchestration, you can set environment variables in your docker-compose.yml
file.
Steps to Set Environment Variables Using Docker Compose:
-
Install Docker Compose on your EC2 instance:
-
Create a
docker-compose.yml
file for your application:
In this example:
-
The
environment
section is where we define our environment variables. -
DATABASE_URL
andAPI_KEY
are the environment variables that will be available within the container. -
The container will run the
my-web-app
Docker image and map port 80 from the container to port 80 on the EC2 instance.
-
Run Docker Compose:
This will start the container(s) with the defined environment variables.
Method 3: Using a .env
File for Environment Variables
You can also store environment variables in a .env
file and reference them in your docker-compose.yml
. This is especially useful for managing environment variables securely and keeping them organized.
Steps to Use a .env
File:
-
Create a
.env
file in the same directory as yourdocker-compose.yml
:
-
Update your
docker-compose.yml
to reference the.env
file:
-
Run Docker Compose:
Docker will automatically load the environment variables from the .env
file and pass them to the container.
Method 4: Using AWS Secrets Manager for Secure Environment Variables
For production environments, it’s essential to store sensitive information (such as API keys and database passwords) securely. AWS Secrets Manager is an excellent tool for storing and managing sensitive environment variables.
Steps to Use AWS Secrets Manager with Docker on EC2:
-
Store your environment variables in AWS Secrets Manager:
-
Go to the AWS Secrets Manager console and create a new secret.
-
Store the key-value pairs for your environment variables (e.g.,
DATABASE_URL
,API_KEY
).
-
-
Retrieve the secret in your Docker container:
-
You can use the AWS SDK or AWS CLI within your Docker container to retrieve the secrets at runtime and inject them into your application.
-
For example, using the AWS CLI within your Dockerfile:
Alternatively, you can use an IAM role on your EC2 instance with permissions to access Secrets Manager and configure the application to automatically pull environment variables from Secrets Manager.
Best Practices for Configuring Environment Variables in Docker on EC2
To ensure that your environment variables are managed efficiently and securely, follow these best practices:
-
Avoid hardcoding sensitive information: Never hardcode passwords, API keys, or other sensitive data in your Dockerfile or source code. Always use environment variables or secrets management tools like AWS Secrets Manager.
-
Use
.env
files for local development: For local development, use.env
files to keep your environment variables organized and separate from your codebase. -
Set environment variables securely: If you’re handling sensitive information, always store them in a secure location like AWS Secrets Manager or use IAM roles for secure access.
-
Use Docker Compose for complex setups: For applications with multiple containers, Docker Compose is a great way to manage the environment variables across different services.
Conclusion
Configuring environment variables in Docker containers deployed to EC2 is essential for ensuring your applications are flexible, secure, and easily configurable across different environments. By following the steps outlined in this guide, you can quickly set up and manage environment variables for your Docker containers in EC2 using methods like docker run
, Docker Compose, .env
files, and AWS Secrets Manager.
At OpsNexa, we believe in making the process of deploying and managing containerized applications as smooth and efficient as possible. Whether you’re deploying a single container or managing a complex microservices architecture, knowing how to configure environment variables properly is a key skill in optimizing your workflow.
Start configuring your Docker environment variables today to improve your application’s security, portability, and overall efficiency!