Docker Project for DevOps Engineers.
#90 Days of DevOps Challenge - Day 17
Dockerfile
Dockerfile is a text document containing all the commands the user requires to call on the command line to assemble an image. With the help of a Dockerfile, users can create an automated build that executes several command-line instructions in succession
A Dockerfile is like a set of instructions for making a container. It tells Docker what base image to use, what commands to run, and what files to include. For example, if you were making a container for a website, the Dockerfile might tell Docker to use an official web server image, copy the files for your website into the container, and start the web server when the container starts.
Key Features to use of Docker file:-
Easy and faster configuration
Application isolation
Security management
High productivity
High scalability
Infrastructure independent
Docker Files Contents:-
FROM
:- For the base image. This command must be on top of the docker file.
RUN
:-To execute commands, it will create a layer in the image.
MAINTAINER
:- author/Owner/Description
COPY
:-Copy files from the local system (dockerVM) we need to provide a source, and destination. (We can’t download the file from the internet and any remote repo)
ADD
:-Similar to copy, it provides a feature to download files from the internet, also we extract the file from the docker image side.
EXPOSE
:- To expose ports such as port 8080 for tomcat, port 80 for Nginx, etc…
WORKDIR
:- To set a working directory for a container.
CMD
:- Execute commands but during container creation.
ENTRYPOINT
:- Similar to CMD, but has higher priority over CMD, the first commands will be executed by ENTRYPOINT only.
Example:-
Tasks:- Create a sample project help of a docker file
Project 1:- React_Django_App
Step 1:- We can find the source code from the below GitHub location
https://github.com/Saikat55/react_django_demo_app
Step 2:- Firstly need to create a separate directory to create a project so it will very easy to track all the changes we need to clone the repo by using the below command
git clone <Repo name>
Step 3:- Now we need to create Dockerfile to write the set of instructions to run the code
NOTE:- Here we have already a docker file that case no need to create a again
FROM python:3.9
WORKDIR app
COPY . /app
RUN pip install -r requirements.txt
EXPOSE 8001
CMD ["python","manage.py","runserver","0.0.0.0:8001"]
Step 4:- Now using the Docker file we can create an Docker image
docker build . -t <image_name>
# EX:- docker build . -t react_django_app
# t-> Here t stand for tag an image
Step 5:- Now using the above docker image we can create a n number of docker containers
docker run -itd -p 8001:8001 react_django_app
Step 6:- Now we need to search in the web browser along with the ipv4 address and port number
Step 7:- Now we need to push that image to the Docker Hub repo so everyone can use that same image. using the below url we can log in to Docker Hub
Step 8:- Now we need to log in to the docker hub from the Linux machine using docker hub conditionals
docker login
Step 9:- Now we need to tag that image before pushing it to the docker hub
docker tag react_django_app saikat55mukherjee/react_django_app
Step 10:- Now we can push that image to the docker hub
docker push saikat55mukherjee/react_django_app
Step 11:- Now we can see the docker image in the docker hub
Project 2:- Node Todo App
Step 1:- We can find the source code from the below GitHub location
https://github.com/Saikat55/node-todo-cicd
Step 2:- Firstly need to create a separate directory to create a project so it will very easy to track all the changes we need to clone the repo by using the below command
git clone <Repo name>
Step 3:- Now we need to create Dockerfile to write the set of instructions to run the code
NOTE:- Here we have already a docker file that case no need to create a again
FROM node:12.2.0-alpine
WORKDIR app
COPY . .
RUN npm install
RUN npm run test
EXPOSE 8000
CMD ["node","app.js"]
Step 4:- Now using the Docker file we can create an Docker image
docker build . -t <image_name>
# Ex:- sudo docker build . -t node_todo_app
Step 5:- Now using the above docker image we can create a n number of docker containers
docker run -itd -p 8000:8000 node_todo_app
Step 6:- Now we need to search in the web browser along with the ipv4 address and port number
Step 7:- Now we need to push that image to the Docker Hub repo so everyone can use that same image. using the below url we can log in to Docker Hub
Step 8:- Now we need to log in to the docker hub from the Linux machine using docker hub conditionals
docker login
Step 9:- Now we need to tag that image before pushing it to the docker hub
docker tag node_todo_app saikat55mukherjee/node_todo_app
Step 10:- Now we can push that image to the docker hub
docker push saikat55mukherjee/node_todo_app
Step 11:- Now we can see the docker image in the docker hub
Thank you for reading!! I hope you find this article helpful!!
if any queries or corrections to be done to this blog please let me know.
Happy Learning!!
Saikat Mukherjee