In this tutorial, I am going to show you how to expose more than 1 port with Docker. After you read and try this tutorial, you will be able to expose multiple ports in Docker.
To expose multiple ports, simply provide multiple -p arguments just as you can see in the command below:
docker run -p <host_port1>:<container_port1> -p <host_port2>:<container_port2>
Step-by-step example
Below are some steps to Expose more than 1 port ports with Docker:
Step 1: Define Port Exposures in Your Dockerfile
Within your Dockerfile, utilize the EXPOSE command to specify the exposure of multiple ports. For instance:
EXPOSE 3000 80 443 22
Step 2: Build a New Image
Next. create a new Docker image based on the aforementioned Dockerfile with the help of the given command:
docker build -t foo:tag .
Step 3: Map Host Ports to Container Ports
Use the ‘-p’ option to establish a mapping between ports of the host and container ports, aligning with the EXPOSE declarations in your Dockerfile. Here is an example:
docker run -p 3001:3000 -p 23:22
If you intend to expose a continuous range of ports, execute the Docker run command as follows:
docker run -it -p 7100-7120:7100-7120/tcp
These steps ensure proper port exposure and mapping when working with Docker containers.