Batch 4: Cloud Ansible AWS & Azure Docker & Podman Apache & NGINX ← Full TOC

← Docker Index

🐳 Docker Basics — docker run, build, push, pull, ps, images, volumes

Docker packages applications with all dependencies into portable containers. Every container runs from an image. Images are built from Dockerfiles and stored in registries. Podman is a daemonless Docker-compatible alternative.

📋 Reference

ItemSyntaxDescription
docker rundocker run [OPTIONS] IMAGE [COMMAND]Run a container from an image
-ddocker run -d nginxDetached mode — run in background
-pdocker run -p 8080:80 nginxPort mapping host:container
-vdocker run -v /host/path:/container/pathBind mount volume
-edocker run -e ENV_VAR=valueSet environment variable
--namedocker run --name myapp nginxAssign container name
--rmdocker run --rm ubuntu echo helloAuto-remove when container exits
-itdocker run -it ubuntu bashInteractive terminal
--networkdocker run --network mynet myappConnect to network
docker psdocker ps / docker ps -aList running / all containers
docker imagesdocker imagesList local images
docker pulldocker pull ubuntu:22.04Pull image from registry
docker pushdocker push myregistry/myimage:tagPush image to registry
docker builddocker build -t myimage:tag .Build image from Dockerfile in current dir
docker execdocker exec -it mycontainer bashExecute command in running container
docker logsdocker logs -f mycontainerStream container logs
docker stopdocker stop mycontainerGracefully stop container (SIGTERM)
docker killdocker kill mycontainerForce stop container (SIGKILL)
docker rmdocker rm mycontainerRemove stopped container
docker rmidocker rmi myimage:tagRemove image
docker volumedocker volume create / ls / rm / inspectManage named volumes
docker networkdocker network create / ls / rmManage networks
docker inspectdocker inspect mycontainerDetailed JSON info about container
docker statsdocker statsLive resource usage for containers
docker cpdocker cp mycontainer:/path/file ./localCopy files between container and host
docker commitdocker commit mycontainer myimage:newCreate image from container (prefer Dockerfile)
docker tagdocker tag myimage:latest myimage:v1.0Tag image with new name
docker logindocker login registry.example.comAuthenticate to registry
docker system prunedocker system prune -afRemove all unused containers, images, networks
podmanpodman run/build/push/pull/psPodman: drop-in Docker replacement, daemonless, rootless

💡 Example

# Docker essential commands

# ── Pull and run ──────────────────────────────────────────────
docker pull ubuntu:22.04
docker run --rm ubuntu:22.04 echo "Hello from container"
docker run -it ubuntu:22.04 bash   # interactive shell

# Run web server
docker run -d --name nginx-test -p 8080:80 nginx:alpine
curl http://localhost:8080          # test it
docker logs nginx-test              # check logs
docker stop nginx-test && docker rm nginx-test

# ── Build image ───────────────────────────────────────────────
mkdir myapp && cd myapp
cat > Dockerfile << 'EOF'
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8080
USER nobody
CMD ["python", "app.py"]
EOF

docker build -t mywebuniversity/api:1.0 .
docker build -t mywebuniversity/api:1.0 --no-cache .  # force rebuild
docker build --build-arg ENV=production -t myapp .

# ── Run your image ─────────────────────────────────────────────
docker run -d \
  --name mywebuniversity-api \
  -p 8080:8080 \
  -e DATABASE_URL=postgresql://db:5432/mydb \
  -e LOG_LEVEL=info \
  --restart unless-stopped \
  mywebuniversity/api:1.0

docker exec -it mywebuniversity-api bash  # debug inside
docker logs -f mywebuniversity-api         # stream logs
docker stats mywebuniversity-api           # resource usage

# ── Volumes ────────────────────────────────────────────────────
docker volume create myapp-data
docker run -v myapp-data:/app/data myapp       # named volume
docker run -v $(pwd)/data:/app/data myapp      # bind mount
docker volume inspect myapp-data
docker volume rm myapp-data

# ── Registry ──────────────────────────────────────────────────
docker tag mywebuniversity/api:1.0 ghcr.io/myorg/api:1.0
docker login ghcr.io -u myuser --password-stdin <<< "$GITHUB_TOKEN"
docker push ghcr.io/myorg/api:1.0
docker pull ghcr.io/myorg/api:1.0

# ── Podman (drop-in replacement) ──────────────────────────────
# All docker commands work with podman:
podman run -d --name nginx -p 8080:80 nginx
podman ps; podman logs nginx; podman stop nginx
# Rootless containers — no daemon required
podman run --rm alpine echo "rootless container!"
# Generate systemd unit for auto-start
podman generate systemd --name nginx --files --new

🏠 Index  |  Dockerfile Best Practices →