Docker config to edit Node code when run

0 votes

I have a simple MEAN app that I want to run multiple times, only problem is I don't want each instance of the app to point at the same database. To solve this in Docker I am placing the Node code and the MongoDB in the same container. The code references a specific DB, but it doesn't matter as each container has its own internal DB server. Obviously this is wasteful and not best practice so I am looking for an alternative solution.

Ideally I want to keep one Docker image with one instance of the Node code, I know I could just rebuild the Docker image twice, with two different environment variables, but that doesn't scale well.

I was thinking I could use Docker networking to reference a single DB server container, and then for each container containing my Node code, have the entry point as a custom script that sets an argument passed to it as an environment variable before starting the Node server. That way whenever I run a container from that image, I pass a new DB name to it as an argument that the Node code will read. This way I could create unlimited containers from one image, all with unique databases but pointing to the same DB server.

I'm unsure if this is the best way though, and if there are any best practices that surround this. Can anyone help me with this?

Thanks

Jun 17, 2018 in Docker by Atul
• 10,240 points
901 views

1 answer to this question.

0 votes

What you can do is pass environment variables that point to the database when you run the app container.

docker run -e "DB_HOST=mongo" -e "DB_USER=user" ...

You can then modify your Node code to read these value from the environment variables. You need then to create a network and add you db container and app container to it. Now you can run the mongo database in a separate container named mongo and pass mongo as the host name for the db.

I hope this would help you.

answered Jun 17, 2018 by shubham
• 7,340 points

Related Questions In Docker

0 votes
1 answer

How to run a docker command from inside the container?

You must have come across the /var/run/docker.sock file, ...READ MORE

answered Jun 28, 2018 in Docker by Sophie may
• 10,620 points
3,553 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,611 views
+3 votes
12 answers

How to run multiple commands in docker at once?

In order to run multiple commands in ...READ MORE

answered Jul 19, 2018 in Docker by Sophie may
• 10,620 points
250,356 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,020 points
2,739 views
+2 votes
6 answers

copy directories in docker container excluding any one directory

One of the way could be copy ...READ MORE

answered Dec 10, 2018 in Docker by Prateek
35,059 views
0 votes
1 answer

Unalble to pull a new container after upgrading Helm.

If there are changes to roll out, ...READ MORE

answered Apr 12, 2018 in Docker by shubham
• 7,340 points
811 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