Let's learn Docker

Let's learn Docker

What is a Container

A container is a running process with resource and capability constraints managed by a computer’s operating system. The files available to the container process are packaged as a container image. Containers run adjacent to each other on the same machine, but typically the operating system prevents the separate container processes from interacting with each other.

Container Image

A container image is an immutable, static file containing the dependencies for the creation of a container. These dependencies may include a single executable binary file, system libraries, system tools, environment variables, and other required platform settings. Container images result from an application's containerization and are typically stored in container registries, where they can be downloaded and run as an isolated process using a Container Runtime Interface (CRI).

Why do we need containers

Container images bundle an application with any of its runtime dependencies, such as an application server. This provides consistency across all environments, including a developer's machine. No More excuses 'It works on my computer' Also, VMs are significantly larger than containers and require a hypervisor to run. Due to the storage, backup, and transfer of these larger VM templates, creating the VM templates is also slow.

containers have

  1. orchestration
  2. engine
  3. runtime

RUNTIME

  • runc it works with the OS and starts and stops the containers
  • containerd it is at a higher level, managing runc and manages the container like how to interact with your container with the network like how to get the information from the interet to the container

ENGINE

here docker daemon

ORCHESTRATION

Kubernetes

Dockerfile -> Container_Image -> container

Learn Basic Docker Commands

$ docker ps  # list currently running containers

$ docker ps -a  # list all the stopped containers which are not deleted

image.png

$ docker pull <image_name>:<tag>  # to fetch / pull the container image from docker hub

image.png

$ docker build -t <name_of_image> <path_of_Dockerfile>  # used to create image out of Dockerfile

$ docker images  # list of all the images stored locally

image.png

$ docker run <image_name>:<tag>  # run a image by create a container from it

$ docker run -it <image_name>:<tag>  # running container and launching it in interactive mode like bash

$ docker inspect <continaer id>    # all the information about the given container

$ docker logs <container id>  # all the outputs from a container even the dead ones

$ docker container prune -f  # to delete all the stopped containers

image.png

$ docker rmi <image>  # to remove a specific image

$ docker rmi $(docker images -q)  # to delete all the images

image.png

$ docker exec -it <container_id> bash  # run a command in a running container

$ docker push <image_name>:<tag>  # Push an image or a repository to a registry

image.png

Advance Topic

Docker volumes

docker run --it -v <path_of_host_computer>:<path_of_container> <image_name>

image.png

Docker port forwarding

docker run -p <host_port>:<container_port> <images_name>

image.png

image.png

Finally the declarative method is Dockerfile

FROM ubuntu:latest

COPY . /app

WORKDIR /app

CMD [ "echo", "Hello World from Docker" ]

FROM -> is the image name

COPY -> to copy the project files from host_path TO container_path

WORKDIR -> change the working directory to the specified one

CMD -> to execute bash commands

Thank you 🙂

Happy coding