How to pass environment variables to Docker containers

Jack Wallen reveals you move surroundings variables to Docker containers for a extra environment friendly growth course of.

docker-with-waves.jpg

Illustration: Lisa Hornung/TechRepublic

Do you know you may move surroundings variables out of your host to Docker containers? Through the use of this function, you may make your growth of these containers a bit extra environment friendly. However earlier than we get into the how of this, we have to handle the what—as in, what are surroundings variables? 

SEE: The most effective programming languages to learn–and the worst (TechRepublic Premium)

Surroundings variables are dynamically named values that may be saved after which handed to companies, functions or scripts. That is a simple method of storing a worth in a centralized location (often in reminiscence) after which utilizing it globally. To search out out extra about what surroundings variables are, try “Linux 101: What are surroundings variables?”

Some of these variables are an extremely useful trick to have up your sleeve. And in the event you’re a container developer, they might help make your job a bit simpler.

Let me present you the way.

What you will want

The one factor you will must move surroundings variables to Docker containers is a operating occasion of Docker and a consumer that’s a part of the docker group. That is it. Let’s move some variables.

The way to set an surroundings variable

To move an surroundings variable to a container, we first need to set it. I will exhibit this in Linux. If you happen to use a special working system for container growth, you will must learn the way to do the identical in your platform of alternative.

For instance we need to set the variable for a database consumer and we plan on utilizing that variable for various containers. We might set a variable referred to as DB_USER, which might be viable for any container utilizing any sort of database. For instance the worth for DB_USER will likely be TechRepublic. To set that variable, we would challenge the command:

export DB_USER=TechRepublic

To confirm that variable has been set, challenge the command:

echo $DB_USER

It’s best to see TechRepublic printed within the terminal. That is it, you have set your variable. Let’s take this one step additional (for the sake of instance) and in addition set a password as an surroundings variable. You would not do that in manufacturing, however it’s a great way for example how that is carried out. Set an surroundings variable for the password with:

export DB_PWORD=T3chR3public

The way to move the variable to a container

Now that you just perceive how surroundings variables work, you may see how they will simply be handed to your containers. First I am going to exhibit do it from the docker command line, after which utilizing an .env file.

In contrast to utilizing surroundings variables in Linux, you may’t set them on the host after which move these set variables to the container in the identical method you’d throughout the host system. In different phrases, you may’t use the variables we simply set with the Docker command like:

docker run --name postgresql -e $DB_PWORD -e $DB_USER -d postgres

If you happen to try to deploy the container as such, it would run however instantly exit. Why? As a result of in contrast to the Linux system, the place you may just about outline surroundings variables nonetheless you want, container photos count on sure variables. For instance, the PostgreSQL database cannot use DB_PWORD or DB_USER, because it expects POSTGRES_PASSWORD and POSTGRES_USER. To that finish, you might set these surroundings variables in your Linux hosts with the instructions:

export POSTGRES_PASSWORD=t3chr3public
export POSTGRES_USER=TechRepublic

OK, now we are able to run that very same command with:

docker run --name postgresql -e POSTGRES_PASSWORD -e POSTGRES_USER -d postgres

The command will succeed and the container will stay operating. You possibly can check it by accessing the PostgreSQL command throughout the container by issuing:

docker exec -it postgresql psql -U $POSTGRES_USER

It’s best to end up on the PostgreSQL console inside your container.

The way to move variables with an .env file

One of many issues with passing surroundings variables as described above is that they stay on in reminiscence (till you unset them with the unset command). To keep away from this, we use an surroundings variable file.

Let’s follow the identical variables we used above. Create a brand new .env file with the command:

nano .env

In that file paste the next:

POSTGRES_PASSWORD=t3chr3public
POSTGRES_USER=TechRepublic

Save and shut the file.

Now, how will we move these variables? Easy, we would challenge the command:

docker run --name postgresql --env-file .env -d postgres

Make sure that to make use of the total path to your .env file (in the event you’re not operating the docker command from the identical listing housing the file). Your container will deploy and be prepared to make use of. You possibly can check it by accessing the PostgreSQL console. The one distinction is, you must manually sort out the consumer (as we did not set the POSTGRES_USER variable within the host system). That command can be:

docker exec -it postgresql psql -U TechRepublic

And that is the way you move surroundings variables to a Docker container, both from the command line or utilizing an .env file. Hopefully, you need to use this technique in your developer workflow to make issues a bit extra environment friendly.

Additionally see

Recent Articles

spot_img

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox