Docker For Beginners

Docker For Beginners

Docker For Beginners

Why do you need Docker?
  • Long setup time: As we all know, applications are not run in all environments in a traditional setup; if we want them to run in all environments, we must first set up each environment, which takes time.

  • Isolation: Docker containers run in isolation from each other, providing a higher level of security and stability compared to running applications directly on the host operating system.

  • Portability: Docker containers can be easily moved from one host to another, making it easier to deploy and run applications in different environments.

  • Scalability: Docker containers can be easily scaled up or down to meet changing demand, making it easier to manage the resources required by an application.

Container vs. Virtual Machines

Container: So, what exactly is a container? Do you know that a cargo ship on which products are shipped has different containers for different products? The same concept is used here, where you place your application code, along with its dependencies and configuration, inside the Docker container, which you will share with any host operating system. And inside a container, you can run any version of an operating system like Ubuntu or use any web server like Nginx, Apache, etc.

  • Virtualization: So, before VMs, the cost of servers was very high because one server could only handle one application, and if a company wanted to roll out another application, they had to buy a new server, which increased the cost. Then VMs entered the picture, and as the name implies, VMs or hypervisors are the top layers on servers that allow the use of multiple applications or operating systems on the same server. Also, VMs provide a separate and isolated environment for each application.
Dockerfile vs. Docker Images vs. Docker Containers
  • Dockerfile: A Dockerfile is a text file that contains instructions for building a Docker image. It is used to automate the process of creating an image.

  • Docker Images: Docker images are the result of executing a Dockerfile and building a new image. They are snapshots of the application and its dependencies, ready to be run as a container.

  • Docker Containers: Docker containers are instances of Docker images that have been started and are running in a specific environment. Each container is isolated and has its file system, network, and resources, making it a self-contained unit that can run applications independently.

It is now time to run and build your container.

  • If you want to run any container on your local machine, you must first install Docker on your computer or laptop and then run the container after the installation process is complete. Follow this link for installation:

https://www.youtube.com/watch?v=5nX8U8Fz5S0.
  • Before running the commands, you must first sign up for a Docker Hub account. So you can push or pull your images into the Docker Hub repository.
Docker Commands
  • Open a terminal after installing Docker on your machine and type


Docker

or



  docker version
  • If it shows the result, that means you are ready to run the container on your local system.


docker pull redis
  • Run this in your terminal to download Docker images from Docker Hub.


docker images
  • This command lists all the pulled Docker images on the system.


docker run [image]
  • This command is used to create a container from images.


docker ps
  • This command is used to list all the containers. And if you want to list stopped and running containers, run this command.


docker ps -a


docker stop [container...]
  • This command is used to stop or start a container by simply typing "run".


docker start [container...]


docker push [images..]
  • This command pushes the images to the Docker Hub.

Containerize a simple static website

Build
  • First, we have to make a Dockerfile to make a Docker image. To make a Dockerfile, simply run the command. Here, I am using my simple static website to do the containerizing.


vi dockerfile
  • After this, give the following code:


FROM nginx:latest
COPY . /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
  • FROM: From is where we are pulling our official images from nginx, which is an official image provided by Docker.

  • COPY: Copy is taking our index.html file from the local directory we are working in and moving it into the /usr/share/nginx/html root directory of the nginx server.

  • EXPOSE: The EXPOSE instruction in a Dockerfile is used to specify the network ports that a container will listen on at runtime.

  • CMD: The CMD instruction in a Dockerfile is used to specify the default command that will be run when a container is started from the image.



docker build -t  gym-management-system .
  • This command is used to build your Docker image.


docker run -p 8080:80 gym-management-system
  • This command runs a container from the image using the docker run command, and it also maps port 8080 on the host to port 80 in the container.

  • Visit the website by browsing localhost:8080. You should see your static website running inside the container.



docker push [images...]
  • This command pushes your images to a Docker Hub website.


https://github.com/singhmohit14072002/Minor-Project

You can practice with a repo project or use these steps to configure your application.