3

i write dockerfile

EXPOSE 2181 2888 3888

and docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                          NAMES
abc644fe1ad0        00088267fb34        "/opt/startzookeeper…"   2 seconds ago       Up 1 second         2181/tcp, 2888/tcp, 3888/tcp   hopeful_curie

but when i telnet localhost 2181

Trying ::1... telnet: connect to address ::1: Connection refused Trying 127.0.0.1... telnet: connect to address 127.0.0.1: Connection refused telnet: Unable to connect to remote host

why i cannot telnet the exposed port? should i add what to dockerfile? thanks your any suggestion

王奕然
  • 3,891
  • 6
  • 41
  • 62
  • 1
    Possible duplicate of [What is the difference between "expose" and "publish" in Docker?](https://stackoverflow.com/questions/22111060/what-is-the-difference-between-expose-and-publish-in-docker) – David Maze Jul 14 '18 at 16:32

2 Answers2

3

EXPOSE is just a metadata added to the image (as noted in "Docker ports are not exposed").
It does not actually publish the port.

You need to make sure you docker run the image with -p option, in order to actually publish the container port to an host port.

-p=[]

Publish a container᾿s port or a range of ports to the host
format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
3

The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified. The EXPOSE instruction does not actually publish the port. . To publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports.

eg: docker run -d -p 2181:2181 <image>

Savio Mathew
  • 719
  • 1
  • 7
  • 14