Docker Community Forums

Share and learn in the Docker community.

Unable to start postgresql and apache from a single dockerfile

Hello guys, sinc two days now i created an Angular application which consumes a data from a php web service connected to a postgres database. I want to dockerize the application as a single container so i created a single dockerfile (content below), but when i run the container, the web service(deployed in apache) is not working and unable to connect to postgres. Only the angular(deployed in nginx) is working. Please i really need help, what i’m i doing wrong :

 ## STAGE 2 : Run nginx to serve application ##
FROM node:12 as builder

WORKDIR .

FROM yvantakoumbo94/postgres-gabon-data:v3

USER postgres
ENV PGDATA /postgres
ENV POSTGRES_DB_HOST postgres
ENV POSTGRES_DB_PORT 5432
ENV POSTGRES_DB_USER postgres
ENV POSTGRES_DB_PASSWORD pgadmin@
ENV POSTGRES_DB_NAME terres_gabon

FROM  php-apache-terres-api:v4

RUN service apache2 start

WORKDIR .
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf

RUN rm -rf /usr/share/nginx/html/*
COPY  /dist/terreGabonaise /usr/share/nginx/html/


ENTRYPOINT ["/docker-entrypoint.sh"]
EXPOSE 4200
EXPOSE 80
EXPOSE 5432

CMD ["postgres"]

TL;DR

  1. Use 172.17.0.0/16 as IP address range, not 172.17.0.0/32 .
  2. Don’t use localhost to connect to the PostgreSQL database on your host, but the host’s IP instead. To keep the container portable, start the container with the --add-host=database:<host-ip> flag and use database as hostname for connecting to PostgreSQL.
  3. Make sure PostreSQL is configured to listen for connections on all IP addresses, not just on localhost . Look for the setting listen_addresses in PostgreSQL’s configuration file, typically found in /etc/postgresql/9.3/main/postgresql.conf (credits to @DazmoNorton).

Long version

172.17.0.0/32 is not a range of IP addresses, but a single address (namly 172.17.0.0 ). No Docker container will ever get that address assigned, because it’s the network address of the Docker bridge ( docker0 ) interface.

When Docker starts, it will create a new bridge network interface, that you can easily see when calling ip a :

$ ip a
...
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN 
    link/ether 56:84:7a:fe:97:99 brd ff:ff:ff:ff:ff:ff
    inet 172.17.42.1/16 scope global docker0
       valid_lft forever preferred_lft forever

As you can see, in my case, the docker0 interface has the IP address 172.17.42.1 with a netmask of /16 (or 255.255.0.0 ). This means that the network address is 172.17.0.0/16 .

The IP address is randomly assigned, but without any additional configuration, it will always be in the 172.17.0.0/16 network. For each Docker container, a random address from that range will be assigned.

This means, if you want to grant access from all possible containers to your database, use 172.17.0.0/16 .

Admin
Apps4Rent.