Docker expose

svg viewer

What is Docker expose command?

The expose command is used to make a Docker container bind and listen on a specific port. It is up to the user to decide whether they want to listen on a TCP or a UDP connection – the connection is TCP by default. The user can then specify the reliability protocol by adding either /tcp or /udp after the port number.

Format

# Exposes port 80 using TCP protocol

EXPOSE 80
# OR
EXPOSE 80/tcp

# Exposes port 80 using UDP protocol
EXPOSE 80/udp

Expose vs. Publish

The user has three options when running a Docker container:

  • Neither expose nor publish
  • Only expose
  • Both expose and publish

Neither expose nor publish

The user restricts the communication with the container from within the container only. This approach is adopted by the user if the container at hand is running isolated from all the other containers.

Only expose

The user restricts the communication with the container from within the Docker. It helps to establish inter-container communication in Docker.

Both expose and publish

After publishing(-p) the container, the user allows communication with the container from outside the Docker.

To publish a container, use the -p flag with the run command.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved