12:00 pm - 1:30 pm 1st june, 2024
A Linux® container is a set of 1 or more processes that are isolated from the rest of the system.
Linux kernel feature that partition kernel resources such that one set of processes sees one set of resources, while another set of processes sees a different set of resources.
It allows you to allocate resources such as CPU time, system memory, network bandwidth, etc, so that an activity per service instance can be ran and constrained by cgroups on the system.
# CLI to List system namespaces.
lsns --help
# list all ns
lsns
Docker is an open platform for developing, shipping, and running containerized applications.
https://docs.docker.com/engine/install/ubuntu/
# 1. Set up Docker's apt repository.
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
# 2. Install the Docker packages.
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# Docker Service Start
sudo service docker start
# Verify that the Docker Engine installation is successful by running the hello-world image.
sudo docker run hello-world
# download an docker image, eg nginx
docker pull nginx
# run image, use -d to run in background
docker run --name exercise2 -p 8080:80 nginx
# check in browser
http://localhost:8080/
# check docker running process
docker ps
# stop
docker stop exercise2
# restart
docker start exercise2
# check logs
docker logs exercise2
# Get PID for docker container
docker inspect --format '{{.State.Pid}}' exercise2
# List docker container ns
sudo ls -la /proc/7059/ns
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
https://docs.docker.com/reference/dockerfile/
# Build a docker container
docker build -t node-api -no-cache .
# Run a docker container
docker run -p 3000:3000 node-api
# Remove image
docker rmi node-api --force
# Clear cache!
docker system prune -a
# Dockerfile
FROM node:22-alpine
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
WORKDIR /home/node/app
COPY package*.json ./
USER node
RUN npm install
COPY --chown=node:node . .
EXPOSE 3000
ENTRYPOINT [ "node", "index.js" ]
Docker Compose is a tool for defining and running multi-container applications.
# compose.yaml
services:
nginx:
image: "nginx"
ports:
- "80:80"
node:
image: "node"
redis:
image: "redis:alpine"