An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# Docker and Kubernetes

## Containers

![containers](./01-containers/docker%20containers.png)

### How?

![under the hood](./01-containers/under%20the%20hood%20docker.png)

### Why?

![why](./01-containers/why%20to%20use.png)

### Working

![working](./01-containers/docker%20working.png)

### Commands

![commands](./01-containers/docker%20commands.png)

## Essentials

### Images

![images](./02-essentials/docker%20images.png)

### Containers

![containers](./02-essentials/docker%20containers.png)

### Dockerfile

An overall view of working with containers is as follows:
![overall view](./02-essentials/overall%20view.png)

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 .
```

![docker file logs](./02-essentials/docker%20file%20logs.png)

without caching it would look like following:

![docker file logs no cache](./02-essentials/docker%20file%20logs%20-%20no%20cache.png)

### 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

![docker file logs rebuild](./02-essentials/docker%20file%20logs%20optimised%20for%20rebuild.png)

Output would be like following for the react app
![react output](./02-essentials/react%20output.png)

## 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 .
```

![build output](./02-essentials/docker-express-app/build%20process.png)

Run a container with the image with environment variables passed in-line

```bash
docker run -p 4000:3000 -e PORT=3000 --rm express-app
```

![output of express app](./02-essentials/docker-express-app/output.png)

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 .
```

![multi-stage build output](./02-essentials/docker-express-multi-stage/build%20output.png)

## 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
```

![basic](./03-networking/basic%20google%20ping.png)

`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.

![container networking](./03-networking/containers%20networking.png)

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
```

![network created](./03-networking/create%20network.png)

Create a new container in the network

```bash
docker run --network my-bridge --name container1 -d nginx
```

![container created](./03-networking/container%20with%20network.png)

Create another container in the same network on alpine image

```bash
docker run --network my-bridge --name container2 -d alpine sleep 3600
```

![alpine container created](./03-networking/two%20containers%20in%20same%20network.png)

Ping the nginx container from the alpine container

```bash
docker exec -it container2 ping container1
```

![ping output](./03-networking/ping%20container.png)

## 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
```

![compose output](./04-compose/docker%20compose%20-%201.png)

![compose output 2](./04-compose/docker%20compose%20-%202.png)

![compose output 3](./04-compose/docker%20compose%20-%203.png)

![compose output 4](./04-compose/docker%20compose%20-%204.png)

![compose output 5](./04-compose/docker%20compose%20-%205.png)