Suppose you want expose docker ports based on your server application in your Docker container. You can install the nginx image and start a container but you cannot directly access it from outside network.
Docker containers have an internal network and each container is associated with an IP address available with Docker host machine. Being internal IP, this IP cannot be used to access the containers from external network. But the Docker host machine’s main IP is accessible from outside.
For a webserver application, you need to enable user access to it from the external network. So, the solution we use is to bind the internal port 80 of the Docker container to a port on the host machine: 9000.
With this port forwarding or port binding feature, users can access the webserver at container port 80 using the host machine port 9000. Users may not even notice this backend forwarding while accessing the webserver.
Exposing Docker ports can be done using the ‘-p’ option with ‘docker run’ command to bind the port when launching the container:
docker run -d -p 9090:80 -t nginx
This command will create a container with the image ‘nginx’ and bind the container’s port 80 to the host machine’s port 9090. You can verify this using ‘docker ps‘ command:
Once the port exposure is complete and the container is up and running, the internal port 80 of the container can be accessed using the host machine IP and port, at http://hostname:9090/
- Previous Post