docker

Docker containerizes code for consistent and reproducible execution across different machines.

Image = blueprint for container

Docker Image layers

Image contains:

  • runtime environment

  • application code

  • dependencies

  • extra config (e.g. env variables)

  • commands

Container = runnable instance of an image , runs the application

How to use docker

  1. Create Dockerfile which is a script that contains a set of instructions for building a Docker image

DockerFile :

Create .dockerignore file so that docker ignores files such as node_modules

  1. Run docker build . to build a docker image

  2. Run docker run -p 3000:3000 58ee4942eb1a creates a container from the docker image (58ee4942eb1a) and runs it on port 3000

  3. See list of running containers docker ps

  4. To stop docker container(eg. c1) docker stop c1

  5. See list of existing containers docker ps -a

  6. To start a existing docker container docker start c1

Layer Caching in Docker

When you build an image, Docker caches each layer so that if a layer hasn't changed, it doesn't need to be rebuilt, saving time and resources.

Before layer caching:

After layer caching:

If there are no changes in dependencies, and only changes in source files, a significant amount of time will be saved during the image-building process as the dependencies are not reinstalled from npm install since they are cached.

Docker Image with Tag

Docker image tags are used to create different versions of an image. Tags help identify and manage specific versions of your application within Docker.

These commands demonstrate how to build a Docker image with a specific tag (v1) and then run a container using that tagged image. The tag serves as a version identifier, allowing you to manage different releases or variations of your application.

Docker volumes

Docker volumes allow you to persist and share data between containers and the host machine. The -v flag in the docker run command is used to create and manage volumes.

Changes made in that local directory on the host machine are reflected immediately inside the container

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define all services, networks, and volumes in a single YAML file (docker-compose.yml) for easy orchestration.

Docker Hub

Use docker hub to push and pull images

  1. Build docker image

  1. Tag docker image with username and version tag

  1. Push docker image

Using docker image from docker hub repository

  • Pull image from docker hub and run container

  • Use in docker-compose.yaml ( use image in frontend & backend to build containers during docker-compose)

Last updated