What is the difference between “expose” and “publish” in Docker?
Feature | Expose | Publish |
---|---|---|
Purpose | Specifies ports exposed from the container to the container network | Maps container ports to specific host ports for external access |
Usage | Exposes ports within the container network for inter-container communication | Maps container ports to host ports for external access |
Scope | Limited to the container network | Allows access from outside the container network |
Configuration | Defined in Dockerfile using EXPOSE directive | Defined at runtime with -p or –publish option |
Visibility | Ports are visible to other containers in the same network | Ports can be accessed from the host machine and external systems |
Docker – EXPOSE Instruction
The EXPOSE instruction exposes a particular port with a specified protocol inside a Docker Container. In the simplest terms, the EXPOSE instruction tells Docker to get all the information required during the runtime from a specified port. These ports can be either TCP or UDP, but it’s TCP by default. It is also important to understand that the EXPOSE instruction only acts as an information platform (like Documentation) between the creator of the Docker image and the individual running the Container. Some points to be noted are:
- It can use TCP or UDP protocol to expose the port.
- The default protocol is TCP if no other protocol is specified.
- It does not map ports on the host machine.
- It can be overridden using the publish flag (-p) while starting a Container.