2

I'm a beginner to Docker world. I've created an ASP.net Core Web API and ran successfully on localhost with PostgreSQL. Now, I've added the WebAPI into a Docker container. I'm using Windows 10, Visual Studio 2015, Docker for Windows 10 and Powershell. Now the problem is, I can't figure it out the following.

  1. How to setup PostgreSQL inside my container?
  2. How to create a DB and Tables?
  3. How to connect the above DB with the WebAPI?

It's giving a "Connection refused" error when I try to call the API. The DB connection fails. I believe it's because there's no DB inside the container. I couldn't find a proper way for a beginner to understand this procedure. Any help with steps would be really helpful.

8
  • 1
    you probably shouldn't use containers for DB Commented Mar 7, 2017 at 12:49
  • @4c74356b41 Just commenting like this is completely useless. Mention what you think is better or your suggestions. Commented Mar 7, 2017 at 13:14
  • its not, it gives you an idea that this might lead to disaster Commented Mar 7, 2017 at 13:14
  • I'm expecting a solution to my problem. Not just a mere idea. I don't even get an idea from your comment tbh. Maybe because I'm new to docker which I've mentioned upfront. So you should be mindful. Commented Mar 7, 2017 at 13:16
  • especially if you new to docker, there is no way for you to know that hosting databases in docker is a best practice, it can be done, but I'd advice against it Commented Mar 7, 2017 at 13:18

1 Answer 1

4

You want to run each application in a seperate container otherwise you end up with the same application hell as before.

But you have to connect the containers together using a network.

steps to do so:

  1. create a bridge network with a name: bride-network: docker network create bride-network
  2. run container named postgres with postgres database inside. make sure you add it to the network: --network bridge-network
  3. add your application ad docker container: also add it to this network with --network bridge-network

Now your application can reach the postgres database with the hostname http://postgres (basically http://[container-name]).

To do this in 1 go you can create a docker-compose.yml file and run docker-compose -f yourfile.yml up to create everything at once

Why split containers

The idea of docker is that 1 container runs 1 application. When this process stops docker knows that the container has stopped/died. If you add multiple apps in 1 container docker will not know. Also it could be that apps create problems, eg: what if both apps register to listen to the same ports?

Another problem case is when you want to scale up: create duplicate containers. do you then als want to scale up the database 1=1 with your app? where is your single source of truth then?

So you split up 1 app per container:

  • Every app runs in its own sandbox, and cannot be hurt/communicate by any other app/container only over http/udp.
  • You can scale them up (horizontally) individually.
  • You can configure them individually through environment variables.
Sign up to request clarification or add additional context in comments.

11 Comments

tbh, this answer is pretty useless, since running a database in a container is madness
@4c74356b41 it is not useless at all. Even though you can't scale it (replicate it) you can still run it as an atomic application and run it in the same cluster reducing network latency.
so whats the point, you can only do that if can throw away all the data in that db and not worry about it, otherwise you are doomed
@4c74356b41 you never heard of volume mapping, that's how we do data persistancy in kubernetes/cloud environments.
I've heard about it, but hosting databases in containers is ASKING for trouble.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.