Make sure to have:
Small codebase with blogpost: How to connect to the Docker host from inside a Docker container?
spring run helloWorld.groovy
A Spring Boot application will start at port 8080 on your host: http://localhost:8080
docker run -it --add-host=host.docker.internal:host-gateway ubuntu bash
You have to specify the run flag --add-host=host.docker.internal:host-gateway
(so just executing docker run -it ubuntu bash
) otherwise your Ubuntu container can't resolve
host.docker.internal
and you will get an error like:
curl: (6) Could not resolve host: host.docker.internal
apt update && apt install curl -y
curl http://host.docker.internal:8080
Response:
Hello world container. You are able to reach the Docker host!
Example config using Docker compose
version: "3.8"
services:
ubuntu:
image: ubuntu
container_name: ubuntu
extra_hosts:
- "host.docker.internal:host-gateway"
command: sleep infinity
Start the Ubuntu container using Docker compose:
docker-compose up -d
Attach to the Ubuntu container started using Docker compose:
docker exec -it ubuntu bash
Shut down the Ubuntu container:
docker-compose down -v