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 16, 2018 in Docker by Atul
• 10,240 points
25 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 16, 2018 by shubham
• 6,890 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
• 9,530 points
324 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
76 views
+1 vote
11 answers

How to run multiple commands in docker at once?

Try this: docker run image bash -c "cd ...READ MORE

answered Apr 17 in Docker by anonymous

edited Apr 17 by Kalgi 16,529 views
0 votes
1 answer
0 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
• 56,520 points
153 views
+2 votes
6 answers
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
• 6,890 points
66 views