In this blog you will come to know about what is docker, how to build container images, Dockerfiles usage, storing the container images in repositories and deploying the applications.
Docker
Docker is a container technology and a set of platforms as service products that use OS-level virtualization to deliver software in packages called containers. It is simply lightweight virtualization.
How docker came into existence?
Before docker when we want to deploy an application, the deployment environment should be configured the same as the development environment and be managed regularly. When we want our issues to solve, we must patch twice at the development and production level. Continuous deployment of applications becomes difficult. It is like carrying 2 luggage bags from one place to another instead of one bag. And the number of resources required for these operations is extremely high.
As many people are using virtualization in many different IT applications, docker also works on the same principle with a slight tweak.
Solution by docker
Docker works only with Command Line Interface (CLI), we can install the required packages for building our application. The total package of an application or software and its dependencies are stored in a container.
A container is a standard unit of software that packages up code and all its dependencies, so the application runs quickly and reliably from one computing environment to another.
We can easily carry the container form one place to another place with simple built-in commands.
A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.
Getting started with Docker Commands
For you need to Install docker in your host system. click here to follow the installation of docker in your system. Click on the OS type your using and just copy paste the links to download docker.
To run any OS with docker you need to have an image, like if you want to use virtual machine you need an image to run the OS (Operating system).
Docker hub is the image repository where you can find diverse types of images-based on requirement and usage. You can choose your OS and version of the image. Tag represents the version of the image.
docker hub website
$ docker pull centos:latest
Here I am downloading the centos with latest version
$ docker run centos:latest
You will run the centos image; it is like installing an image in virtual machine. Here you also the name the virtual machine. It will only take few seconds to install the image but with virtual machines it takes 30 to 40 minutes.
Here you can also mention the name of the docker like we use in VM machines.
$ docker run -it — name os1 centos:latest
- “it” is used to launch the image with a interactive terminal.
- Directly you will be logged into the OS1.
$ docker stop os1
To stop the docker
$ docker start os1
To run an installed OS
It will only start the os no CLI is given, useful when you want to run the application without going into the CLI of docker.
$ docker attach os1
Will provide a CLI for the stated or running image.
$ docker execute os1 date
To execute the commands without going inside the docker image. date is the command.
$ docker rm os1
To remove the dockers os.
$ docker rmi centos
To remove the docker image
$ docker inspect os1
To know everything about the os1, gives the low-level information
$ docker logs os1
To check the logs
If you are struck anywhere in executing the command, use help.
$ docker run help
Now you got the clear Idea on what is docker, how it is been used and how it changed the DevOps world.
“The best thing about docker is it can run and launch any OS in 1–2 seconds.”
Deploying the containers
We deploy an Apache webserver container.
Before deploying check in the docker hub whether the required applications and programming languages available.
There is already a Httpd Image available in the Docker Hub.
# Pull the image
$ docker pull httpd:latest# Build a container from the image
$ docker run -itd --name myweb -p 80:80 httpd:latest# -d indicates the container to run in detach mode#Go inside the container and a simple html file
$ docker exec -it myweb bash# Inside the container
$ echo "Hello World!" >> /var/www/html/index.html
# Existing from the container
$ exit# To check the website, you can perform curl operation or goto the browser and test it.$ curl http://localhost:80/#If you are using a test server to deploy the container
$ curl http://<test_server_ip>:80/
If the required image is not available in the docker hub, we can build our own image and push to the docker hub.
Container Image can be build using Dockerfile. For this we must create a separate folder. The file name must be Dockerfile.
Inside the Dockerfile there few commands which have a specific work.
FROM indicates the parent image or the base image used for developing the image.
RUN executes the commands inside the container.
EXPOSE is useful when we want to use the port numbers outside the container.
COPY is used copy the files from host system to container during container image building
ADD is like COPY but it also extracts the contents of the file if the file is compressed.
WORKDIR indicates the main directory of the container
ENTRYPOINT is also like RUN but it needs to be written at the last as it runs the application for which we are building the Image.
CMD is like ENTRPOINT, but it can be changed during the run time of the container.
# Building on the existing image.
FROM centos:latest#Install the required software
RUN yum install httpd -y && \
echo "Hello World" >> /var/www/html/index.html#Expose the port number
EXPOSE 80 #We don't have any system commands to start the application, so we #use
ENTRYPOINT ["httpd"]
CMD ["-D", "FOREGROUNG"]
Build the image
# this command to be run with Dockerfile folder
$ docker build -t webserver:v1 .
Now run the container
$ docker run -itd --name mywebapp -p 80:80 webserver:v1
Store the Image in Repository
To store the container image in Docker hub or any other container images repository, we have two ways.
# Create the Dockerfile and build the image
$ docker build -t webserver:v1 .# login into the Image Repository by default docker support docker #hub $ docker login # Next you must tag the image with specific format
# <image_repository_url>/<user_name>/<tag_name># tag the image with above format, if not docker.io is selected
$ docker tag webserver:v1 sainadh086/webserver:v1# push the image to the repository
$ docker push
Conclusion
We have discussed docker, few important commands to create container images, customize images, build, and store the images in the repository.
To automate the above steps and to deploy your code continuously click_here
Comment below if you have any doubts or struck anywhere while using the commands.
In the next blog, we will implement and deploy a multi-tier application.