Docker
🚢

Docker

Docker helps developers build, share, run, and verify applications anywhere — without tedious environment configuration.
  • YC backed company, started in 2014.
  • Simply ⇒ Docker solves the problem of replicating the environment.

Table of contents



 

Use Cases of Docker


  • Makes setup opensource projects set up locally
    • infact most open source projects have the dockerfile and docker-compose.yaml
  • Deploying applications via docker.

Containers

Allows you to package an application, along with all it’s dependencies and libraries, into a single unit that can run consistently on any machine with a container runtime, such as DOCKER.

What are inside Docker?


  1. Docker Engine
  • Open source containerization technology allows developers to package their application
  1. CLI
    1. command line interface which helps to interact with the docker engine or docker deamon.
    2. Docker engine
    3. Registry (docker hub)
      1. where we push images ( similar to GitHub)
      • docker run -d -p 80:80 docker/getting-started:latest
        • -d is detached mode (doesn’t block your terminal and runs in the background)
        • -p port mapping
        • maps 80 port of your host to 80 port of container
        • notion image
  1. Docker Registry
      • Cloud service for storing and sharing Docker images
      • It’s similar to GitHub, lets you push images

Image vs Containers ( Buzzword)


docker run -d -p 80:80 docker/getting-started
  • you are mapping port 80 of your host machine to port 80 of your Docker container.
  • We're starting a new container from the "docker/getting-started" image.
  • Image is like class and container is the instance of the class

Images


A Docker image is a lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files.
  • Eg:- your codebase on GitHub
  • Images define the initial file system of new containers.
  • Name should be in small letters
  • Image is immutable
    • Example: Think of a Docker image as a cake recipe. The recipe lists all the ingredients (software dependencies) and instructions (configuration) needed to bake the cake (run the application).
  • Building an image from docker file
    • docker build -t image_Name
  1. to check the images
    1. docker images
  • to remove an image
    • docker rmi IMAGENAME --force
  • Deleting an image ( if there are no running containers of the image else we have to delete the containers first)
    • docker rmi imageName/imageId
       

Containers


A container is a running instance of an image. It's the actual application running in an isolated environment.
  • You running node index.js on your machine from some source code on GitHub
  • Container has Separate environment from your pc and also other containers ( isolated environment )
  • To start a container
    • docker run mongo # if -d is added then it opens in detached mode
  • to check all the containers
    • docker ps
  • lists all the containers running or not
    • docker ps -a
  • start a container with containerID
    • docker start xyzId
  • Removes a stopped container
    • docker rm containerId
  • Immediately stops a container by sending a SIGKILL signal
    • docker kill containerID
  • Gracefully stop a running container by sending a SIGTERM(polite and gracefull) signal. followed by SIGKILL( forcefull) if doesn’t stop.
    • docker stop containerID
  • Spin up a new container
    • docker run -it xyz_image
  • Staying in mac and executing commands
    • docker exec containerID <command>
  • Running an interactive shell
    • docker exec -it conatinerName bash #it is interactive # Use ctrl+d to get out of it

Passing env variables


docker run -it -e key=value -e key=value -e key=value ImageName
  • we should give `-e` for every environment variable
 

Docker File


  • Docker file is a text document that contains all the commands a user could call on the command line to create an image.
  • if you want to push your code to dockerhub you need to create a Dockerfile for your application.
  • WE NEED
    • base IMAGE
    • directory
    • commands to run
    • commands to start
  • docker build -t imageName
  • docker run -it -p xyz:xyz imageName to start the container
  • With env
    • docker run -it -p xyz:abc -e port=abc imageName
 

Layers


In Docker, layers are a fundamental part of the image architecture that allows Docker to be efficient, fast, and portable.
  • Layers are cached and reusable across different images, which makes building and sharing images more efficient.
  • Base Layer, instructions layer
  • If a layer changes, all subsequent layers also change
  • Layers are immutable
notion image
  • In the above image creation we can see
      1. Cached [1/6] from the previous build
      1. [2/6] workDIR
      1. [3/6] COPY . .
      1. [4,5,6 /6] are the Instruction layers
  • These are all the layers.

Volumes

  • Suppose we have a mongo container running and if I somehow got the container stopped the data wont be persistant upon restarting the container.
  • To achieve we need to attach volumes.
  • Docker volumes are stored within a virtual machine managed by Docker Desktop.
docker volume create xyz
  • while starting the container attach the volume to make it persistant (/data/db is the location where the data is stored in mongodb)
docker run -v xyz:/data/db -p 27017:27017 mongo
notion image
  • By attaching the container to volumes we can persist the data.

Networks


Network is a powerful feature that allows containers to communicate with each other and with the outside world.
  • By default containers can’t talk to each other.
  • If you have a mongo container running on 27017 and trying to connect to it from other node.js container (here we are using two containers) we can’t connect it.
    • But we can connect in our mac machines.
notion image
  • They can only connect when they are on the same network.
docker network create my_custom_network
  • start the mongo with network attached
docker run -d -v volume_database:/data/db --name mongoCont --network my_custom_network -p 27017:27017 mongo
docker build -t image_tag .
docker run -d -p 3000:3000 --name backend --network my_custom_network image_tag
notion image

Pushing to dockerhub


  • Just like we create a repo in GitHub create a repository
  • Login in CLI
  • Build the image with tag
    • docker build -t xyz:v1_tag .
    • if without tag ( gets latest as the default)
  • Then push it to dockerHub using the tag
    • Tags are like versions.
 
 

Docker Compose


Designed to help you define and run multi-container Docker applications.
  • yaml file similar to json let’s you create key-value pairs.
  • We use this yaml file to configure networks, volumes, and services of our application.
  • Whenever we have multiple services in the same docker compose they are by default attached to same network
  • we got to create volumes
docker-compose up # for building too docker-compose up --build

Resources