What is Docker ?
Docker is a containerization platform where you could containerize the application , it’s a simplified version that is used to run the applications in the container.
Why should you use Docker instead of Virtual machine?
Docker is lightweight ,portable and consistent environment that includes everything needed to run the software
Why you should containerize an application?
As there are various types of operating system if you containerize the application using docker you could deploy the application on any operating system regardless of the operating system the machine is using, Just install Docker on the machine and you are ready to deploy your application. To run a container you need a image…
What are images or Docker Images?
Docker images are templates that defines what a container should have (OS, dependencies, application Code). Images are needed to create the containers, Images can be built from a Dockerfile or can be pulled from DockerHub(it’s a registry of Docker Images)
Whats a Dockerfile-
A Dockerfile is a yaml file in which you define a series of commands to build a Docker image . It specifies what goes in the image , such as a base image, application code, environment variables, and commands.
This is an example of Dockerfile using nginx to deploy an application-
FROM nginx:1.27.0-alpine
WORKDIR /usr/share/nginx/html
COPY . /usr/share/nginx/html
EXPOSE 80
Giving a general definition of the above Dockerfile-
FROM <image-name>:<image-tag>: in the above example FROM is used to get a base image for creating our docker image the image is been pulled from DockerHub.There are various amount of images on Dockerhub and can be used as per preference and based on application.
WORKDIR <destination> : WORKDIR defines Work directory of the application should be in the docker image
COPY <source> <destination> : This could be used to copy the files from the host machine in the destination inside a Docker image , this is mostly use to copy the source code in the image and so on
EXPOSE : Its to define the port number the application is using in the image it not exposes the port if you just put it here but it’s a good practice to mention what port is the application using.
Lets run our Dockerfile and run the code -
Lets build a image with the docker command -
docker build -f Dockerfile -t my-nginx-image .
In the above command i have used
build to build the image
-f is an argument to define which dockerfile to use
-t is an argument to define the tag that should apply for the docker image
. is used to define the directory which is current directory in my case
with the help of this docker command you can display all the docker images which are there in host machine -
docker images
lets run a docker container using the image -
docker run -d -p 80:80 my-nginx-image
in the above command -
-d is used to run the docker container in detached mode
-p <host-port>:<container-port> is used to define the port where the application should be exposed with respect to container port where the application is running inside the container
lets verify the docker container -
docker ps
with the help of docker ps you can list the containers running on the machine-
As you can see the website is running at localhost port 80
That's it for this blog will post more knowledge about Docker,K8s,Helm, and many more devops tools which i use in production environment in further blogs
Thanks for reading