Docker
More actions
Docker keeps projects contained and bundles all python/node versions, dependencies, and OS libraries into one image! This is best for web apps, projects with databases, etc. If you're hosting a Discord/Slack bot or a simple script, you might want to consider using PM2 or Systemd instead.
1. Installation
Run this command to update and install docker and its dependencies on your container.
Debian
apt update
apt install ca-certificates curl gnupg -y
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian bookworm stable" | tee /etc/apt/sources.list.d/docker.list
apt update
apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
docker run hello-worldUbuntu
apt update
apt install ca-certificates curl gnupg -y
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu noble stable" | tee /etc/apt/sources.list.d/docker.list
apt update
apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
docker run hello-worldIf you see a "Hello from Docker!" message, you're good.
2. Project Setup
Create a docker-compose.yml . You can use a dockerfile, but the docker compose file is simpler for beginners and scales more easily! Below is a template.
services:
web:
image: <image> # base image for your language, see table below
container_name: myapp
restart: unless-stopped
working_dir: /app
volumes:
- .:/app
command: sh -c "<install command> && <run command>"
env_file:
- .env
Swap <image> and <install command> && <run command> for your language:
| Language | image
|
command
|
|---|---|---|
| Python | python:3.12-slim
|
pip install -r requirements.txt && python3 app.py
|
| Node.js | node:20-slim
|
npm install && node index.js
|
| Bun | oven/bun:slim
|
bun install && bun run index.ts
|
| Go | golang:1.22-alpine
|
go mod download && go run main.go
|
| Rust | rust:slim
|
cargo build --release && ./target/release/app
|
If your language isn't listed, just search it up! Docker has hundreds of compatible images!
Then, setup your environment variables and secrets in the same directory as the docker-compose.yml, and add .env to .gitignore so you don't accidentally leak them to GitHub.
Finally, run your code!
docker compose up -dCheck to make sure it's running:
docker compose psWhen you edit your code, you can easily restart your docker container!
docker compose restart web3. Helpful Commands
| Command | What it does |
|---|---|
docker compose ps
|
List your project's containers |
docker compose logs -f <service>
|
Live-tail a service's logs |
docker compose restart <service>
|
Restart one service |
docker compose down
|
Stop everything |
docker exec -it <name> bash
|
Get a shell inside a running container |
docker system prune -af
|
Clean up unused images/cache |