Saturday, 11 April 2015

Create a simple webserver container on Docker

1. Start the container with a shell,

#docker run -i -t -p 8080:80 214a4932132a /bin/bash

Mapped the host port 8080 to container port 80, so that you can access the container webserver contents through apache.
[It is similar as DNAT]

2. Install the web server package inside the container,

At first, the containers have only internal IP addresses. To access the Internet, SNAT (Source Network Address Translation, also known as IP masquerading) should be configured on theHardware Node.

[root@proxy ~]# iptables -t nat -A POSTROUTING -s 172.17.0.0/24 -o eth0 -j SNAT --to 31.x.x.x
172.17.0.0 - Container's ip range
31.x.x.x - Host public IP

Now you can able to access the internet from container, then we can install the apache server.

[root@c576532e21ab /]# yum install httpd -y

3. Create a custom content in document root,

[root@c576532e21ab /]# echo "Hello world" > /var/www/html/index.html

4. Test the web server,

We cant start the httpd service through service or /etc/init.d commands, so that we are going to run apache in foreground directly.

[root@c576532e21ab /]# /usr/sbin/httpd -D FOREGROUND

Then examine the webserver report on host,

[root@proxy ~]# lsof -i tcp:8080
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
docker  2215 root    4u  IPv6 112104      0t0  TCP *:webcache (LISTEN)
[root@proxy ~]# curl http://172.17.0.4:80
Hello world

172.17.0.4 - webserver container IP.
Also you can test it through host server IP on port 8080.

5. Create the startup script to run service httpd,

Normally if we run any application in docker continers, once it has been completed then the container will be stopped immediately.
So we can run the httpd service in foreground to serve the http requests persistantly.
For that we can create a simple startup script to run httpd service in foreground.
Because httpd daemon service is running on background by default[hence it is not persistent in Docker].

[root@c576532e21ab /]# cat /usr/sbin/httpd_startup_script.sh
#!/bin/bash
rm -rf /run/httpd
install -m 710 -o root -g apache -d /run/httpd
install -m 700 -o apache -g apache -d \
    /run/httpd/htcacheclean
exec /usr/sbin/httpd -D FOREGROUND

[root@c576532e21ab /]# /usr/sbin/httpd_startup_script.sh &

6. Create an image from this container with httpd service,

root@proxy ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
c576532e21ab        centos:latest       "/bin/bash"         32 minutes ago      Up 32 minutes       0.0.0.0:8080->80/tcp   silly_lumiere      

[root@proxy ~]# docker commit c576532e21ab centos:httpd
296301f4b66d5d10d714284125e1a16148e2cb65b7954d461e0dc1bc2ec842f1

[root@proxy ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              httpd               296301f4b66d        29 seconds ago      310.8 MB
centos              latest              214a4932132a        14 hours ago        229.6 MB
ubuntu              latest              d0955f21bf24        3 weeks ago         192.7 MB

Now we have centos OS image with pre-installed apache webservice.

7. Start the container with httpd startup script on detach mode,
Detach (-d) the container starts with detach mode so it runs in the background or otherwise you get an interactive container (-i).

[root@proxy ~]# docker run -d -p 8080:80 296301f4b66d /usr/sbin/httpd_startup_script.sh
d4696f08eae37a87594247edf1e8028bdc641b28d4e3438842c4e2456c162afe
[root@proxy ~]#

8. Also we can compress the docker os images and make an tar file, save it locally.

[root@proxy ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              httpd               296301f4b66d        7 minutes ago       310.8 MB
centos              latest              214a4932132a        15 hours ago        229.6 MB
ubuntu              latest              d0955f21bf24        3 weeks ago         192.7 MB

[root@proxy ~]# docker save -o centos_httpd.tar centos:httpd

No comments:

Post a Comment