2

I built a docker image from a dockerfile. I see the image was built successfully ( $ docker images) and when I use this command to run the image as a container :

$ docker run -i -t 8dbd9e392a96 

My application was running successfully, but when I'm trying to open I've this message

This site can’t be reached

This is my list of images :

 $ docker  images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
blog                latest              b9c52b9f2999        About an hour ago   143MB
openjdk             8-jre-alpine        14a48fdee8af        3 days ago          83MB

and my containers list :

$ docker  ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4dbb68c87813        b9c52b9f2999        "./entrypoint.sh"   27 minutes ago      Up 27 minutes       8080/tcp            dazzling_shirley

I got this result after running the app using docker image :

    ----------------------------------------------------------
        Application 'blog' is running! Access URLs:
        Local:          http://localhost:8080
        External:       http://172.17.0.2:8080
        Profile(s):     [dev, swagger]
----------------------------------------------------------

I dunno why the app didn't work any help please ?? !

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Ikbel
  • 1,817
  • 1
  • 17
  • 30
  • Possible duplicate of [Connecting to Postgresql in a docker container from outside](https://stackoverflow.com/questions/37694987/connecting-to-postgresql-in-a-docker-container-from-outside) – David Maze Sep 08 '18 at 17:29
  • 1
    (While that question has some PostgreSQL specifics, its general advice about "use `docker run -p`" is universal across all services running in Docker.) – David Maze Sep 08 '18 at 17:29

2 Answers2

1

You have to map the docker container port to the host port. The reason is docker container is an isolated environment and it's public ip is same as that of the host machine. You have to ensure that host knows when to redirect requests to the container. So when you map the host port to docker container port, all the requests coming to HOST_IP_ADDRESS:HOST_PORT are redirected to the docker container whose port is mapped to the HOST_PORT.

You do that by using -p flag while using the docker run command as shown below:

docker run -it -p 8080:8080 IMAGE_NAME

Now all the requests coming to localhost:8080 will be directed to application running in container.

Yug Singh
  • 3,112
  • 5
  • 27
  • 52
  • I've got the same display , even after changing the command but i used `docker run -it -p 8080:8080 IMAGE_Id` coz the image _name didn't work and gave me this error : ** Unable to find image latest:latest locally docker: Error response from daemon: pull access denied for latest, repository does not exist or may require 'docker login'. See 'docker run --help ** – Ikbel Sep 08 '18 at 17:27
  • 1
    The error means the image you are trying to access is private. It's similar to what happens in Github, If it is private repo you have to provide credentials and should have access to the repo. Do as mentioned in the error. use `docker login` and provide your credentials . If you have access the docker run command will be able to pull image from docker hub – Yug Singh Sep 08 '18 at 17:33
  • 1
    also when you use docker run command docker checks for image locally as it doesn't make sense to pull image from docker hub if it exists locally – Yug Singh Sep 08 '18 at 17:33
  • to make dockerfile:push automatically run in the install or deploy lifecycle phases I added this code : ` default install build push ` But I got this error : `Caused by: com.spotify.docker.client.exceptions.DockerException: denied: requested access to the resource is denied` – Ikbel Sep 08 '18 at 17:55
0

Are you using docker toolbox for windows. Docker toolbox on windows runs docker in a Linux based virtualbox vm. You might have to open the port in the virtual box VM itself. So your host os can access the guest os's port.

See https://www.simplified.guide/virtualbox/port-forwarding

Riz
  • 1,055
  • 11
  • 18