0

I have installed MariaDB on Ubuntu (not as a Docker image). Then I have an image with a Spring Boot Application. This Spring Boot application should be reachable on port 8002 and be able to connect to MariaDB on the host system.

If I start docker run without --network=host, the Spring Boot application in the docker image cannot connect to the database.

If I use the --network=host parameter, the Spring Boot application can connect to the database, but the application is not reachable on port 8002.

docker run \
   -e "DB_URL=jdbc:mariadb://127.0.0.1:3306/app" \
   -e "DB_USERNAME=***" \
   -e "DB_PASSWORD=***" \
   -it \
   -p 8002:8080 \
   --network=host \
   repository/app:0.0.3
CONTAINER ID   IMAGE                   COMMAND            CREATED         STATUS          PORTS       NAMES
2fe96a608298   repository/app:0.0.3    "java -jar app…"   2 minutes ago   Up 2 minutes                hardcore_hopper

I have tried many. But I can't get the application to be reachable on a port.

Bademeister
  • 389
  • 1
  • 10
  • You do not need host networking for most typical applications. Among other problems with it, it outright just doesn't work if you're using Docker Desktop or a similar VM-based system. Do the other solutions to [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) help you establish a database connection (it will _not_ be on 127.0.0.1), in a way where normal Docker networking still works? – David Maze Aug 13 '23 at 17:52
  • This is not for production. I want to educate myself and better understand the topics of how to create and deploy Docker images/containers, containerisation, using cloud services and serverless. Step by step. I don't want to become a DevOps engineer or administrator, but I want to understand the concepts. You just learn more through trial and error. I also have a MariaDB running as a Docker image on my vServer on AWS. I would like to try ECS soon. But I wanted to test if you can also use a local database. My local development environments are Dockerised, so nothing runs on the host system. – Bademeister Aug 13 '23 at 18:41

1 Answers1

3

When you use host networking you are telling docker that you use the host's networking stack therefore you cannot expose ports on your container. Your application should be running as if it was directly running on your host, meaning you should be able to reach it at port 8080.

To quote the documentation on the host network driver:

If you use the host network mode for a container, that container’s network stack is not isolated from the Docker host (the container shares the host’s networking namespace), and the container does not get its own IP-address allocated. For instance, if you run a container which binds to port 80 and you use host networking, the container’s application is available on port 80 on the host’s IP address.

I am assuming you are on a Linux operating system, since host networking does not work on Windows machines.

The host networking driver only works on Linux hosts, and is not supported on Docker Desktop for Mac, Docker Desktop for Windows, or Docker EE for Windows Server.

Mushroomator
  • 6,516
  • 1
  • 10
  • 27