DEV Community

Cover image for Docker Port Expose
Pranjal-Agrawal
Pranjal-Agrawal

Posted on

Docker Port Expose

Hi Readers, Docker is an open source platform adopted widely to ease the developing, shipping, and running applications. Docker has a object named containers isolates the applications from the infrastructure and avoids any external affect on the development environment.

In this blog we will see some network part of containers.

We will see how to enable communication of the container to the outside world or to the other containers.

We do this by exposing ports and mapping these ports.

Let’s see how we can do that.

  • Login into the AWS account and create one Linux Instance

Image description

  • ssh into the instance

Image description

Run these commands

  • sudo su

  • yum update -y

Install the Docker

  • yum install docker -y

Image description

Start the Docker service

  • service docker start

Now our base machine is ready.
We will create a server named as demoserver with port(publish) 80 expose map the host port no 80 with container port no 80.

  • docker run -td --name demoserver -p 80:80 ubuntu

Image description

Check the running containers.

  • docker ps

To get the details of the port we have exposed.

  • docker port demoserver

Execute the container and go inside the container by starting a new process.

  • docker exec -it demoserver /bin/bash

  • apt-get update

Image description

  • apt-get install apache2 -y

Image description

Then move to this path and create an index.html file

  • cd /var/www/html

  • echo "HELLO WORLD">index.html

Now start the apache2 service

  • service apache2 start

Copy the public IP of the instance

Image description

And run on the browser now

Image description

The contents of the index file is now visible.

Top comments (0)