How to reach Docker virtual machines?

Before asking the question I would like to make a premise. I am not a computer scientist and I have no network experience.
I created a .war file on Windows that uses Java 11, Tomcat 9 and PostgreSQL. I have also used Spring Boot 2 and Maven but I believe this is not important for this discussion.
With my IDE and PostgreSQL installed on Windows, with Java, Tomcat, Maven downloaded on my computer I can see my application in the browser at the following address:
https: // localhost: 8443 / it
On Windows to reach the database I use the following link:
spring.datasource.url = jdbc: postgresql: // localhost: 5432 / databasename
These days I have installed the Ubuntu 22.04 system on vmware. Later I installed Docker on Ubuntu 22.04.
My ultimate goal is to test my .war file on Ubuntu 22.04 using Docker.
I would like to write the following link on the Linux browser:
https: // localhost: 8443 / it
and see, thanks to Docker, my application as on Windows.
If it is not possible to use localhost on Linux or there is another practice, I can change direction.
Using localhost on Linux is also convenient because so I donā€™t have to recreate a new .war but I can use the one I use for Windows.
Iā€™d like to build the following links and services:

https: // localhost: 8088 -> Apache Server (maybe I won't use it)
https: // localhost: 8443 -> Tomcat
https: // localhost: 5432 -> PostgreSQL
https: // localhost: 80 -> pgAdmin

(I have chosen random numbers for the doors, if it is not appropriate I can change.)
I also have another need. I would like that when I open Linux I find the Apache Server, Tomcat, PostgreSQL and pgAdmin containers all active.
I havenā€™t done any vmware network configuration. I installed the virtual machine and Ubuntu by pressing only on ā€˜nextā€™.
I wrote this code but it doesnā€™t work.
Tomcat

/home/gi/Dropbox/SERVER/DOCKER-LINUX/Docker-Tomcat/Dockerfile

