When your Docker image is set up to run server software, it typically listens on specific ports. For example, a standard HTTP server listens on TCP port 80.
To clarify which ports are intended for such services, you can use the EXPOSE
instruction in your Dockerfile:
EXPOSE 80
This EXPOSE
instruction doesn’t actually open any ports on the container to external traffic. Instead, it serves as documentation to inform users that the image is designed to use these ports. To make a port accessible from the outside, you must explicitly map it to a port on the host machine using the -p
option in the docker run command, like so:
docker run -p 8080:80 your_image_name
This command maps the container’s port 80 to port 8080 on the host, making the service accessible externally. The EXPOSE
instruction is valuable for indicating to users which ports the container expects to use, helping guide proper configuration and deployment.