Skip to content

Latest commit

Β 

History

14 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Docker

Dockerization is alternative for virtualization as it is light weight, and easily shareable than vm machines.

Diffrence between vitualization and Dockerization

1- In virtualization vm machine have full blown OS. 2- Kernel is not shared, each vm has its own kernel. 3- Can run multiple type of OS on VM.

1- In dockerization we can run something known as containers of any type of images(linux, windows, mac). 2- Kernel is shared, it means if the host machine have linux kernel i cannot run windows kernel. 3- These docker images are easily shareable as these are of small in size than VM, and are not a full blown OS these are just slice of OS.

Feature Virtualization Dockerization
Full OS βœ… Yes ❌ No
Kernel Separate kernel per VM Shared host kernel
OS types Multiple OS types possible Same kernel only
Startup time Slow Fast
Image size Large (GBs) Small (MBs)
Resource usage High Low

How to optimze the size of the images

  • Make sure to build the images as less of space as possible
  • Also add the .dockerignore file to ignore the folders/files that are not needed in the container.

Port exposing

EXPOSE PORT Numbers either can be like this 3000-3009 it will expose port in this range.

Port mapping

  • The container application is running in the isolated enviroment on a port but that port is not working in the host machine so how do we port the application to run in host machine we will use port mapping how ?

docker run -it -p port of host machine:port of app in container

Automatic Port Mapping

  • If our project is large and using multiple ports we have to manually map the ports so to tackle this we use automatic port mapping by exposing the ports in dockerfile and run it like this :

docker run -it -P

you have to check like - docker ps

Docker Commands

πŸš€ Docker Cheat Sheet

πŸ“¦ Build the image

docker build -t <image-name> .

▢️ Run the container

docker run -it <image-name>

πŸ“‹ Show running containers

docker ps

πŸ“‚ Show all containers

docker ps -a

▢️ Start a container

docker start <container-name>

⏹️ Stop a container

docker stop <container-name>

πŸ’» Open a Bash shell inside the container

docker exec -it <container-name> bash

πŸ“Š View image history

docker history <image-name>

🌍 Run with environment variables

docker run -it \
  -e NODE_ENV=production \
  -e PORT=3000 \
  <image-name>

🌐 Docker Network Commands

List all Docker networks

docker network ls

Inspect a network

docker network inspect <network-name>

Create a new network

docker network create <network-name>

Run a container and attach it to a network

docker run -it --network <network-name> <image-name>

Connect container to network

docker network connect <network-name> <container-name>

Disonnect container to network

docker network disconnect <network-name> <container-name>

How to publish your custom docker images

1- First we have to creat an account on docker hub. 2- Create a repository on the dockerhub let say node-app. 3- After creating a repo it will be something like this <your-username/node-app> 4- Build the docker image locally 5- Now push the image in repository of docker hub by docker push <your-username/node-app>

Docker compose

  • We use Docker Compose to run multiple containers together easily. If we have a full stack app containing frontend backend database without docker compose we have to create seprate images and have to remembers commands and ports management, run seprately one by one with the help of docker compose we can build images, manage port and running instantly by one command in a single file called docker-compose.yml like this
# multiple containers
services:
  # backend container
  backend:
    # name of the image to build and the path to the Dockerfile
    build: ./backend

    # single environment variable
    environment:
      - PORT=5000

    # multiple environment variables via file
    env_file:
      - ./backend/.env

    ports:
      - 8000:5000

  frontend:
    build: ./frontend
    ports:
      - 5173:5173

Multi Stage Build optimized images for production

  • When building images for production, we should avoid including development-only code, tools, and dependencies. Instead, the final image should contain only the compiled/build artifacts and the files required to run the application in production.

This is why multi-stage builds are commonly used. In the first stage, the application is built and all development dependencies are used. In the final stage, only the build output and required runtime dependencies are copied into the production image.

This approach reduces the image size, improves security by excluding unnecessary files, and results in a cleaner and more efficient production image.

Docker Networking

First understand about the network in our local machine (host). In host machine we have anetwork driver hardware layer which get connected to the router/wifi internet and by which we can access the internet.

Now when we install the docker in our system the docker comes up with the network interface layer called the bridge which connects with our router/wifi. So when we spin up the container docker by default attach the network with it containing the bridge configuration on it and by that they can access the network. By dafault there are three built-in network provided by the docker.

1- Bridge (default one) 2- Host 3- none

  • Host Here we dont have to provide the port maping because when you attach network host on container it will use host network.
  • none Providing the network none to container will result out that it will not run or expose on any network.

Docker Custom Networks

By default, Docker places containers on the default bridge network. Although containers on the same network can communicate, it is a best practice to create a user-defined bridge network for applications that consist of multiple services.

A user-defined network provides:

  • Automatic DNS resolution (containers can communicate using their names).
  • Better isolation from other containers.
  • Easier management of multi-container applications.

For example, suppose we have a full-stack application consisting of:

  • A Node.js application
  • A PostgreSQL database
  • A Redis cache

First, create a network:

docker network create fullstack

Then run each container on the same network:

docker run -d --name node-app --network fullstack node-app

docker run -d --name postgres-db --network fullstack postgres

docker run -d --name redis-cache --network fullstack redis

Now the containers can communicate using their container names.

For example, the Node.js application can connect to:

  • PostgreSQL using postgres-db
  • Redis using redis-cache

There is no need to know the containers' IP addresses because Docker automatically provides DNS-based name resolution within the network.

Docker Volumes

Whenever we stop or delete the containers our data inside that containers will be lost, in production suppose we are building an ecommerce app which uses the React Node Postgres now imagine if we have updated something in the db container and we recreated it now all the data of the users, orders, payment will be lost so to tackle this docker provide a way by which we can permanantly store data even if the containers are deleted. There are two ways;

1- Bind mount

In binds mount we mount the specific file or folder from the host into a container. You just have to privide the location of folder. It is good when we are in development and is more flexible as compared to the volume. It is controlled by the user host itself. We can backup data manually. The changes are reflected inside mounted folder or file instantly.

docker run -it -v "location inside host:location inside the container" my-app

Docker volume

We create a volume inside the docker.The volume is managed by the docker. We can do it via command. Its best use case is in production where we want safe backup and portability. The folder will be created inside the docker volume app-data.

docker run -d -v app-data:/user/share/app/data node-app 

About

The repo contains the documentation of the docker for beginners one.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages