Yashika
Yashika

Reputation: 21

when I expose a port in a dockerfile and it build successfully but not responding in a server

When I run this command docker build -t my_image .

I got this as an output:

Sending build context to Docker daemon  2.048kB
Step 1/5 : FROM php:7.2-apache
 ---> f046c4ead123
Step 2/5 : MAINTAINER JLT 7 <j*******t.******@gmail.com>
 ---> Using cache
 ---> 2d04942bf1f3
Step 3/5 : RUN apt-get update
 ---> Using cache
 ---> 8f5c190e13ab
Step 4/5 : CMD ["echo","Hello World!!!...My frst message"]
 ---> Using cache
 ---> 9d67b4f4cf85
Step 5/5 : EXPOSE 8090
 ---> Using cache
 ---> 7ad8354944b2
Successfully built 7ad8354944b2
Successfully tagged my_image:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

But when I run this port on the server it is not responding...

Upvotes: 0

Views: 129

Answers (1)

thaJeztah
thaJeztah

Reputation: 29127

The EXPOSE Dockerfile instruction by itself does not publish a container's ports (see the Dockerfile reference section in the documentation; the instruction only adds metadata to the image that you built, and allows you to give an indication which ports the service running in your container listens on.

The EXPOSE instruction is optional, and even without setting that port, you are able to connect to a port that the container is listening on.

In order to make a port accessible;

  • first, make sure to have the service in your container listen on a port (your example Dockerfile is based on the official php:7.2-apache image, which listens on port 80 by default
  • make sure that the service in your container is listening on any IP-address (0.0.0.0), and not on localhost / 127.0.0.1 (because localhost is "localhost" inside the container, which means that if your service is listening on localhost, it will only be accessible from inside the container itself)
  • when running the container, you can map the container's port to the port that your want it accessible on ("port mapping") (also see the networking section in the documentation.

The -p / --publish option has a shorthand, and an advanced syntax; I'll use the shorthand syntax in my examples; this notation is using the format: -p <host-port>:<container-port>

For example, if your container is listening on port 80, and you want to publish (or "map") that port to port 8090 on your host;

docker run -d -p 8090:80 myimage

Your container can now be reached at:

  • http://<ip-address of your host>:8090
  • (depending on your setup): http://localhost:8090

Upvotes: 1

Related Questions