Skip to content

n4sunday/docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🐳 Docker

πŸš€ Basic Command

Create and Running a Container from as Image

docker run <image name>

List all running containers

docker ps

Create a Container

docker create <image name>

Start a Container

docker start <container id>

Stop All

docker system prune

Get logs from a container

docker logs <container id>

Stop container

docker stop <container id>

Kill container

docker kill <container id>

Execute an additional command in a container

docker exec -it <container id> <command>

Command

  • bash
  • powershell
  • zsh
  • sh

πŸš€ Building and Custom Image

πŸ“ redis-image

Basic Build

πŸ“„ Dockerfile

# Use an exising docker image as a base
FROM alpine

# Download and install a dependency
RUN apk add --update redis

# Tell the image what to do when it starts
# as a container
CMD ["redis-server"]
docker build .

docker run <image id>
Tagging an Image
docker build -t n4sunday/redis:latest .
docker build -t <your docker id>/<repo | project name>:<version> .

πŸš€ Making Real Project with Docker

πŸ“ real-project

Basic Node Project

πŸ—ƒοΈ Structure

project
  β”œβ”€β”€ index.js
  β”œβ”€β”€ package.json
  └── Dockerfile

πŸ“„ index.js

const express = require("express");

const app = express();

app.get("/", (req, res) => {
  res.send("Hello World");
});

app.listen(3000, () => {
  console.log("Listen on port 3000");
});

πŸ“„ package.json

{
  "name": "real-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "express": "*"
  },
  "author": "",
  "license": "ISC"
}

πŸ“„ Dockerfile

# Specify a base image
FROM node:alpine

# Install some depenendencies
RUN npm install

# Default command
CMD ["npm","start"]
Command Description Description
COPY ./ ./
Path to folder to copy on your machine relative to build context Place to copy stuff to inside the container

update πŸ“„ Dockerfile

# Specify a base image
FROM node:alpine

# Install some depenendencies
COPY ./ ./
RUN npm install

# Default command
CMD ["npm","start"]

πŸ”₯ Container Port Mapping

docker run -p <incoming request port>:<port inside container> <image id>

docker run -p 3000:3000 <image id>

Working Directory

πŸ“„ Dockerfile

# Specify a base image
FROM node:alpine

WORKDIR /usr/app

# Install some depenendencies
COPY ./ ./
RUN npm install

# Default command
CMD ["npm","start"]

πŸ“„ Dockerfile

FROM node:alpine

WORKDIR /usr/app

COPY ./package.json ./
RUN npm install
# ----- cache -----
COPY ./ ./

CMD ["npm","start"]

πŸš€ Docker Compose with Multiple Local Container

πŸ“ multiple-container

  • Used to start up multiple Docker containers
  • Automates some of the long-winded arguments we were padding to 'docker run'

πŸ—ƒοΈ Structure

project
  β”œβ”€β”€ index.js
  β”œβ”€β”€ package.json
  β”œβ”€β”€ Dockerfile
  └── docker-compose.yml

πŸ“„ index.js

const express = require("express");
const redis = require("redis");

const app = express();
const client = redis.createClient({
  host: "redis-server",
  port: 6379,
});
client.set("visits", 0);

app.get("/", (req, res) => {
  client.get("visits", (err, visits) => {
    res.send("Number of visits is " + visits);
    client.set("visits", +visits + 1);
  });
});

app.listen(3000, () => {
  console.log("Listen on port 3000");
});

πŸ“„ Dockerfile

FROM node:alpine

WORKDIR /usr/app

COPY ./package.json ./
RUN npm install
COPY ./ ./

CMD ["npm","start"]

πŸ“„ package.json

{
  "name": "multiple-container",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "express": "*",
    "redis": "2.8.0"
  },
  "author": "",
  "license": "ISC"
}

πŸ“„ docker-compose.yml

version: "3"
services:
  redis-server:
    image: "redis"
  node-app:
    build: .
    ports:
      - "3000:3000"

