https://github.com/anirudhuuu/docker-kubernetes
docker & kubernetes: build, deploy & scale on aws
https://github.com/anirudhuuu/docker-kubernetes
devops docker kubernetes
Last synced: 2 months ago
JSON representation
docker & kubernetes: build, deploy & scale on aws
- Host: GitHub
- URL: https://github.com/anirudhuuu/docker-kubernetes
- Owner: anirudhuuu
- Created: 2025-05-23T06:12:14.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-11T06:28:04.000Z (about 1 year ago)
- Last Synced: 2026-03-28T19:34:55.741Z (3 months ago)
- Topics: devops, docker, kubernetes
- Language: JavaScript
- Homepage:
- Size: 11.3 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Docker and Kubernetes
## Containers

### How?

### Why?

### Working

### Commands

## Essentials
### Images

### Containers

### Dockerfile
An overall view of working with containers is as follows:

Plan of action will be as follows:
1. Create a fresh react app
2. Create a docker image
3. Spin up a container with that image
4. Perform updates in the application
5. Make a fresh image of the updated application
6. Sipn up a new container with the updated image
### Command to build an image
```bash
docker build -t react-docker .
```

without caching it would look like following:

### Command to run the image
The format for porting is `:`
```bash
docker run -p 5173:5173 react-docker
```
Since the vite application server needs the host to be defined we have set in the `vite.config.js` file the following:
```javascript
export default defineConfig({
server: {
host: "0.0.0.0",
port: 5173,
},
});
```
### Command to rebuild the image
after making changes to the application we need to rebuild the image for the changes to take effect.
For versioning we can use
```bash
docker build -t react-docker:2 .
```
To rebuild the exact same image we can use
```bash
docker build -t react-docker .
```
Optimised logs for the docker rebuild of image

Output would be like following for the react app

## Common Commands
Runs a container based on the image
```bash
docker run
```
Runs a container based on the image in detached mode
```bash
docker run -d
```
Provide a name to the container
```bash
docker run --name
```
Run on a specific port
```bash
docker run -p :
```
Load present working directory into the container
```bash
docker run -v $(pwd):/app
```
Execute in an interactive mode, this will allow you to run commands inside the container using `/bin/bash` or `sh` shell
```bash
docker run -it /bin/bash
```
```bash
docker run -it sh
```
## More Commands
List all the running containers
```bash
docker ps
```
List all the containers (including stopped ones)
```bash
docker ps -a
```
List logs of a container
```bash
docker logs
```
Get attached to a running container, just to get inside the container
```bash
docker attach
```
Stop a running container
```bash
docker stop
```
Container is stuck or processing something, force stop it
```bash
docker kill
```
Stop all the active running containers
```bash
docker stop $(docker ps -q)
```
Restart a container
```bash
docker restart
```
Start a stopped container
```bash
docker start
```
Remove a container
```bash
docker rm
```
Container is busy, force remove it
```bash
docker rm -f
```
Delete all the stopped containers
```bash
docker prune
```
Copy a file from the container to the host
```bash
docker cp :/path/to/file /path/on/host
```
## Dockerise an Express App
Build an image for an express app
```bash
docker build -t express-app .
```

Run a container with the image with environment variables passed in-line
```bash
docker run -p 4000:3000 -e PORT=3000 --rm express-app
```

Run a container with the image with environment variables passed in a file
```bash
docker run -p 4000:3000 --env-file .env --rm express-app
```
## Multi-Stage Docker build
```bash
docker build -t express-mutli-stage .
```

## Networking
Basic networking in docker allows containers to communicate with each other and the outside world.
```bash
docker run --rm -it busybox sh
hostname
ping google.com
ifconfig
exit
```

`eth0` is the default network interface in docker containers. eth stands for Ethernet network interface. `veth` stands for virtual Ethernet network interface. `docker0` is the master interface for the docker bridge network that talks to the host machine.

To list all the networks in docker, you can use the following command:
```bash
docker network ls
```
| NETWORK ID | NAME | DRIVER | SCOPE |
| ------------ | ------ | ------ | ----- |
| 287fceb8c8d1 | bridge | bridge | local |
| 30a4b9d415b7 | host | host | local |
| 66118539c033 | none | null | local |
Read more regarding network drivers in docker [here](https://docs.docker.com/engine/network/drivers/).
| NETWORK TYPE | DESCRIPTION |
| ---------------- | -------------------------------------------------------------------------------- |
| bridge (default) | containers on the same host can communicate, external access needs port mapping. |
| host | The container shares the host's networking namespace (no isolation) |
| none | No network connectivity (completely isolated) |
| overlay | Enales multi-host networking for Swarm services |
| macvlan | Assigns a real MAC address to the container (acts like a physical device) |
## Networking Commands
List all the networks
```bash
docker network ls
```
Inspect a network
```bash
docker network inspect
```
Create a new network
```bash
docker network create
```
Run a container in a specific network
```bash
docker run --network
```
Attach a running container to a network
```bash
docker network connect
```
Disconnect a container from a network
```bash
docker network disconnect
```
Remove a network
```bash
docker network rm
```
## Docker Networking Hands-on
List all the networks
```bash
docker network ls
```
Create a new network
```bash
docker network create my-bridge
```

Create a new container in the network
```bash
docker run --network my-bridge --name container1 -d nginx
```

Create another container in the same network on alpine image
```bash
docker run --network my-bridge --name container2 -d alpine sleep 3600
```

Ping the nginx container from the alpine container
```bash
docker exec -it container2 ping container1
```

## Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define services, networks, and volumes in a single YAML file.
```yml
# Full-stack application with React frontend, Node.js backend, and PostgreSQL database
# Uses custom network for inter-service communication
# React frontend service - serves UI on port 3000
# Waits for backend to be ready before starting
# Node.js backend API service - runs on port 5001
# Connects to PostgreSQL database using environment variables
# Waits for database to be ready before starting
# PostgreSQL database service - uses Alpine Linux for smaller image size
# Automatically restarts if container stops
# Data persisted using named volume 'pgdata'
# Named volume for PostgreSQL data persistence
# Custom bridge network for service communication
# Allows services to communicate using service names as hostnames
version: "3.8"
services:
frontend:
build: ./frontend
ports:
- "3000:80"
depends_on:
- backend
networks:
- my-custom-network
backend:
build: ./backend
ports:
- "5001:5000"
environment:
DB_HOST: database
DB_USER: postgres
DB_PASS: password
DB_NAME: postgres
depends_on:
- database
networks:
- my-custom-network
database:
image: postgres:16-alpine
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: postgres
volumes:
- pgdata:/var/lib/postgresql/data
networks:
- my-custom-network
volumes:
pgdata:
networks:
my-custom-network:
driver: bridge
```




