README
DOCKER-IMAGE-AND-CONTAINER
Docker Cheat Sheet: https://find-saminravi99.notion.site/Docker-Cheat-Sheet-10dc48b8ac8c80b79f73ece2abfc6841?pvs=4
GitHub Link: https://github.com/Apollo-Level2-Web-Dev/docker-with-typescript-backend/tree/module-2
WSL Cheat Sheet
Checking WSL Installation
-
List Installed Distributions:
wsl --list --all -
List Running Distributions:
wsl --list --running -
Check Available Online Distributions:
wsl --list --online -
Check WSL Version:
wsl --list --verbose -
Check if WSL Feature is Enabled:
dism.exe /Online /Get-Feature /All | find "Microsoft-Windows-Subsystem-Linux"
Installing a New Distribution
-
Install a Distribution: (Replace
<distro-name>with the name of the distribution, e.g.,Ubuntu.)wsl --install -d <distro-name>
Uninstalling a Distribution
-
Unregister (Uninstall) a Distribution:
wsl --unregister <distro-name> -
Check for Remaining Files:
- Path:
C:\\Users\\<your-username>\\AppData\\Local\\Packages\\<distro-folder>\\LocalState\\ - Ensure no
.vhdxfiles remain.
- Path:
Uninstalling WSL Completely
- Remove Installed Distributions:
- Go to Settings → Apps → Apps & Features and uninstall each distribution.
- Disable WSL Feature:
- Open Windows Features (
optionalfeatures.exe). - Uncheck Windows Subsystem for Linux and Virtual Machine Platform (if applicable).
- Open Windows Features (
- Remove WSL Kernel Update:
- Go to Settings → Apps → Apps & Features and uninstall Windows Subsystem for Linux Update.
Starting WSL
-
Open a Distribution:
wsl -d <distro-name> -
Open from Start Menu:
- Search for the distribution (e.g., Debian, Ubuntu) in the Start Menu.
Logging Out of WSL
-
Logout Command: or
exitlogout
Terminating WSL
-
Terminate a Running Distribution:
wsl --terminate <distro-name>
Additional Tips
- To update your WSL kernel manually, you can check Microsoft's WSL documentation for the latest version.
Docker Cheat Sheet
Getting Started with Docker
-
Check Docker Installation:
docker --version
Basic Commands
-
Pull an Image from Docker Hub:
docker pull <image-name>Example:
docker pull ubuntu -
List Available Images:
docker images -
Run a Container:
docker run -it <image-name>Example:
docker run -it ubuntu -
Run a Container in Detached Mode:
docker run -d <image-name> -
Stop a Running Container:
docker stop <container-id> -
Start a Stopped Container:
docker start <container-id> -
Remove a Stopped Container:
docker rm <container-id> -
Remove an Image:
docker rmi <image-name>
Managing Containers
-
List Running Containers:
docker ps -
List All Containers (including stopped):
docker ps -a -
View Container Logs:
docker logs <container-id> -
Execute a Command in a Running Container:
docker exec -it <container-id> <command>Example:
docker exec -it <container-id> bash -
Inspect a Container:
docker inspect <container-id>
Networking
-
List Docker Networks:
docker network ls -
Create a New Network:
docker network create <network-name> -
Connect a Container to a Network:
docker network connect <network-name> <container-id> -
Disconnect a Container from a Network:
docker network disconnect <network-name> <container-id>
Volumes and Data Management
-
Create a Volume:
docker volume create <volume-name> -
List Volumes:
docker volume ls -
Remove a Volume:
docker volume rm <volume-name> -
Mount a Volume to a Container:
docker run -v <volume-name>:<container-path> <image-name>
Dockerfile and Building Images
-
Create a Dockerfile: Basic structure:
FROM <base-image> MAINTAINER <your-name> COPY <source> <destination> RUN <command> CMD ["<executable>"] -
Build an Image from a Dockerfile:
docker build -t <image-name>:<tag> .Example:
docker build -t myapp:latest . -
List Built Images:
docker images
Advanced Commands
-
Tag an Image:
docker tag <image-id> <new-image-name>:<tag> -
Push an Image to Docker Hub:
docker push <image-name>:<tag> -
Save an Image to a Tar File:
docker save -o <path-to-output-file> <image-name> -
Load an Image from a Tar File:
docker load -i <path-to-input-file> -
Docker Compose:
-
Start Services:
docker-compose up -
Stop Services:
docker-compose down
-
-
Scale Services:
docker-compose up --scale <service-name>=<number> -
View Running Docker Compose Services:
docker-compose ps
Useful Docker Commands
-
Remove All Stopped Containers:
docker container prune -
Remove Unused Images:
docker image prune -
Remove All Unused Data (containers, networks, images):
docker system prune -
Get Docker System Information:
docker info
2-1 What is Images & Containers?
- While creating a project docker provides a container. By running the container we can run our project.we have already done a project and build an image
What is image?
-
Docker gives us a container which means a unit of software which holds the codes and the environment for running the code. For running this container we need a blueprint. This blue print is called image. The image holds the codes and the environment. Using the same image we can run multiple container (create and manage). Suppose we have an image that we have built on node.js and this is a node.js project. we can create multiple container of this project and and run separately. We can share the exact container within team mate and it will work smoothly and show output.
-
Interesting fact about the image is : The image is read only and we can not change the code. But when we create a container using the image we can do both read and write. Image is a static paper which is the blueprint.
-
The image is like a snapshot. using the image we can create container and run the container.
2-2 Using Pre-built Images
-
FOR CREATING CONTAINER WE WILL NEED IMAGE. WE CAN GET IMAGE IN TWO WAYS
- Prebuilt image (might be coming from docker hub/ team mates)
- Create Your Own Custom Image (write your own docker file based on another image)
-
Now lets work with a prebuilt image first.
-
lets got to docker hub and get the node image docker hub node , This is an official image of node. If wer want we can run this image in our own machine
-
open the power shell and tun the docker desktop and engine on.
docker run node
- in power shell write this.
- This will first search time image locally. and if not found it will download the image from the hub.

- for showing the running process we will write
docker ps -a
- here
psmeans process and-ameans all.

-
here we can see the node image container tried to run and exited immediately.
-
we can run our locally installed node in our pc like operating using node .

- if we want to do this similar kind of node operation in terminal using the image what we have grabbed right now. now lets do it
docker run node
- This command will not act like earlier because the image is already downloaded and now it will run in a container

- it has exited immediately. But we want to run the container and do some interactions like node.
docker run -it node
- here
itmeans interactive. it will be continuously running and we can do some interactions. but if it was a server we do not have to useitBecause server will run until we close. as node is not a server it runs init

- its up now
- we can wo operations like node installed in our machine

2-3 Writing Our First Dockerfile
- Basic project setup is done
- Now lets dockerize our own projects. for this we will need to build an image from the project. and then the image should be run in a container.
- lets write a
docker filein the root folder
# Use Node.js 20 as the base image
# This means our container will run in a Node.js 20 environment
# Think of this like installing Node 20 on a fresh Linux machine
FROM node:20
# Set the working directory inside the container
# This tells Docker: "From now on, run all commands inside /app"
# It also creates the folder automatically if it does not exist
#
# Why important?
# - All files we copy will go here (unless we specify another path)
# - npm install will run inside this folder
# - Our project will live here
#
# Without WORKDIR, Docker would use root (/) which is messy
WORKDIR /app
# Copy only package.json first
# First "." = source (your local project folder)
# Second "." = destination inside container (current WORKDIR => /app)
#
# Why copy package.json first?
# Docker caching optimization:
# If dependencies don't change, Docker won't reinstall npm packages
COPY package.json .
# Install dependencies inside the container
# Runs inside /app because of WORKDIR
# Equivalent to:
# cd /app && npm install
RUN npm install
# Copy the rest of the project files into the container
#
# First "." = copy everything from current local folder
# Second "." = paste into current container folder (/app)
#
# So this means:
# Local project → /app inside container
COPY . .
# Expose port 5000
# This is just documentation for Docker
# It tells: "This app runs on port 5000 inside container"
# To access from outside we still need -p mapping
# we know the container is isolated so we need to connect through the port. basically we are telling docker that you expose through 5000 port to the pc
EXPOSE 5000
# Default command when container starts after image
# Runs: npm run dev
# Example: starts Express dev server or Vite or Next.js dev mode
CMD ["npm", "run", "dev"]
# Its basically npm run dev which will run over container not while building the image
FROM node:20
COPY . /app
RUN cd /app && npm install
FROM node:20
WORKDIR /app
COPY . .
RUN npm install
- the flow is
Start container
↓
Create /app
↓
Enter /app (WORKDIR)
↓
Copy package.json → /app
↓
npm install in /app
↓
Copy rest of files → /app
↓
Run app from the image
2-4 Building an Image & Running Container Based On Our Image
- now lets build a image from the docker file we have made
docker build .
# in the terminal
- this is going to build an image of the docker and the
.means the current directory. if the Dockerfile is in /src the the command will bedocker build ./src
docker images
- in the terminal if we write this it will show the i9mages for the project
docker run -p 5000:5000 sha256:46e4f3eba05896ff1c4bb607fce55c3031a13c877e7cea56ed2f04f1068b6027
## left 5000 means external and right is internal
or
docker run -p 5000:5000 46e4f3eba058

-
this will run the project
-
show just the running containers
docker ps
- show all the container available
docker ps -a
- to stop the container
docker container stop clever_agnesi

2-5 Deep Dive Into Docker Images
- Docker image is a read only file. If we change it will not reflect the changes.
- Image builds one time so i is not gets automatically aware of the changes. we need to build again and start the container again
- Yes, this can be a problem, especially when we make frequent changes during development. Rebuilding the container every time can feel slow and inefficient.To solve this, Docker provides faster ways to connect with the container so that changes reflect immediately in the output. We’ll learn these techniques gradually (like volume mounting and live reload).Also, Docker uses a caching mechanism during builds. When rebuilding an image, Docker only rebuilds the layers that have changed. Any unchanged layers are reused from the cache. This makes the rebuild process much faster and more efficient.
# Use Node.js 20 as the base image
# This means our container will run in a Node.js 20 environment
# Think of this like installing Node 20 on a fresh Linux machine
FROM node:20
# Set the working directory inside the container
# This tells Docker: "From now on, run all commands inside /app"
# It also creates the folder automatically if it does not exist
#
# Why important?
# - All files we copy will go here (unless we specify another path)
# - npm install will run inside this folder
# - Our project will live here
#
# Without WORKDIR, Docker would use root (/) which is messy
WORKDIR /app
# Copy only package.json first
# First "." = source (your local project folder)
# Second "." = destination inside container (current WORKDIR => /app)
#
# Why copy package.json first?
# Docker caching optimization:
# If dependencies don't change, Docker won't reinstall npm packages
COPY package.json .
# Install dependencies inside the container
# Runs inside /app because of WORKDIR
# Equivalent to:
# cd /app && npm install
RUN npm install
# Copy the rest of the project files into the container
#
# First "." = copy everything from current local folder
# Second "." = paste into current container folder (/app)
#
# So this means:
# Local project → /app inside container
COPY . .
# Expose port 5000
# This is just documentation for Docker
# It tells: "This app runs on port 5000 inside container"
# To access from outside we still need -p mapping
# we know the container is isolated so we need to connect through the port. basically we are telling docker that you expose through 5000 port to the pc
EXPOSE 5000
# Default command when container starts after image
# Runs: npm run dev
# Example: starts Express dev server or Vite or Next.js dev mode
CMD ["npm", "run", "dev"]
# Its basically npm run dev which will run over container not while building the image
2-6 Managing Images & Containers
- for all the command of docker
docker --help
- for in depth help tell the process and help
docker container --help
-
When we build a image
- we can give tag to the image
- can be listed docker images
- can be analyzed docker image inspect
- can be removed docker image using terminal as well.
-
When we Make a container
- can be named
- can be configures in detail
- can be listed docker ps
- can be removed docker rm
-
now lets see in practical
Image
docker build --help
docker run --help
- There is a problem each and every time we try to run a container it creates a new container.
- If no code changes we can restart the same container instead of creating a new one.
docker run -p 5000:5000 sha256:9e18c5d209a67745a6b5c1b12ec75221dab5c46c5460ebc92f65f54969d666f5
- nwe can run the container maually using the container id or name
docker container start <container-id or name>
- but the problem is when we run using image it will be interactive and while running manually it will not be interactive.This is called detached mode. Its like it will be running in detached mode and so we need to attach the terminal to the container. Running using image is attached mode.
2-7 Detached Mode & Interactive Mode
- when we run a container using the image it will be in interactive mode. but if we run the container manually it will be in detached mode.
- we can run the image and make it detached mode using
-dflag.
docker run -p 5000:5000 -d sha256:9e18c5d209a67745a6b5c1b12ec75221dab5c46c5460ebc92f65f54969d666f5
- we can also run the container in interactive mode using
-itflag.
docker run -p 5000:5000 -it sha256:9e18c5d209a67745a6b5c1b12ec75221dab5c46c5460ebc92f65f54969d666f5
- we can also attach the terminal to the running container using
docker container attach <container-id or name>
docker container start --attach <container-id or name>
- this will be interactive mode and we can do some interactions. but if it was a server we do not have to use
itBecause server will run until we close. as node is not a server it runs init
So the question is when we should use detached mode and when we should use interactive mode?
- If we want to run a server or an application that should keep running in the background, we should use
detached mode. This is common for production environments where you want your app to be always available without needing to keep a terminal open. - If we want to do development work, debugging, or any task that requires direct interaction with the container's terminal, we should use
interactive mode. This allows us to run commands, see real-time output, and make changes on the fly.
2-8 Deep Dive Into Container
- image and container takes a huge storage . so the managing container and image is very important.
- see the images
docker images
- see the container
docker ps
docker ps -a
lets see how we can remove he docker images
docker image rmi sha256:9e18c5d209a67745a6b5c1b12ec75221dab5c46c5460ebc92f65f54969d666f5
// or we can add multimle image id to remove multiple images at a time
docker image rmi <image-id1> <image-id2> <image-id3>
docker image prune
- remember we can not delete a image if any container is made from it
lets remove a container
docker container rm charming_feynman
// or we can add multiple container id to remove multiple container at a time
docker container rm <container-id1> <container-id2> <container-id3>
- if we want to delete all the container at a time we can use this command
docker container prune
one another concept is if we stop a container or image it will automatically remove after some time. this is called auto remove. we can use --rm flag to do this.
docker run -p 5000:5000 --rm moby-dangling@sha256:4ea772ea64a5439ff850dd67de08e76efc7f9ca065ed44a154fe424ebc39268e
2-9 Naming & Tagging Container & Images
- when we were working it was giving weird id and a container name we need to configure on our own way.
- this will add a tag to the image
docker build -t docker-ts:1 .
- here
docker-tsis the name of the image and1is the tag

- suppose we will change in code and we want to build again we can give the same name but different tag
docker build -t docker-ts:2 .
- we can run the container using the name and tag
docker run -p 5000:5000 --rm docker-ts:2
- now lets set a name to the container
docker run -p 5000:5000 --rm --name docker-app-container docker-ts:2
- as we have used proper naming for images and container the
prunecommand will not be able to delete the images and container because they are not dangling. so we need to remove them manually using the name or tag. or use-a tag
docker image prune -a
2-10 Pushing Docker Image to DockerHub

- first login to the docker hub in the terminal
docker push sazid60/ts-node-back:tagname
- remember the image name and the repository name should be same
docker build -t sazid60/ts-node-back:v1 .
docker push sazid60/ts-node-back:v1
- we can also rename the image name as well
docker image tag docker-ts:2 sazid60/ts-node-back:v2
2-11 Pulling Docker Image to DockerHub
docker pull sazid60/ts-node-back:v1
docker run -p 5000:5000 --rm --name docker-app-container sazid60/ts-node-back:v1
- this will work like if available then run. if not it will fetch (pull) and run
- image and container understanding complete