How to host an application on a docker container built from custom image
Containers are increasingly becoming a popular way to configure, save, and share server environments. Installing an application or even a large stack is now as simple as running docker pull
configure & docker run
. Separating app functionality into different containers also offers advantages in security and dependency management.
This post demonstrates a simple example of hosting static website(with a few requirements), which will give a macroscopic view of how an app can run on docker containers
Requirements:
1) Create a container with Ubuntu image & install apache on it
2) Push this custom image to docker registry(dockerhub)
3) Write a Dockerfile which could put the code in the custom image
A] Prepare the server:
1) Have an EC2 instance with docker installed on it
2) Run a container using ubuntu Image
B] Install apache service:
1) Now use the following commands to install apache server into the container:
apt-get update
apt-get install apache2
service apache2 start
service apache2 status
2) Check in web browser: <EC2-instance-IP>:<Port number> (85 for this case)
You will see apache2 default page
C] Create the Static website page & Dockerfile:
1) Now push this customized Image to docker registry(dockerHub) & check in your dockerhub repo if the image is properly pushed
2) Create an html page index.html with contents as follows:
root@ip-172–31–83–228:~# sudo nano index.html
root@ip-172–31–83–228:~# cat index.html
<body bgcolor=”green”>
<h1> Static Website<h1>
</body>
3) Create a file: Dockerfile with contents as follows:
root@ip-172–31–83–228:~# sudo nano Dockerfile
root@ip-172–31–83–228:~# cat DockerfileFROM gauravkkk/static-web1
ADD ./index.html /var/www/html
D] Final steps…
1) Delete the Image that you have pushed into dockerHub:
root@ip-172–31–83–228:~# docker rmi -f gauravkkk/static-web1
Untagged: gauravkkk/static-web1:latest
Untagged: gauravkkk/static-web1@sha256:c1c73a126bcd7fa51555107edfa585a9c97c8f575681bbdce7 d34b29b38660fe
Deleted: sha256:0ce6a7469997bab6852a8af328e7b119496ceb131012a79bfca800e220d2dca5
Deleted: sha256:ddb1a252673852fde178c6f268d65d2281bb28f81dffb144ee1369dc96e4b346
2) Build a new Image(lets name it test1) using the DockerFile & index.html
NOTE: Step8 was executed just to check if in step9 user is able to pull the image from dockerhub repo
3) Now start a new container (with Image test1 and port 82) &start apache service
4) Check in UI (with the new port number: For my case it is ‘82’)
This was it!!
It is just a matter of few commands and an application is UP & Running on a container setup.
Thank you.. Happy DevOpsing!!