简体   繁体   中英

Install PostgreSQL into a asp.net core container

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM