0 1
Disclosure: This page uses affiliate links.When you click an affliate link,we get a small compensation at no cost to you.Thanks for your support!Learn more
Read Time:4 Minute, 7 Second

There could be instances where you want to connect to Postgresql database on the host from your containers. In this post, we take look at configuration steps on how to connect to Postgresql running on host from your Docker containers.

We are going to use Docker Compose tool to define and run multi-container applications. We would be defining compose configuration as YAML file for our application’s services/networks/volumes etc., and then with a single command, we can create and start all the services from the configuration.

Here are the key steps :

  1. Define Dockerfile for your app’s environment.
  2. Define docker-compose.yml for the services that make up your app services.
  3. Configure Postgresql to able to connect from Docker containers.
  4. Run docker-compose up and Compose starts and runs your entire app.

This quickstart assumes basic understanding of Docker concepts, please refer to earlier posts for understanding on Docker & how to install and containerize applications.

In the next section, we can look at how to define services in compose file for sample application with Angular as front end, Spring Boot as API, and for database as Postgres but this will be located on the host.

#1.Definition of Docker files

We have already defined below docker files in the previous posts herehere. Create a new project directory and copy all dockerfiles to this folder.

Subscribe to Emails

More than 100,000 subscribers across the world trust & read Upnxtblog articles. Subscribe to emails updates!
* indicates required

Angular Application Docker file

FROM httpd:2.4
#copy angular dist folder to container 
COPY dist/ /usr/local/apache2/htdocs/

#copy htaccess and httpd.conf to container
COPY .htaccess /usr/local/apache2/htdocs/
COPY httpd.conf /usr/local/apache2/conf/httpd.conf

#change permissions
RUN chmod -R 755 /usr/local/apache2/htdocs/

#expose port

Spring Boot API Docker file

FROM openjdk:8-jre
#add required jars
ADD spring-boot-rest-postgresql-0.0.1-SNAPSHOT.jar spring-boot-rest-postgresql-0.0.1-SNAPSHOT.jar

#expose port
#cmd to execute
ENTRYPOINT ["java","","-jar","/spring-boot-rest-postgresql-0.0.1-SNAPSHOT.jar"]

#2.Define services in Docker compose file

Create a file called docker-compose.yml in your project directory and paste the following:

version: '3'
      context: .
      dockerfile: UIDockerfile
      - '4200:4200'
      - samplenet
      - 'api:api'
     context: .
     dockerfile: AppDockerfile
     - '8080:8080'
    - samplenet
samplenet: null


Above compose file defines 2 services as below:

Top online courses in Office Productivity
  • ui : This is for angular application,it uses an image that’s built from the UIDockerfile in the current directory and forwards the exposed port 4200 on the container to port 4200 on the host machine.
  • api : This is for spring boot application, it uses an image that’s built from the AppDockerfile in the current directory and forwards the exposed port 8080 on the container to port 8080 on the host machine.
  • All the above services uses samplenet network
  • depends_on denotes the service dependencies. When you start the services, compose would start the dependent services as well.
  • If you have noticed, we haven’t created service for Postgresql application instead we would be using Postgresql database installed on the host.
  • Also on your SpringBoot configuration ( file) specify the external IP address of the database host like  spring.datasource.url=jdbc:postgresql://<IP Address>:5432/postgres

#3.Configure Postgresql to able to connect from Docker containers

Assuming Postgresql is already installed on the host machine, follow the below steps to configure the listen_addresses on postgresql.conf to accept all connections.

Image – Modify postgresql.conf

Also edit pg_hba.conf  file to add entry for the host connections like below

host        all         all           md5

host connections
Image – Add entries for host connections

#4.Build and run your app with Compose

Recover over 700 unique file formats. Fully recover documents, emails, photos, videos, audio files and more. Supports 8 different data lost scenarios: deleted files recovery, recycle bin recovery, formatted drive recovery, lost partition recovery, externa

Once you have completed the above configurations,next step is to start the application.In the current project directory, run docker-compose up .Compose pulls all the required Docker image, builds an image for your code, and starts the services you defined.

Image – Start containers using docker-compose up command
Image – Both containers are up

Congrats! today we have learned how to configure Postgresql running on the host to be able to connect from Docker containers.

Like this post? Don’t forget to share it!

Additional Resources:

About Post Author


Allo! My name is Karthik,experienced IT professional.Upnxtblog covers key technology trends that impacts technology industry.This includes Cloud computing,Blockchain,Machine learning & AI,Best mobile apps, Best tools/open source libs etc.,I hope you would love it and you can be sure that each post is fantastic and will be worth your time.
0 %
50 %
0 %
0 %
0 %
50 %
How do I connect to Postgresql running on host from Docker container
Article Name
How do I connect to Postgresql running on host from Docker container
In this post,we take look at configuration steps on how to connect to Postgresql running on host from your Docker containers.
Publisher Name
Publisher Logo

Average Rating

5 Star
4 Star
3 Star
2 Star
1 Star

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.