5

I'm new with docker and I'm probably missing a lot, although i went through the basic documentation and I'm trying to deploy a simple Spring Boot API

I've deployed my API as a docker-spring-boot .jar file , then i installed docker and pushed it with the following commands:

  • sudo docker login
  • sudo docker tag docker-spring-boot phillalexakis/myfirstapi:01
  • sudo docker push phillalexakis/myfirstapi:01

Then i started the API with the docker run command:

sudo docker run -p 7777:8085 phillalexakis/myfirstapi:01

When i visit localhost:7777/hello I'm getting the desired response

This is my Dockerfile

FROM openjdk:8
ADD target/docker-spring-boot.jar docker-spring-boot.jar
EXPOSE 8085
ENTRYPOINT ["java","-jar","docker-spring-boot.jar"]

Based on this answered post this the command to get the ip address

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

So, i run it with container_name_or_id = phillalexakis/myfirstapi:01 and I'm getting this error

Template parsing error: template: :1:24: executing "" at <.NetworkSettings.Networks>: map has no entry for key "NetworkSettings"

If i manage somehow to get the IP will i be able to visit it and get the same response?

This is how i have it in my mind: ip:7777/hello

Phill Alexakis
  • 1,449
  • 1
  • 12
  • 31

2 Answers2

10

You have used the image name and not the container name.
Get the container name by executing docker ps.
The container ID is the value in the first column, the container name is the value in the last column. You can use both.

Then, when you have the IP, you will be able to access your API at IP:8085/hello, not IP:7777/hello

The port 7777 is available on the Docker Host and maps to the port 8085 on the container. If you are accessing the container directly - which you do, when you use its IP address - you need to use the port that the container exposes.


There is also another alternative:
You can give the container a name when you start it by specifying the --name parameter:

sudo docker run -p 7777:8085 --name spring_api phillalexakis/myfirstapi:01

Now, from your Docker host, you can access your API by using that name: spring_api:8085/hello

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • 1
    okay so, i got the `IP` as you correctly pointed out. although i'm unable to connect at `IP:8085/hello` – Phill Alexakis Aug 25 '19 at 17:33
  • @PhillAlexakis: Are you trying to connect from your Docker Host? – Daniel Hilgarth Aug 25 '19 at 17:35
  • how can i find that out? i think i'm visiting it from the client side , my browser – Phill Alexakis Aug 25 '19 at 17:41
  • That's not gonna work. The IP address of the container is only available from the Docker Host, i.e. from the machine that you executed `docker run` on. If you want make it available on the internet, you somehow need to route the traffic from the internet to the container. If the Docker host is available via the internet, one option is to use it's internet address and the port 7777 (don't forget to open it in the firewall). – Daniel Hilgarth Aug 25 '19 at 17:47
  • oh alright, so i'm accessing it from the Docker host, I'm on the machine that i executed `docker run` – Phill Alexakis Aug 25 '19 at 17:52
  • note that `sudo docker run -p 7777:8085 --name spring_api phillalexakis/myfirstapi:01` works the same with `sudo docker run -p 7777:8085 phillalexakis/myfirstapi:01` – Phill Alexakis Aug 25 '19 at 17:53
  • about making it available over the internet, do you think this link might work? https://docs.docker.com/v17.09/engine/userguide/networking/ , is it just for internal IP's? – Phill Alexakis Aug 26 '19 at 11:05
  • 1
    No, this won't work. Docker networks are for Docker internal organization purposes. – Daniel Hilgarth Aug 26 '19 at 18:07
3

You should never need to look up that IP address, and it often isn't useful.

If you're trying to call the service from outside Docker space, you've done the right thing: use the docker run -p option to publish its port to the host, and use the name of the host to access it. If you're trying to call it from another container, create a network, make sure to run both containers with a --net option pointing at that network, and they can reach other using the other's --name as a hostname, and the container-internal port the other service is listening on (-p options have no effect and aren't required).

The Docker-internal IP address just doesn't work in a variety of common situations. If you're on a different host, it will be unreachable. If your local Docker setup uses a virtual machine (Docker Machine, Docker for Mac, minikube, ...) you can't reach the IP address directly from the host. Even if it does work, when you delete and recreate the container, it's likely to change. Looking it up as you note also requires an additional (privileged) operation, which the docker run -p path avoids.


The invocation you have matches the docker inspect documentation (as @DanielHilgarth notes, make sure to run it on the container and not the image). In the specific situation where it will work (you are on the same native-Linux host as the container) you will need to use the unmapped port, e.g. http://172.17.0.2:8085/hello.

David Maze
  • 130,717
  • 29
  • 175
  • 215