FROM tomcat:9.0.22-jdk13-openjdk-oracle
LABEL Author="Nome Cognome"
EXPOSE 8080
RUN rm -fr /usr/local/tomcat/webapps/ROOT
COPY ./*.war /usr/local/tomcat/webapps/ROOT.war
CMD ["catalina.sh", "run"]

/home/gi/Dropbox/SERVER/DOCKER-LINUX/Docker-Tomcat/tomcat_build.txt

cd /home/gi/Dropbox/SERVER/DOCKER-LINUX/Docker-Tomcat
docker build -t tomcat-eb:v.9.0.17 .
docker run -it --rm --name tomcat-eb-container -p 8888:8080 tomcat-eb:v.9.0.17

/home/gi/Dropbox/SERVER/DOCKER-LINUX/Docker-Tomcat/esercitazione.1.maven.war

http://localhost:8888/

PostgreSQL

/home/gi/Dropbox/SERVER/DOCKER-LINUX/Docker-PosgreSQL/Dockerfile

FROM kartoza/postgis:9.6-2.4
LABEL Author="Nome Cognome"
EXPOSE 5432
VOLUME create pg_data

/home/gi/Dropbox/SERVER/DOCKER-LINUX/Docker-PosgreSQL/postgresql_build.txt

cd /home/gi/Dropbox/SERVER/DOCKER-LINUX/Docker-PosgreSQL
docker build -t postgresql-postgis-eb:9.6-2.4 .
docker run --name=postgis -d -e POSTGRES_USER=eb -e POSTGRES_PASS=password -e POSTGRES_DBNAME=gis -e ALLOW_IP_RANGE=0.0.0.0/0 -p 5432:5432 -v pg_data:/var/lib/postgresql --restart=always postgresql-postgis-eb:9.6-2.4

pgAdmin

docker pull dpage/pgadmin4
docker run --name=contenitore_pgadmin -p 80:80 \
    -e 'PGADMIN_DEFAULT_EMAIL=admin@gmail.com' \
    -e 'PGADMIN_DEFAULT_PASSWORD=password' \
    -d dpage/pgadmin4


The problems are as follows:
1) Why canā€™t I connect to the ā€˜gisā€™ database with pgAdmin?
2) I would like the service to be automatically active when I open the VM? Itā€™s possible? How can I do?

Thanks

Good to know that you actual work on Windows, but want to test everything inside the Linux VM.

Now here is the thing with localhost: localhost on a docker host is local to the host, localhost inside a container is local to the container itself. Thus your list of ā€œI want to access those services from the Linux vmā€ does work for published ports, BUT does not for communication amongst the containers.

I strongly suggest to create your containers using a docker compose file, as it makes the wiring easier and preserves the configuration in the file. It also creates a user defined network, which provides dns-based service discovery, which allows to communicate containers within the same user defined network, using the service or container name and the port the application is listening inside the container, to communicate with each other.

Since you donā€™t provide a custom network, your containers will use the default network, which does not provide service disovery. You either need to create a custom network, and use the --network parameter when creating the containers OR use --link target-container-name:alias (of course those are just placeholders for the real container name and an alias of your choice) and then address the target container either by its container-name or alias and the port the application is listending inside the target container.

1 Like

Thank you very much, thatā€™s exactly how you write. I have installed Squirrel SQL manually and am able to connect to the DBMS using localhost. To start Tomcat in bacground just use -d.
Now I try to create a new Docker project with the network you speak of. Iā€™ll post the code by today.

I wrote this new project using Docker Compose but I am still having difficulty connecting to the database. The application starts but when I try to login it fails.
In the .war the login credentials are as follows:

{
	"url": "jdbc:postgresql://localhost:5432/gis",
	"user": "eb",
	"password": "password"
}

In the database, the tables and records of the users are present. I checked with Squirrel SQL.
My new project is as follows:

/home/gi/Dropbox/SERVER/DOCKER-LINUX/Docker-Tomcat-PosgreSQL/postgresql-postgis/Dockerfile

FROM kartoza/postgis:9.6-2.4
LABEL Author="Nome Cognome"
EXPOSE 5432
VOLUME create pg_data

/home/gi/Dropbox/SERVER/DOCKER-LINUX/Docker-Tomcat-PosgreSQL/tomcat/Dockerfile

FROM tomcat:9.0.22-jdk13-openjdk-oracle
LABEL Author="Nome Cognome"
EXPOSE 8080
COPY ./*.war /usr/local/tomcat/webapps/
CMD ["catalina.sh", "run"]

/home/gi/Dropbox/SERVER/DOCKER-LINUX/Docker-Tomcat-PosgreSQL/docker-compose.yaml

version: '3.8'

services:
  tomcat:
    build: ./tomcat
    image: image-tomcat-eb:v.1.0
    container_name: container-tomcat-eb
    ports:
      - 8888:8080
    restart: on-failure
    depends_on:
      - postgresql-postgis
    networks:
      - eb

  postgresql-postgis:
    build: ./postgresql-postgis
    image: image-postgresql-postgis-eb:v.1.0
    container_name: container-postgresql-postgis-eb
    ports:
      - 5432:5432
    volumes:
      - pg_data:/var/lib/postgresql
    environment:
      POSTGRES_USER: "eb"
      POSTGRES_PASS: "password"
      POSTGRES_DBNAME: "gis"
      ALLOW_IP_RANGE: "0.0.0.0/0"
    restart: always
    networks:
      - eb

volumes:
  pg_data:

networks:
  eb:

Like I said: localhost inside a container is not the same localhost of another container or the localhost of the host. You need to change it to "url": "jdbc:postgresql://postgresql-postgis:5432/gis" as you namesd the service postgresql-postgis.

Also instead of hard coding the configuration you should realy adapt the intendended spring boot approach. A short google search for application.properties and/or application.yaml shoud give you plenty of blog posts that describe how itā€™s implemented and how to override default values with environment variables. Once its implemented you simply add the environment variables to your compose file and have all the flexibility you need.

1 Like

A thousand thanks!!!
Everything works!!!
Wawwww !!!
Iā€™m very happy!!!
Today I have a commitment but tomorrow Iā€™ll try to read here:

Maybe I prefer this approach because if I decide to abandon Spring Boot in the future I donā€™t have to get my hands on Docker again.

I havenā€™t had time to read the guide for Spring Boot yet and first Iā€™d like to improve my script with Docker for applications built with JSTL and without Spring Boot. I wrote this new code and it works perfectly but I would like pgAdmin to automatically connect to the database inside the pg_data volume. Is it possible to do this? How you do it?

In the .war file:

{
	"url": "jdbc:postgresql://localhost:5432/gis",
	"user": "eb",
	"password": "password"
}

/home/gi/Dropbox/SERVER/DOCKER-LINUX/Tomcat-PosgreSQL-PostGIS-PgAdmin/postgresql-postgis/Dockerfile

FROM kartoza/postgis:9.6-2.4
LABEL Author="Nome Cognome"
EXPOSE 5432
VOLUME create pg_data

/home/gi/Dropbox/SERVER/DOCKER-LINUX/Tomcat-PosgreSQL-PostGIS-PgAdmin/tomcat/Dockerfile

FROM tomcat:9.0.22-jdk13-openjdk-oracle
LABEL Author="Nome Cognome"
EXPOSE 8080
COPY ./*.war /usr/local/tomcat/webapps/
CMD ["catalina.sh", "run"]

/home/gi/Dropbox/SERVER/DOCKER-LINUX/Tomcat-PosgreSQL-PostGIS-PgAdmin/pgadmin

FROM dpage/pgadmin4:latest
LABEL Author="Nome Cognome"
EXPOSE 80

/home/gi/Dropbox/SERVER/DOCKER-LINUX/Tomcat-PosgreSQL-PostGIS-PgAdmin/docker-compose.yaml

version: '3.8'

services:
  postgresql-postgis:
    build: ./postgresql-postgis
    image: image-postgresql-postgis-eb:v.1.0
    container_name: container-postgresql-postgis-eb
    ports:
      - 5432:5432
    volumes:
      - pg_data:/var/lib/postgresql
    environment:
      POSTGRES_USER: "eb"
      POSTGRES_PASS: "password"
      POSTGRES_DBNAME: "gis"
      ALLOW_IP_RANGE: "0.0.0.0/0"
    restart: always
    networks:
      - eb

  tomcat:
    build: ./tomcat
    image: image-tomcat-eb:v.1.0
    container_name: container-tomcat-eb
    ports:
      - 8888:8080
    depends_on:
      - postgresql-postgis
    restart: on-failure
    networks:
      - eb

  pgadmin:
    build: ./pgadmin
    image: image-pgadmin-eb:1.0
    container_name: container-pgadmin-eb
    ports:
      - 5050:80
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@gmail.com
      PGADMIN_DEFAULT_PASSWORD: password
    depends_on:
      - postgresql-postgis
    restart: always
    networks:
      - eb

volumes:
  pg_data:

networks:
  eb:

I assume you mean you want pgadmin to automaticly connect to the database from thepostgresql-postgis service. I doubt pgadmin can directly interact with the binary database files - last time I checked it was ā€œjustā€ a management application that interacts with a postgres database server, not with the binary files the database server uses to store the content.

I see two options:

  • You wait untill someone who actualy uses pgAdmin responds to your post
  • You read the dockerhub description of the pgAdmin image to identify if the iamge supports it and you learn how to configure the pgAdmin service yourself.

FYI: the topic in the thread shifted again, which lowers the chances that someone who might know the answer might never see your question, as the thread title is about Docker virtual machines.

1 Like