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
π redis-image
π 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>
docker build -t n4sunday/redis:latest .
docker build -t <your docker id>/<repo | project name>:<version> .
π real-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"]
docker run -p <incoming request port>
:<port inside container>
<image id>
docker run -p 3000:3000 <image id>
π 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"]
π 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"
Launch docker compose
docker-compose up
Launch in background
docker-compose up -d
docker-compose up --build
Stop Containers
docker-compose down
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"
π production-grade-workflow
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.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"]
π 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.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
System for running many different containers over multiple different machines
When you need to run many different containers with different images
install kubectl
install minikube
minikube start
minikube status
π production-grade-workflow
π 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"
π 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"