general

How to expose a port for listener in a Docker container?

Benjamin Alexander faced an issue with a Docker image not having a port for the listener. He resolved it by adding port 8080. How can one expose a port for a listener in a Docker container?

Be

Benjamin Alexander

Asked on Feb 22, 2024

  • Use the -p flag in the docker run command to expose a port from the container to the host environment.
  • Specify the port mapping in the format hostPort:containerPort/tcp.
  • Ensure that the service inside the container is listening on the specified container port.

Example:

docker run -d -p 8080:8080/tcp my_image_name

This command exposes port 8080 from the container to the host environment.

Feb 22, 2024Edited by