Docker Essentials:-

What is Docker?

Docker is a containerization platform for developing, packaging and shipping, and running applications. It provides the ability to run an application in an isolated environment called a container.

Why Use Docker?

Container===>>Makes deployment and development efficient. A way to package an application with all the necessary dependencies and configuration.

  • Container1 -> APP1 -> Node.js version 14 -> Success done๐Ÿ‘

  • Container2 -> APP2 -> Node.js version 16 -> Success done ๐Ÿ‘

DockerVMs(Virtual Machines)
IsolationStrong Isolation
low impact on the host OSHigher impact on the host OS
LightweightResource-intensive
very fastvery Slow

Key Points of Docker:-

Dockerfile:-

A simple text file containing instructions to build a Docker image..

Image:-

A single file that encapsulates all the dependencies and libraries required to run an application.

Container:-

A running instance of a Docker image that executes the application in a lightweight, isolated environment.

Docker Registry:-

A repository for storing and distributing Docker images, with Docker Hub being the most popular public registry.

๐Ÿ’ก
Dockerfile โ†’ Image โ†’ Container


  1. DockerImage:-

     docker build -t <imagename> .
    
    e.g:-docker build -t image3 .
    builds a Docker image named "image3" from the Dockerfile located in the current directory.

Container:-

docker run -i -p 3000:3000 <image-id>
e.g:-docker run -i -p 3000:3000 df91a6294206
starts an interactive container, mapping host port 3000 to container port 3000.

Main Components of Docker

1. Docker Engine

2. Docker Images

3. Docker Containers

4. Dockerfile

5. Docker Registry

6. Docker Compose

7. Volumes

8. Networks

React app containerization:-

Python Code Containerization:-


docker ps

to check if a container is running or stopped

docker stop <container_id>
docker image ls

lists all local Docker images with their names, tags, and sizes.

docker run -d -p 3000:3000 <container id>

runs a Docker container in detached mode (-d), mapping port 3000 on the host to port 3000 in the container.

docker ps -a

lists all Docker containers, including both running and stopped ones.

docker rm <CONTAINER ID>

removes a stopped Docker container specified by its ID.

docker pull python
docker image ls

docker pull python downloads the latest Python image, while docker image ls lists all available Docker images.

๐Ÿ’ก
Nginx can run on port 8080 to serve web applications or as a reverse proxy
docker pull nginx
docker image ls
docker run -p 8080:80 nginx:latest



Connect to an AWS EC2 instance via SSH and run Docker commands directly in the terminal:-

sudo apt-get update
sudo apt-get install docker.io
docker --version
systemctl status docker
sudo reboot
  • sudo apt-get update: Updates the package list for available software.

  • sudo apt-get install docker.io: Installs the Docker engine on the system.

  • docker --version: Displays the installed Docker version.

  • systemctl status docker: Checks the current status of the Docker service.

  • sudo reboot: Restarts the system to apply any changes.

sudo usermod -aG docker $USER----------------->>>It adds the user to the Docker group for sudo-less access to Docker commands
cat /etc/group 
sudo reboot
docker ps
  • sudo usermod -aG docker $USER: allows you to run Docker commands without needing to type sudo each time. It adds your user to the Docker group.

  • cat /etc/group: Displays the contents of the group file, showing group memberships.

  • sudo reboot: Restarts the system to apply changes.

  • docker ps: Lists all currently running Docker containers.

Java with Docker: Develop, Containerize, Deploy:-

docker pull mysql
docker images
 docker run mysql:latest
docker run -d -e MYSQL_ROOT_PASSWORD=ankit@756035 mysql:latest
docker ps
  • docker pull mysql: Downloads the latest MySQL image from Docker Hub.

  • docker images: Lists all the Docker images on your local machine.

  • docker run mysql:latest: Runs a MySQL container in the foreground using the latest image.

  • docker run -d -e MYSQL_ROOT_PASSWORD=ankit@756035 mysql:latest: Runs a MySQL container in detached mode (-d) with an environment variable setting the root password.

docker ps: Displays the currently running containers.

sudo apt-get install tree
 tree .

installs the Tree package using administrative privileges via the APT package manager on Debian-based systems.

docker rmi <image_id>

removes the specified Docker image by its ID from your local system.

Containerize Node.js application:-



0
Subscribe to my newsletter

Read articles from Ankit Chakrabarty directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ankit Chakrabarty
Ankit Chakrabarty