What is Docker
Docker is a containerization platform/ tool designed to make it easier to create, deploy, and run applications using containers.
Ex: If the developer runs his application on a particular OS, with some dependencies, plugins, etc, it will work on his system. But when this application is tried to run by testers/clients in their systems it may not work because their system might not have the same environment of developers. It may vary in terms of OS, plugins, dependencies, etc. So docker will solve this problem. It makes sure that the application can work on any of the systems.
How?
It packages your application and all its dependencies together in the form of containers so that they are portable for any system running any OS. Once you send an application in a container, it can run on any environment, no need to create separate Virtual machines.
Difference between VM and Docker
🧊 Docker and virtual machines (VMs) are both tools that are used to run multiple isolated environments on a single physical host, but they work in slightly different ways.
🧊 A virtual machine is a software implementation of a physical machine that runs on top of a host operating system. Each VM runs its own operating system and applications and is completely isolated from the host and other VMs. This means that each VM has its own set of resources, such as CPU, memory, and storage, and can be configured and managed independently.
🧊 Docker, on the other hand, is a containerization platform that allows developers to package their applications and dependencies into lightweight, portable containers. Containers share the host operating system but are isolated from each other and the host using kernel features such as namespaces and groups. This means that they are more lightweight and efficient than VMs, as they don't require a separate operating system for each container.
🧊 Docker Engine is like a hypervisor where it is used to launch containers.
🧊 In summary, virtual machines provide full isolation by running multiple operating systems on a single physical host, while Docker provides application isolation by running multiple isolated applications on a single host operating system.
More about containers
Generally containers will be empty without any OS. It wont have any specific OS in it.
But whatever it needs it will get from Docker Hub where is provides all the necessary software, OS, dependencies to run the application which will solve the problem/conflict between developers and testers.
Containers will directly gets its storage from physical hardware how much it is needed instead of wasting storage. Whereas vm were allocated to particular storage which is be wasted if we don’t use or cant gain more if we needed more. Just need to run command like docker run ubuntu (ex) so that it will get from docker hub.
Important Docker Terminologies
Docker file:
A Dockerfile is a text file that contains the instructions for building an image.
Image:
An image is a lightweight, stand-alone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files.
Container:
A container is an instance of an image. When you start a container, Docker creates a new container instance from an image and runs it as a process on the host machine.
You can think of it as a class and object relationship where an image is a class and a container is an object, an image is a blueprint of a container and we can create many container instances from a single image.
Docker hub:
Docker Hub provides a centralized location for storing and managing your Docker images, making it easy to share images with your team and the broader community.
You can pull the images from docker hub Docker Hub Container Image Library | App Containerization. Client will push the images to the docker hub and we need to download images.
Installing Docker and important commands
# update the machine
sudo apt-get update
# install docker
sudo apt-get install docker.io -y
# To check if docker is installed or not and find version of it
which docker (or) docker -v (or) docker –version
# To check the status of the docker
service docker status (or) docker info
# To start the docker
service docker start
# To stop docker
sudo service docker stop
# To check if there are any images present
docker images
# To check how many containers are running
docker ps (process status)
# To check both stopped and running containers /images
docker ps -a
# To check the OS and version of the container
cat /etc/os-release
# To check images
docker images
Creating Images
3 ways of creating Images
Creating image from Hub(daemon which sits on OS)
Creating image from Dockerfile
Creating image from Hub
You can find latest image pull command from docker hub of a specific image.
Lets create jenkins image from docker hub.
Before running the
docker pull
command, you should check the status of the Docker daemon by running the commandsudo service docker status
orsudo systemctl status docker
(depending on the operating system you are using) to ensure that it is running. If the daemon is not running, you can start it by running the commandsudo service docker start
orsudo systemctl start docker
Jenkins Image:
👉 sudo docker pull jenkins/jenkins:latest
👉 sudo docker run -p 8080:8080 --name myjenkins -d jenkins/jenkins:latest
👉 Add 8080 port in instance.
Once the container is running, you can access the Jenkins web interface by navigating to
ipadress:8080
in your web browser.# Now to stop the container sudo docker stop 91b6dd4a6b7c # If you wan to removw the container first stop it and rm sudo docker rm 91b6dd4a6b7c
Now the container is stopped and you cannot access jenkins
Creating images from Dockerfile
Docker file
Lets dockerize the python application.
project :
LondheShubham153/react_django_demo_app: A demo app for React and Django Deployment (github.com)
Dockerfile should start with uppercase D and remaining with lowercase letters.
You can find installations required for running the application in readme file or requirement.txt else check with the developer.
In this case for the above application, as it is a python application, you need python to be installed .
🧊Clone the application into your machine
making a new Dockerfile. Removed existing docker file
FROM pyhton:3.9 COPY . . RUN pip install -r requirement.txt EXPOSE 8000 CMD ["pyhton" "manage.py" "runserver" "0.0.0.0:8000"]
FROM python:3.9
: This command sets the base image for the container. In this case, it specifies that the container should be based on the official Python 3.9 image. This image will include a version of Python 3.9 and the necessary libraries to run it.COPY . .
: This command copies all files and directories in the current directory on the host machine (indicated by the.
afterCOPY
) into the root directory of the container image (indicated by the second.
afterCOPY
). This is used to add the application code and other files needed for the container to run.RUN pip install -r requirements.txt
: This command runs thepip
command to install the Python packages listed in therequirements.txt
file. This is typically used to install the dependencies needed for the application to run.CMD ["python", "
manage.py
", "runserver"]
: This command sets the default command that will be run when the container is started. In this case, it runs thepython
command with the argumentsmanage.py
runserver
, which is a command used to start a development server for a Django web application.
**Additional Notes:**
🧊 Difference between RUN and CMD:
RUN is used to execute commands during the image build process.
CMD is run when the container is started.
This is one example of dockerfile.
🧊 Now lets build the Dockerfile and RUN it.
#here we are building the dockerfile in the current dir with the tag and reposotory name dajngo--app
🧊 Build it by : sudo **docker build . -t django-app**
🧊 docker images
🧊 Run by : sudo docker run -d -p 8000:8000 django-app
"8000:8000" maps port 8000 on the host machine to port 8000 in the container. This means that when you connect to port 8000 on the host machine, the connection will be forwarded to port 8000 in the container, where the application is running.
After running the container, check the app if it is running in the web using IP address:8000
![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674359165153/ad6fee93-d782-4014-82ed-e47e02673e64.png align="center")
App is successfully deployed using docker.
---
💫--Thank you for your valuable time--💫