πŸ”₯ Commands

Launch docker compose

docker-compose up

Launch in background

docker-compose up -d
docker-compose up --build

Stop Containers

docker-compose down

Automatic Container Restarts

Status Codes

Status Codes Description
0 We exited and everything is OK
1, 2, 3, etc We exited because something went wrong!

Restart Policies default no

Status Codes Description
no Never attempt to restart this . container if it stops or crashes
always If this container stops for any reason always attempt to restart it
on-failure Only restart if the container stops with an error code
unless-stopped Always restart unless we (the developers) forcibly stop it

πŸ“„ docker-compose.yml

version: "3"
services:
  redis-server:
    image: "redis"
  node-app:
    restart: always
    build: .
    ports:
      - "3000:3000"

πŸš€ Creating a Production Grade Workflow

πŸ“ production-grade-workflow

Volumes

use docker

docker run -p 3000:3000 -v /app/node_modules -v $(pwd):/app <image_id>

use docker compose

πŸ“„ Dockerfile.dev

FROM node:alpine

WORKDIR /app

COPY package.json .
RUN npm install

COPY . .

CMD ["npm","run","dev"]

πŸ“„ docker-compose.yml

version: "3"
services:
  node-app:
    build:
      context: .
      dockerfile: Dockerfile.dev
    ports:
      - "3000:3000"
    volumes:
      - /app/node_modules
      - .:/app

vite project config usePolling: true

πŸ“„ vite.config.js

import { defineConfig } from "vite";
import reactRefresh from "@vitejs/plugin-react-refresh";

export default defineConfig({
  server: {
    host: "0.0.0.0",
    port: 3000,
    watch: {
      usePolling: true,
    },
  },
  plugins: [reactRefresh()],
});

πŸš€ Docker Compose for Running Tests

πŸ“„ docker-compose.yml

version: "3"
services:
  node-app:
    build:
      context: .
      dockerfile: Dockerfile.dev
    ports:
      - "3000:3000"
    volumes:
      - /app/node_modules
      - .:/app
  tests:
    build:
      context: .
      dockerfile: Dockerfile.dev
    volumes:
      - /app/node_modules
      - .:/app
    command: ["npm", "run", "test"]

πŸš€ Implement Muti-Step Builds

πŸ“„ Dockerfile

# STEP 1
FROM node:alpine as builder
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
RUN npm run build

# STEP 2
FROM nginx
COPY --from=builder /app/dist /usr/share/nginx/html

Run nginx

docker run -p 3000:80 <image_id>

πŸš€ Travis

πŸ“„ .travis.yml

sudo: required
services:
  - docker

before_install:
  - docker build -t n4sunday/docker-react -f Dockerfile.dev .

script:
  - docker run n4sunday/docker-react npm run test -- --coverage

πŸš€ Kubernetes (K8S)

πŸ”₯ What is Kubernetes?

System for running many different containers over multiple different machines

πŸ”₯ Why use Kubernetes?

When you need to run many different containers with different images

install kubectl install minikube

minikube start
minikube status

πŸš€ NGINX

πŸ“ production-grade-workflow

πŸ”₯ Web Server

πŸ“„ Dockerfile

# STEP 1
FROM node:alpine as builder
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
RUN npm run build

# STEP 2
FROM nginx
COPY --from=builder /app/dist /usr/share/nginx/html

πŸ“„ docker-compose.yml

version: "3"
services:
  front-app:
    build: .
    ports:
      - "3000:80"

πŸ”₯ Reverse proxy

πŸ“„ Dockerfile

FROM node:alpine as builder
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
RUN npm run build

RUN npm install -g serve
CMD ["serve", "-p", "5000", "-s", "./dist"]

πŸ“„ docker-compose.yml

version: "3"
services:
  nginx:
    image: nginx
    container_name: nginx
    restart: always
    ports:
      - "80:80"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
  front-end:
    build: .
    ports:
      - "3000:5000"

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published