Page 1 of 11
1. Why use Docker?
- Docker makes it
realy→ really easy to install and run software without worrying about setup or dependencies.
2. What is Docker?
Before diving in, it's important to understand the Docker Ecosystem:
classDiagram
class Docker_Ecosystem {
Docker_Client
Docker_Server
Docker_Machine
Docker_Images
Docker_Hub
Docker_Compose
}
Docker is a platform or ecosystem designed around creating and running containers.
Example:
- Hello-world or Ubuntu:
- Image → A single file with all the dependencies and configurations required to run a program.
- Container → An instance of an image that runs a program.
stateDiagram-v2
Image --> Container : Instance of app image
Image --> Container : Runs a program
Page 2 of 11
3. What is a Container?
A container is a subset of processes with a set of resources assigned to it.
classDiagram
class Container {
Program
Kernel
RAM
Network
CPU
Filesystem
}
Components of an Image:
classDiagram
class Image {
FS_snapshot
Startup_Command
Libraries
Runtime
}
Docker Commands:
(i) To create and run a container:
docker run <image-name>
- Docker client interacts with the Docker server.
(ii) To list all running containers:
docker ps # Currently running
docker ps -a # All
Page 3 of 11
Notes:
docker run = docker create + docker start
(iii) Removing/Deleting Containers:
docker system prune
(iv) Start a specific container:
docker start -a <container-id>
(v) Get logs from a container:
docker logs <container-id>
(vi) Stop a container:
docker stop <container-id> # SIGTERM (Terminate signal; allows cleanup)
docker kill <container-id> # SIGKILL (Forcefully terminates immediately)
(vii) Multi-command in Docker:
docker exec -it <container-id> <command>
- Interactive Mode: Allows running commands inside containers.
- Because containers are isolated.
Page 4 of 11
Creating a dedicated command prompt for a container:
docker exec -it <container-id> sh/bash
- Examples:
lspwdredis-cli
Attach to Docker Run:
docker run -it <image-name> sh/bash
- Note: Not recommended.
Containers are isolated:
Containers are isolated from the host machine and from each other.
Docker Images
classDiagram
class Docker_Images {
Dockerfile
Docker_Client
Docker_Server
Usable_Image
}
Creating a Dockerfile:
- Specify a base image.
- Run some commands to install additional programs.
- Specify a command to run when the container starts.
Page 5 of 11
Writing Dockerfiles
Example of a Dockerfile:
# Use an existing Docker image as a base
FROM alpine
# Download & install a dependency
RUN apk add --update redis # (For alpine Linux, install redis)
# Tell the image what to do when it starts as a container
CMD ["redis-server"]
Docker Build Process
Command: docker build
FROM
# Pull image from Dockerhub
RUN
# Create new image & run this container for this particular command
CMD
# Get container ready by running the container & removing intermediate layers (startup command)
Notes:
- Docker makes great use of cache & runs commands swiftly.
Common Docker Commands:
docker run <container-id>
docker build -t <desired-name/project-name:version>
Page 6 of 11
Manually Creating Commits
Example commands:
docker run -it alpine
apk add --update redis
CMD ["redis-server"] # Containerized
docker commit -c 'CMD ["redis-server"]' <container-id>
Creates a new SHA1 commit. Note: Ensure the container is stable enough before committing.
Running a Container:
docker run <new-id>
Port Mapping
Example command:
docker run -p 5000:8080 <image-name>
- Maps incoming requests on port 5000 to port 8080 on the container.
Networking Between Containers
Option 1: Use Docker CLI's network features.
Option 2: Docker Compose
- Purpose: A tool for defining & running multi-container Docker applications.
- Separate CLI that gets installed alongside Docker.
- Used to start up multiple Docker containers at the same time.
- Automates some of the long-winded arguments to
docker run.
Docker Compose Example:
version: '3'
services:
redis-server:
image: 'redis'
node-app:
build: .
ports:
- "4000:8080"
Page 7 of 11
Docker Compose Commands
- Run & Build:
docker-compose up # For image creation & run
docker-compose up --build # Build & run
- Run in Background:
docker-compose up -d
docker-compose ps
docker-compose down
docker-compose logs
Docker Compose Container Maintenance
Think of it like PM2 for managing processes.
Restart Policies:
"no"→ Never restart."always"→ Always restart."on-failure"→ Restart on failure."unless-stopped"→ Restart unless manually stopped.
Page 8 of 11
Running Dockerfile with Custom File Name
Command:
docker build -f ./Dockerfile.dev .
Docker Volumes
Example Command:
docker run -v /app/node-modules:/app <image-name>
Diagram illustrating Docker Volumes:
graph LR A[Local Folder] --> B[Docker Container] A[Frontend] A --> C[/src] A --> D[/public] A --> E[/node_modules] B[Backend] B --> F[/src] B --> G[/public] B --> H[/node_modules]
Using Docker Compose for Volumes
volumes:
- /app/node-modules
- ./app
Page 9 of 11
Docker Bind Mounts vs Volumes
- Volumes:
COPY also works but it's not efficient→ COPY also works, but it is less efficient.
Volumes are used to link a directory from inside the nginx container.
Example Command:
docker run -d -p 8080:80 -v ~/projects/docker/html:/usr/share/nginx/html --name custom-nginx nginx
Use Cases:
- Share data from host machine to nginx container.
- Share data from nginx container to host machine.
Example for logs:
-v ~/projects/docker/logs:/var/log/nginx
This will transfer the nginx container files to:
~/projects/docker/logs
Building New Docker Images
- A Dockerfile is a text document that contains all the commands you could normally execute manually in order to build a Docker image.
Steps:
- Build the image using the following command:
docker build -t mynginx-image .
- Push the image to Docker Hub:
docker login
docker tag [image] [username/repository:tag]
docker push [username/repository]
Page 10 of 11
Docker Services
- A web app contains a DB, Redis server, and much more to coordinate.
- In Docker, we can orchestrate such "services" by combining multiple images.
- This can be done using a Dockerfile, but docker-compose comes to the rescue.
Example:
For a ToDo Application, we need:
- REST API
- MongoDB Server
Dockerignore:
A .dockerignore file is like .gitignore.
Example Dockerfile for a Node.js Application:
FROM node
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 4000
CMD npm start
Page 11 of 11
Steps for Docker Compose:
Step 1:
Add docker-compose.yml informing about services:
- Node App
- MongoDB
Step 2:
Run the following command:
docker-compose up
This will run both services, and bing bong done.
References & Related Topics:
- Docker Documentation
- Related topics: Kubernetes, Containerization, Virtualization.