vault backup: 2025-06-29 13:49:40
This commit is contained in:
@@ -0,0 +1,243 @@
|
||||
|
||||
## What is Docker?
|
||||
|
||||
Docker is a platform that allows you to package applications with all their dependencies into a standardized unit called a **container**. These containers are portable, isolated, and consistent across environments.
|
||||
|
||||
---
|
||||
|
||||
## What is a Container?
|
||||
|
||||
A container is a lightweight, standalone executable package that includes everything needed to run an application: code, runtime, libraries, and configurations.
|
||||
|
||||
Containers are stored in **container repositories**:
|
||||
- Public repositories: [Docker Hub](https://hub.docker.com)
|
||||
- Private repositories: Used by organizations for internal deployments
|
||||
|
||||
---
|
||||
|
||||
## Why Containers?
|
||||
|
||||
### Before Containers:
|
||||
- Developers shared artifacts (e.g., `.jar` files) with setup instructions.
|
||||
- Operators had to install dependencies manually.
|
||||
- Setup was error-prone and inconsistent across OS environments.
|
||||
|
||||
### With Containers:
|
||||
- Everything is bundled together and works the same everywhere.
|
||||
- No need to install dependencies manually.
|
||||
- Runs in its own isolated environment.
|
||||
- Easy to version, share, and deploy (just one command).
|
||||
- Multiple versions of the same app can run simultaneously.
|
||||
|
||||
---
|
||||
|
||||
## Image vs. Container
|
||||
|
||||
|Term|Description|
|
||||
|---|---|
|
||||
|**Image**|A snapshot or package (the blueprint). Immutable.|
|
||||
|**Container**|A running instance of an image. Has its own file system, environment, and process.|
|
||||
Running an image creates a container.
|
||||
|
||||
---
|
||||
|
||||
## Docker vs Virtual Machine
|
||||
|
||||
| Feature | Docker | Virtual Machine |
|
||||
| --------------- | ------------------ | ------------------ |
|
||||
| Virtualizes | Application layer | Full OS kernel |
|
||||
| Startup time | Seconds | Minutes |
|
||||
| Size | MBs | GBs |
|
||||
| Isolation | OS-level | Hardware-level |
|
||||
| Performance | Near-native | Heavier overhead |
|
||||
| Host dependency | Shares host kernel | Has its own kernel |
|
||||
|
||||
Docker runs natively on Linux; on Windows/macOS it uses **Docker Desktop**, which runs Linux under WSL2 or HyperKit.
|
||||
|
||||
---
|
||||
|
||||
## Docker Architecture
|
||||
|
||||
### Layers of a Docker Image:
|
||||
|
||||
- Base Layer: Usually a minimal Linux distribution (e.g., `alpine`)
|
||||
- Application Layer: Your app and its dependencies
|
||||
|
||||
Each image is made of **layers** stacked on top of each other.
|
||||
|
||||
---
|
||||
|
||||
## Docker Installation
|
||||
|
||||
To use Docker on:
|
||||
|
||||
- **Linux**: Install Docker engine directly.
|
||||
- **Windows/macOS**: Use Docker Desktop, which includes WSL2 integration or virtualization backend.
|
||||
|
||||
---
|
||||
|
||||
## Docker Commands: Basics
|
||||
|
||||
```bash
|
||||
# Run a container from an image
|
||||
docker run image-name
|
||||
|
||||
# List running containers
|
||||
docker ps
|
||||
|
||||
# List all containers (running and stopped)
|
||||
docker ps -a
|
||||
|
||||
# Stop a running container
|
||||
docker stop CONTAINER_ID
|
||||
|
||||
# Start a stopped container
|
||||
docker start CONTAINER_ID
|
||||
```
|
||||
|
||||
### Port Binding
|
||||
|
||||
```bash
|
||||
docker run -p HOST_PORT:CONTAINER_PORT image-name
|
||||
```
|
||||
|
||||
This binds a container’s port to a specific port on your machine.
|
||||
|
||||
---
|
||||
|
||||
## Debugging Containers
|
||||
|
||||
```bash
|
||||
# View logs
|
||||
docker logs CONTAINER_ID
|
||||
|
||||
# Start a container with detached mode and port
|
||||
docker run -d -p 3000:3000 image-name
|
||||
|
||||
# Open an interactive shell inside a running container
|
||||
docker exec -it CONTAINER_ID /bin/bash
|
||||
```
|
||||
|
||||
You can assign names to containers using `--name`.
|
||||
|
||||
---
|
||||
|
||||
## Docker Networking
|
||||
|
||||
### Concept:
|
||||
|
||||
Containers can communicate with each other over a virtual network.
|
||||
|
||||
```bash
|
||||
# List networks
|
||||
docker network ls
|
||||
|
||||
# Create a new network
|
||||
docker network create my-network
|
||||
|
||||
# Run container in a network
|
||||
docker run --net my-network ...
|
||||
```
|
||||
|
||||
Example: MongoDB + Mongo Express on same network can communicate via service name.
|
||||
|
||||
---
|
||||
|
||||
## Docker WSL2 Error (Windows)
|
||||
|
||||
### Issue:
|
||||
|
||||
```
|
||||
Failed to configure network (networkingMode Nat)...
|
||||
```
|
||||
|
||||
### Fix:
|
||||
|
||||
Create or edit the file at:
|
||||
|
||||
```
|
||||
C:\Users\LENOVO\.wslconfig
|
||||
```
|
||||
|
||||
Add:
|
||||
|
||||
```
|
||||
[wsl2]
|
||||
networkingMode=None
|
||||
```
|
||||
|
||||
Then restart Docker Desktop.
|
||||
|
||||
---
|
||||
|
||||
## Docker Compose
|
||||
|
||||
Docker Compose lets you define and run multi-container apps using YAML.
|
||||
|
||||
### Example:
|
||||
|
||||
```yaml
|
||||
version: '3'
|
||||
services:
|
||||
app:
|
||||
image: my-app
|
||||
ports:
|
||||
- "3000:3000"
|
||||
environment:
|
||||
- NODE_ENV=production
|
||||
|
||||
mongodb:
|
||||
image: mongo
|
||||
ports:
|
||||
- "27017:27017"
|
||||
environment:
|
||||
- MONGO_INITDB_ROOT_USERNAME=admin
|
||||
- MONGO_INITDB_ROOT_PASSWORD=secret
|
||||
```
|
||||
|
||||
### Commands:
|
||||
|
||||
```bash
|
||||
docker-compose -f docker-compose.yml up
|
||||
docker-compose down
|
||||
```
|
||||
|
||||
- All services run in the same default Docker network.
|
||||
- Indentation in YAML is **critical**.
|
||||
|
||||
---
|
||||
|
||||
## `Dockerfile`
|
||||
|
||||
A `Dockerfile` is a script used to build Docker images.
|
||||
|
||||
### Example:
|
||||
|
||||
```Dockerfile
|
||||
FROM node:18
|
||||
|
||||
ENV NODE_ENV=production
|
||||
|
||||
# Inside container
|
||||
RUN mkdir -p /home/app
|
||||
|
||||
# Copy from host into container
|
||||
COPY ./home/app /home/app
|
||||
|
||||
WORKDIR /home/app
|
||||
|
||||
CMD ["node", "server.js"]
|
||||
```
|
||||
|
||||
Build with:
|
||||
|
||||
```bash
|
||||
docker build -t my-node-app .
|
||||
```
|
||||
|
||||
Then run it with:
|
||||
|
||||
```bash
|
||||
docker run -p 3000:3000 my-node-app
|
||||
```
|
||||
|
||||
@@ -1,186 +0,0 @@
|
||||
|
||||
|
||||
# Docker?
|
||||
|
||||
# Container?
|
||||
A way to package application with everything they need inside the package. that package is portable and can be shared and moved around.
|
||||
|
||||
The place of containers? they live in a **container repository**
|
||||
Some companies have private repositories
|
||||
There is a public repository for Docker: DockerHub
|
||||
|
||||
# How Containers Helped?
|
||||
Before container, people had to install all the things stuff
|
||||
Problems: Installation process is different on different OS. There are many steps for installing applications and this is prone to errors
|
||||
With Container: You do not have to install anything
|
||||
The container is in its own isolated environment.
|
||||
everything needed is packaged with all needed configuration.
|
||||
the download is just one command
|
||||
You can also some 2 different versions of the same app
|
||||
|
||||
|
||||
## Deployment:
|
||||
before:
|
||||
people would produce artificats with a set of instructions on how to do it.
|
||||
you would have a jar file or stuff
|
||||
development would give it to operators and all
|
||||
problem: you need to install everything...
|
||||
misunderstandings...
|
||||
after:
|
||||
No envioronmental configuration needed on server - except docker runtime
|
||||
|
||||
# Container:
|
||||
## Layers of images
|
||||
## Mostly Linux Base Image, because small in size (alpine)
|
||||
## Application image on top
|
||||
|
||||
# Docker Image vs Container
|
||||
Image: actual package. artifact that can be moved around
|
||||
Container: actually start the application. container envioronment is created
|
||||
|
||||
# Docker vs VM?
|
||||
|
||||
Docker works on OS level.
|
||||
|
||||
OS Layers: OS Kernel -> Application
|
||||
|
||||
Docker: virtualizes the application layer
|
||||
VM: virtualizes OS Kernel as well
|
||||
|
||||
Size of docker images are much smaller
|
||||
Speed of docker is much faster
|
||||
Compatibility: VM of any OS can be run on any other... there should be kernel compatibility.
|
||||
Need to use docker DESKTOP
|
||||
|
||||
# Docker Installation
|
||||
# Image vs Container
|
||||
|
||||
|
||||
Container is a running environment for IMAGE
|
||||
Container: File System, Environment configs, application image(postgres, radis, mongo)
|
||||
Container has a port binded.
|
||||
The file system in Container is virtual
|
||||
|
||||
All the artificts in docker hub are images, not containers
|
||||
|
||||
|
||||
Running an image will create a container I guess.
|
||||
|
||||
|
||||
docker ps -> list containers
|
||||
|
||||
docker run -> start new container with a command
|
||||
|
||||
docker stop CONTAINER_ID
|
||||
docker start CONTAINER_ID
|
||||
|
||||
docker ps -a => will show all, running or not running
|
||||
|
||||
|
||||
How to use different versions of stuff
|
||||
Run the containers with different versions
|
||||
|
||||
# Container Port vs Host port:
|
||||
|
||||
# Binding between laptop and container port:
|
||||
during the run command =>
|
||||
docker run -pHOSTPORT:CONTAINERPORT
|
||||
|
||||
|
||||
# Debugging Containers
|
||||
|
||||
docker run -d -p
|
||||
|
||||
docker logs CONTAINER_ID/NAME
|
||||
|
||||
you can give names to containers, or some random thing is given
|
||||
|
||||
|
||||
docker exec -it [CONTAINERID/NAME] /bin/bash
|
||||
(interactive terminal)
|
||||
use exit to exit
|
||||
|
||||
|
||||
# Demo
|
||||
|
||||
## Docker Network
|
||||
MongoDb - MongoExpress
|
||||
Those packages in the same isolated docker network can connect directly to each othe or something
|
||||
|
||||
|
||||
docker network ls
|
||||
|
||||
## Creating docker network
|
||||
docker network create mongo-network
|
||||
docker run -p ... : .... -d [userpass... view documentation on docker hub] -net mongo-network
|
||||
mongo
|
||||
|
||||
## Error:
|
||||
```
|
||||
deploying WSL2 distributions
|
||||
ensuring main distro is deployed: deploying "docker-desktop": importing WSL distro "Failed to configure network (networkingMode Nat). To disable networking, set `wsl2.networkingMode=None` in C:\\Users\\LENOVO\\.wslconfig\r\nError code: Wsl/Service/RegisterDistro/CreateVm/ConfigureNetworking/HNS/0xffffffff\r\n" output="docker-desktop": exit code: 4294967295: running WSL command wsl.exe C:\WINDOWS\System32\wsl.exe --import docker-desktop <HOME>\AppData\Local\Docker\wsl\main C:\Program Files\Docker\Docker\resources\wsl\wsl-bootstrap.tar --version 2: Failed to configure network (networkingMode Nat). To disable networking, set `wsl2.networkingMode=None` in <HOME>\.wslconfig
|
||||
Error code: Wsl/Service/RegisterDistro/CreateVm/ConfigureNetworking/HNS/0xffffffff
|
||||
: exit status 0xffffffff
|
||||
checking if isocache exists: CreateFile \\wsl$\docker-desktop-data\isocache\: The network name cannot be found.
|
||||
```
|
||||
|
||||
|
||||
save
|
||||
|
||||
```
|
||||
[wsl2]
|
||||
networkingMode=None
|
||||
```
|
||||
in Users/Lenovo as .wslconfig
|
||||
|
||||
|
||||
# Compose
|
||||
```
|
||||
version:'3'
|
||||
services:
|
||||
name:
|
||||
image:[image]
|
||||
ports:
|
||||
- HOST:CONTAINER
|
||||
environment:
|
||||
- SOMETHING=value
|
||||
mongodb:
|
||||
image:mongo
|
||||
ports:
|
||||
- 27017:27017
|
||||
environment:
|
||||
- MONGO..._USERNAME=admin
|
||||
|
||||
```
|
||||
Docker compose handles the same network thing
|
||||
Indentation is important
|
||||
|
||||
|
||||
```
|
||||
docker-compose -f ... up/down
|
||||
```
|
||||
|
||||
|
||||
# Dockerfile
|
||||
dockerfile -> image
|
||||
|
||||
copy artifacts (jar, war, bundle.js)
|
||||
|
||||
blueprint for building images
|
||||
|
||||
```
|
||||
FROM node
|
||||
(Alternative to the one in the docker compose file... that one is better)
|
||||
ENV ...=...
|
||||
...=...
|
||||
|
||||
(Directory is built INSIDE of a container)
|
||||
RUN mkdir -p /home/app
|
||||
|
||||
(This one is on HOST)
|
||||
COPY ./home/app
|
||||
|
||||
CMD ["node", "server.js"]
|
||||
```
|
||||
|
||||
The name MUST be: Dockerfile
|
||||
Reference in New Issue
Block a user