Wednesday, September 11, 2019

Docker: Exposing and Publishing Container Ports

===Exposing Container Ports===

docker container run -d nginx

docker container ls

curl localhost

docker inspect container container_id

curl 172.17.0.2

docker container run -d --expose 3000 nginx

docker container ls

docker rm -f container_id

docker container run -d --expose 3000 -p 80:3000 nginx

docker container ls

curl localhost:3000
connection refused

docker container rm -f container_id


docker container run -d --expose 3000 -p 8080:80 nginx

curl localhost:8080

docker container run -d -p 8081:80/tcp -p 8081:80/udp nginx


curl localhost:8081

docker container run -d -P nginx

docker container ls

curl localhost:32768

docker container port container_id

===Summary===

Exposing:
  • Expose a port or a range of ports
  • This does not publish the port
  • Use --expose [PORT]
docker container run --expose 1234 [IMAGE]
Publishing:
  • Maps a container's port to a host`s port
  • -p or --publish publishes a container's port(s) to the host
  • -P, or --publish-all publishes all exposed ports to random ports
docker container run -p [HOST_PORT]:[CONTAINER_PORT] [IMAGE]
docker container run -p [HOST_PORT]:[CONTAINER_PORT]/tcp -p [HOST_PORT]:[CONTAINER_PORT]/udp [IMAGE]
docker container run -P
Lists all port mappings or a specific mapping for a container:
docker container port [Container_NAME]