How to pass environment variables to Docker containers

3 years ago 346

Jack Wallen shows you however to walk situation variables to Docker containers for a much businesslike improvement process.

docker-with-waves.jpg

Illustration: Lisa Hornung/TechRepublic

Did you cognize you tin walk situation variables from your big to Docker containers? By utilizing this feature, you tin marque your improvement of those containers a spot much efficient. But earlier we get into the however of this, we request to code the what—as in, what are situation variables? 

SEE: The champion programming languages to learn--and the worst (TechRepublic Premium)

Environment variables are dynamically named values that tin beryllium stored and past passed to services, applications oregon scripts. This is an casual mode of storing a worth successful a centralized determination (usually successful memory) and past utilizing it globally. To find retired much astir what situation variables are, cheque retired "Linux 101: What are situation variables?"

These types of variables are an incredibly useful instrumentality to person up your sleeve. And if you're a instrumentality developer, they tin assistance marque your occupation a spot easier.

Let maine amusement you how.

What you'll need

The lone happening you'll request to walk situation variables to Docker containers is simply a moving lawsuit of Docker and a idiosyncratic that is portion of the docker group. That's it. Let's walk immoderate variables.

How to acceptable an situation adaptable

To walk an situation adaptable to a container, we archetypal person to acceptable it. I'm going to show this successful Linux. If you usage a antithetic operating strategy for instrumentality development, you'll request to find retired however to bash the aforesaid connected your level of choice.

Let's accidental we privation to acceptable the adaptable for a database idiosyncratic and we program connected utilizing that adaptable for antithetic containers. We could acceptable a adaptable called DB_USER, which would beryllium viable for immoderate instrumentality utilizing immoderate benignant of database. Let's accidental the worth for DB_USER volition beryllium TechRepublic. To acceptable that variable, we'd contented the command:

export DB_USER=TechRepublic

To verify that adaptable has been set, contented the command:

echo $DB_USER

You should spot TechRepublic printed successful the terminal. That's it, you've acceptable your variable. Let's instrumentality this 1 measurement further (for the involvement of example) and besides acceptable a password arsenic an situation variable. You wouldn't bash this successful production, but it's a bully mode to exemplify however this is done. Set an situation adaptable for the password with:

export DB_PWORD=T3chR3public

How to walk the adaptable to a instrumentality

Now that you recognize however situation variables work, you tin spot however they tin easy beryllium passed to your containers. First I'll show however to bash it from the docker bid line, and past utilizing an .env file.

Unlike utilizing situation variables successful Linux, you can't acceptable them connected the big and past walk those acceptable variables to the instrumentality successful the aforesaid mode you would wrong the big system. In different words, you can't usage the variables we conscionable acceptable with the Docker bid like:

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

If you effort to deploy the instrumentality arsenic such, it volition tally but instantly exit. Why? Because dissimilar the Linux system, wherever you tin beauteous overmuch specify situation variables nevertheless you like, instrumentality images expect definite variables. For example, the PostgreSQL database can't usage DB_PWORD oregon DB_USER, arsenic it expects POSTGRES_PASSWORD and POSTGRES_USER. To that end, you could acceptable those situation variables connected your Linux hosts with the commands:

export POSTGRES_PASSWORD=t3chr3public export POSTGRES_USER=TechRepublic

OK, present we tin tally that aforesaid bid with:

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

The bid volition win and the instrumentality volition stay running. You tin trial it by accessing the PostgreSQL bid wrong the instrumentality by issuing:

docker exec -it postgresql psql -U $POSTGRES_USER

You should find yourself astatine the PostgreSQL console wrong your container.

How to walk variables with an .env record

One of the problems with passing situation variables arsenic described supra is that they unrecorded connected successful representation (until you unset them with the unset command). To debar this, we usage an situation adaptable file.

Let's instrumentality with the aforesaid variables we utilized above. Create a caller .env record with the command:

nano .env

In that record paste the following:

POSTGRES_PASSWORD=t3chr3public POSTGRES_USER=TechRepublic

Save and adjacent the file.

Now, however bash we walk those variables? Simple, we'd contented the command:

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

Make definite to usage the afloat way to your .env record (if you're not moving the docker bid from the aforesaid directory lodging the file). Your instrumentality volition deploy and beryllium acceptable to use. You tin trial it by accessing the PostgreSQL console. The lone quality is, you person to manually benignant retired the idiosyncratic (as we didn't acceptable the POSTGRES_USER adaptable successful the big system). That bid would be:

docker exec -it postgresql psql -U TechRepublic

And that's however you walk situation variables to a Docker container, either from the bid enactment oregon utilizing an .env file. Hopefully, you tin usage this method successful your developer workflow to marque things a spot much efficient.

Developer Essentials Newsletter

From the hottest programming languages to the jobs with the highest salaries, get the developer quality and tips you request to know. Weekly

Sign up today

Also spot

Read Entire Article