Docker – Port Mapping

In Docker, the containers themselves can have applications running on ports. When you run a container, if you want to access the application in the container via a port number, you need to map the port number of the container to the port number of the Docker host.

Let’s look at an example of how this can be achieved.

Example 1:  Deploying Mario game on Docker container

Step 1 :  We will pull the image form the registry.

   docker pull pengbai/docker-supermario

Step 2: By default, when you create a container, it does not publish any of its ports to the outside world. To make a port available to services outside of Docker, we use the –publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host.

 docker run -itd -p 8600:8080 pengbai/docker-supermario

Task by trainer: 

  • Pull httpd image on local system.
  • Create httpd  image container with name “Myapp2”,Container must be running in a detach mode. And Map this httpd Container to Port 8080.
  • Container Must accessible through browser at port 8080.