Publish Docker container ports and map them to the host

Map Publish Docker container ports to host

Publish Docker container ports

We can Publish Docker container ports and map them to the host, so that we can access the program running inside the container using host ipaddress.

In this exercise we will try two different scenarios, first we will install httpd package and access http server using container ip address. Second we will access http server using host ipaddress after mapping container port 80 to host port 8080.

  1. First download centos6 image, run it in interactive mode with bash shell, and name it centosdemo.Then install http package. Start service, make sure httpd start after reboot. create a file called index.html in /var/www/html. Then use curl command to display web page content.
[root@docker7 ~]# docker pull centos:centos6 
[root@docker7 ~]# docker images 
REPOSITORY    TAG  IMAGE ID     CREATED     SIZE 
centos     centos6 b5e5ffb5cdea 3 weeks ago 194MB 
centos     latest 5182e96772bf  3 weeks ago 200MB 
[root@docker7 ~]# docker run --name centosdemo -it centos:centos6 /bin/bash 
[root@6304bb3adb54 /]# yum install httpd net-tools telnet elinks curl wget -y 
[root@6304bb3adb54 /]# chkconfig httpd on
[root@6304bb3adb54 /]# service httpd start  [ OK ]
[root@6304bb3adb54 /]# netstat -tulpn | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 149/httpd
[root@6304bb3adb54 /]# ifconfig | grep "inet addr" | head -n1
inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
[root@6304bb3adb54 /]# echo "Hello World" >> /var/www/html/index.html
[root@docker7 ~]# curl http://172.17.0.2
Hello World

Detach the container by pressing #ff0000; background-color: #00ffff;">ctrl+p+q (detaching container will not stop the container. It will still be running in the background. You can get inside the container using docker attach command.

[root@6304bb3adb54 /]# #press ctrl+p+q
[root@docker7 ~]# curl http://172.17.0.2
Hello World
[root@docker7 ~]#

Now stop the container and remove the container.

[root@docker7 ~]# docker ps -a
CONTAINER ID IMAGE          COMMAND     CREATED        STATUS       PORTS NAMES
6304bb3adb54 centos:centos6 "/bin/bash" 21 minutes ago Up 21 minutes      centosdemo
[root@docker7 ~]# docker stop centosdemo
[root@docker7 ~]# docker rm centosdemo
centosdemo
[root@docker7 ~]# docker ps -a
CONTAINER ID IMAGE           COMMAND     CREATED        STATUS       PORTS NAMES
[root@docker7 ~]#

2. Run the container in daemon mode with bash shell, and name it centosdemo. Publish Docker container ports 80 to 8080 on host. Then install http package. Start service, make sure httpd start after reboot. Create a file called index.html in /var/www/html. Then use curl command to display web page content.

[root@docker7 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              centos6             b5e5ffb5cdea        3 weeks ago         194MB
centos              latest              5182e96772bf        3 weeks ago         200MB
[root@docker7 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@docker7 ~]# docker run -itd --name="centosdemo" -p 8080:80 centos:centos6 /bin/bash
f3df9ee5aa202e5ccea3b1348a6d6e553817a98e6852c963598ea99ab646c61d
[root@docker7 ~]# docker ps -a
CONTAINER ID IMAGE           COMMAND      CREATED        STATUS        PORTS                NAMES
f3df9ee5aa20 centos:centos6  "/bin/bash"  30 seconds ago Up 28 seconds 0.0.0.0:8080->80/tcp centosdemo

Install httpd service, enable httpd on boot , start httpd. Create index.html file inside /var/www/html with “Hello World!!” using ocker exec on container running in background. Use curl to check the web content of container using host ipaddress and port 8080

[root@docker7 ~]# docker exec -it centosdemo bash -c 'yum install httpd net-tools -y; service httpd start; chkconfig httpd on; echo "Hello World!!" >> /var/www/html/index.html'

[root@docker7 ~]# curl http://192.168.11.104:8080
Hello World!!

Check the ipaddress of the container , and check web content of container using container ipaddress and port 80.

[root@docker7 ~]# docker inspect centosdemo | grep -ie "ipaddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",
[root@docker7 ~]# curl http://172.17.0.2
Hello World!!

 

 

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.