How do I pass environment variables to Docker containers

+1 vote

I'm new to Docker, and not clear about how to access an external database from a container. Can I just hard-code in the connection string?

# Dockerfile
ENV DATABASE_URL amazon:rds/connection?string
Jul 20, 2018 in Docker by Hannah
• 18,540 points
1,649 views

1 answer to this question.

+1 vote
Best answer

You can pass environment variables to your containers with the -e flag.

Ex-

sudo docker run -d -t -i -e REDIS_NAMESPACE='staging' \ 
-e POSTGRES_ENV_POSTGRES_PASSWORD='foo' \
-e POSTGRES_ENV_POSTGRES_USER='bar' \
-e POSTGRES_ENV_DB_NAME='mysite_staging' \
-e POSTGRES_PORT_5432_TCP_ADDR='docker-db-1.hidden.us-east-1.rds.amazonaws.com' \
-e SITE_URL='staging.mysite.com' \
-p 80:80 \
--link redis:redis \  

--name container_name dockerhub_id/image_name
answered Jul 20, 2018 by Kalgi
• 52,350 points

selected Dec 23, 2020 by MD

Related Questions In Docker

+1 vote
2 answers

How do I copy a file from docker container to host?

Here is a way which you can ...READ MORE

answered Aug 28, 2018 in Docker by Damon Salvatore
• 5,980 points
28,546 views
0 votes
2 answers
0 votes
1 answer

How do I connect to my docker VM from windows shell?

You can run a container with full ...READ MORE

answered Sep 5, 2018 in Docker by Tyrion anex
• 8,700 points
2,950 views
0 votes
1 answer

How do I scale in Docker Swarm Mode W/Terraform Digital Ocean Load Balancing

The solution you could build for Digital ...READ MORE

answered Jun 19, 2018 in Docker by shubham
• 7,340 points
1,471 views
+2 votes
1 answer
+2 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 58,100 points
2,687 views
+1 vote
2 answers

How to run docker containers on different machines

You can use labels and selectors for ...READ MORE

answered Oct 23, 2018 in Docker by Laila
3,544 views
0 votes
1 answer

How to list containers in Docker

To show only running containers use the given command: docker ...READ MORE

answered Jul 20, 2018 in Docker by Kalgi
• 52,350 points
778 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP