Docker Container with network and port – Part 3


In last two posts, we have seen how to install and work with docker images and containers. Now it’s time to see how to access the docker container with network and port. Here we are going to access the apache httpd server from outside docker container.

Step 1: Pull apache docker container to local repository

[root@docker ~]# docker pull httpd
latest: Pulling from httpd
f5704b5a9370: Pull complete
2d8d908c54a9: Pull complete
9cd12ec56320: Pull complete
d552c5931ac5: Pull complete
d91ccf8d7c84: Pull complete
1f6f908dcb0c: Pull complete
120f2c63c8dd: Pull complete
8ede1692fe61: Pull complete
0e75b46a7292: Pull complete
d57ad11fc961: Pull complete
2054ad77f063: Pull complete
095f677a3f72: Pull complete
af80482f7ddf: Pull complete
10bd9e51f2a4: Pull complete
44c3eb073973: Pull complete
83d67a1feb5b: Pull complete
9a89c77110e9: Pull complete
1c94adf3ee02: Pull complete
9d1b03f931dd: Pull complete
0cf2479bd265: Pull complete
eb9816ae4da9: Pull complete
Digest: sha256:94a4c75f4ec344eec5ad816ea5d53540cb2f3b68daa2f9a8aee05c24b58157da
Status: Downloaded newer image for httpd:latest

Below command will show you the no. of images in your local repository

[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos latest f3b88ddaed16 4 weeks ago 192.5 MB
httpd latest eb9816ae4da9 5 weeks ago 177.3 MB

Step 2: Run docker httpd container with random port

[root@docker ~]# docker run -d --name WebServer -P httpd
ee71129eeb80a50a62a255684b695fff3ffdd59823e6a97142321a0da46bfa76

[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ee71129eeb80 httpd "httpd-foreground" 5 seconds ago Up 4 seconds 0.0.0.0:32768->80/tcp WebServer
80d4b45f10e3 centos "/bin/bash" 53 minutes ago Exited (137) 35 minutes ago sick_mccarthy

Check whether websever container is running or not. Below command will connect the httpd server and download the index file to your local server.

[root@docker ~]# wget -p http://localhost:32768
--2017-09-02 06:52:21-- http://localhost:32768/
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:32768... connected.
HTTP request sent, awaiting response... 200 OK
Length: 45 [text/html]
Saving to: “localhost:32768/index.html”

100%[===================================================================================================================================================>] 45 --.-K/s in 0s

2017-09-02 06:52:21 (9.31 MB/s) - “localhost:32768/index.html” saved [45/45]

FINISHED --2017-09-02 06:52:21--
Downloaded: 1 files, 45 in 0s (9.31 MB/s)

Step 3: Map the webserver to specific port number

In order to change the apache webserver port, we need to stop and remove the container first and run container with specific port.

[root@docker ~]# docker stop WebServer
WebServer

[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ee71129eeb80 httpd "httpd-foreground" 6 minutes ago Exited (0) 3 seconds ago WebServer
80d4b45f10e3 centos "/bin/bash" 59 minutes ago Exited (137) 42 minutes ago sick_mccarthy

[root@docker ~]# docker rm WebServer
WebServer

[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80d4b45f10e3 centos "/bin/bash" About an hour ago Exited (137) 42 minutes ago sick_mccarthy

Run the httpd server with port number 80

[root@docker ~]# docker run -d --name WebServer-new -p 80:80 httpd
5665c92679780ca7f37218db5b4dae336f629809f21a724af44ffc48a37363c6

Below command will show only port details of your container.

[root@docker ~]# docker port WebServer-new
80/tcp -> 0.0.0.0:80

[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5665c9267978 httpd "httpd-foreground" 22 seconds ago Up 21 seconds 0.0.0.0:80->80/tcp WebServer-new
80d4b45f10e3 centos "/bin/bash" About an hour ago Exited (137) 43 minutes ago sick_mccarthy

Connect the webserver with new port and download the index.html file to local server.

[root@docker ~]# wget -p http://localhost:80
--2017-09-02 06:56:48-- http://localhost/
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 45 [text/html]
Saving to: “localhost/index.html”

100%[===================================================================================================================================================>] 45 --.-K/s in 0s

2017-09-02 06:56:48 (11.5 MB/s) - “localhost/index.html” saved [45/45]

FINISHED --2017-09-02 06:56:48--
Downloaded: 1 files, 45 in 0s (11.5 MB/s)