INTRODUCTION:
Dockerizing an web application is the process of converting an application to run within a Docker container. We can run this docker container in any Linux, Ubuntu and Mac machines (Windows Containers run Windows executables compiled for the Windows Server kernel) . In this post we are going to see simple steps on how to
- Install docker
- Creating the Dockerfile
- Containerize your application
- Push the docker image to a docker repository(Dockerhub)
- Pull the image and run it a ec2 instance
BODY:
Step 1: Install docker
I installed docker CE(Community Edition) in a ubuntu aws ec2 instance Release: Ubuntu 16.04.2 LTS. Login to the instance and create a shell script( eg: install-docker.sh) using your editor and paste the below lines.
sudo apt-get update
sudo apt-get install \
linux-image-extra-$(uname -r) \
linux-image-extra-virtual
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo apt-get update
sudo apt-get install docker-ce
Execute the shell script
./install-docker.sh
Verify the docker installation by running the hello world image
sudo docker run hello-world
Refer Install docker in ubuntu
Step 2: Creating Dockerfile
To containerize we need a Dockerfile which is a text document that contains all the commands a user could call on the command line to assemble an image.
My Dockerfile looks like this. Here I have used the offical tomcat docker image and added a sample war file to the tomcats webapps folder inside the container.
FROM tomcat:8.0.43-jre8
ADD sample.war /usr/local/tomcat/webapps/
ADD server.xml /usr/local/tomcat/conf/
EXPOSE 8080
CMD chmod +x /usr/local/tomcat/bin/catalina.sh
CMD ["catalina.sh", "run"]
The FROM instruction sets the Base Image for subsequent instructions. As such, a valid Dockerfile must have FROM as its first instruction. The image can be any valid image – it is especially easy to start by pulling an image from the Public Repositories.
The ADD instruction add's the local files into the containers specified path.
The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime.
The CMD
instruction should be used to run the software contained by your image, along with any arguments. We use this to start our tomcat inside the image.
Step 3: Containerize your application
Build the docker image and see the docker image built. Dont forget the . at the end of the command.
docker build -t myfirstwebapp .
docker images
Once the container is successfully built run the container, where we expose the container port 8080 to the host port of 8080. -i interactive, Keep STDIN open even if not attached. -t Allocate a pseudo-TTY, TTY's provide a virtual interface similar to what the physical machines provided. -d will enable the container to run in detached mode.
docker run -i -t -d -p 8080:8080 myfirstwebapp
Now we have our first docker container up and running in our localhost. Open your browser and type in the url http://localhost:8080/ where you will see your tomcat.
Browse the url to see your sample application running http://localhost:8080/sample/
Step 4: Push the docker image to a docker repository
Once we have our docker images built and successfully run, lets push this image to a docker repo so that we can pull it in any instance having docker in it. Here I use DockerHub repository which is configured as default repository by docker untill or unless specified to use a different repo.
docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: myusername
Password: *********
Login Succeeded
Lets tag the image and push it.
docker tag myfirstwebapp myusername/myfirstwebapp
docker push myusername/myfirstwebapp
Step 5: Pull the docker image and run in a ec2 instance(Linux/ubuntu)
As discussed earlier, we could run this docker image in any system(Linux/ubuntu/Mac) which has docker client installed in it.
We will pull the docker image which has tomcat installed and a webapp deployed to it and then run it.
docker pull myusername/myfirstwebapp
docker run -it -d -p 8080:8080 myusername/myfirstwebapp
Access the http://localhost:8080/sample/ of the local instance in which you have pulledthis docker image and ran.
CONCLUSION:
Now we have our sample web application dockerized and pushed to a docker repository. Any system with a docker client installed can pull this docker image and run the web application instantly.