John Student
John Student

Reputation: 308

How to make a simple docker service accessible from external hosts

First, I inform you that i'm currently student in software development. So I beg for your tolerance regards to my question.

I'm dockerising one of my java web application. It's working fine on my Windows 10 host, connecting in localhost. But I need to make my web application accessible from other hosts, like I would do in a real life scenario.

[EDIT]: in this post docker-compose assign lan ip to service, they use some network config in docker-compose.yml. But as I said, I don't understand it enough to adapt it to my case :/

Currently, I can access my web app only from the host running the docker containers, with this url : "http://localhost:8080/public/showAtlas"

What I would like is my web app to be accessible in any browser with an url like : "http://xxx.xxx.xxx.xxx:8080/public/showAtlas" where xxx.xxx.xxx.xxx is a public IP adress that I could define in the dockerfile or docker-compose.yml

Is that possible with few config lines or is it tricky? I guess many will want to tag my question as duplicate, but I didn't find any simple answer to this simple usecase.

Could someone please help me to get this working?

I looked many online resources but the solutions are too "complex" for me to adapt it to my case.

Dockerfile of my springboot app:

FROM store/oracle/serverjre:8
VOLUME /tmp
ADD webapp/output/webapp.jar webapp.jar
ENTRYPOINT ["java", "-jar", "webapp.jar"]

Dockerfile of my database:

FROM postgres:latest
COPY db_wtc.sql /docker-entrypoint-initdb.d/init.sql

My docker-compose.yml:

version: '3.7'

services:
    db-wtc:
        build:
            context: .
            dockerfile: Dockerfile-db
        container_name: cont-db-wtc
        volumes:
            - db_data:/var/lib/postgres/data
        restart: unless-stopped
        ports:
            - 5432:5432
        expose:
            - 5432
        environment:
            POSTGRES_USER: admin_wtc
            POSTGRES_PASSWORD: 123
            POSTGRES_DB: db_wtc
        networks:
            - wtc-network

    wtc:
        depends_on:
            - db-wtc
        build:
            context: .
            dockerfile: Dockerfile
        container_name: cont-wtc
        ports:
            - 8080:8080
        expose:
            - 8080
        environment:
            SPRING_DATASOURCE_USERNAME: admin_wtc
            SPRING_DATASOURCE_PASSWORD: 123
            SPRING_DATASOURCE_URL: jdbc:postgresql://db-wtc:5432/db_wtc
        networks:
            - wtc-network

networks:
    wtc-network:

volumes:
  db_data:

No particular error. I just can't access my docker service from any external host.

Thank you guys very mush for your time!

Upvotes: 3

Views: 1088

Answers (3)

John Student
John Student

Reputation: 308

Indeed, it was just a problem due to my home network. I rent a vps and it worked like a charm, using the IP of the vps. I didn't need to change anything in the above docker files. Thanks guys!

Upvotes: 0

Wesgur
Wesgur

Reputation: 3227

An alternative could be using tunneling. If you are not trying to deploy for production try using ngrok. (https://ngrok.com/)

All you have to do is install ngrok and run

$ ngrok http 8080

And it will provide you with an address that you can use temporarily.

Upvotes: 0

The Fool
The Fool

Reputation: 20448

As I derive from the comments, this is not a docker-compose issue! The problem is that you want to access your "home" network from the public internet wich is not possible by default. You need to configure your modem to allow this.

Upvotes: 1

Related Questions