What is Docker?
Docker packages apps and dependencies into containers so they run the same everywhere. You build an image once and run it on any host with the Docker runtime.
Docker packages apps and dependencies into containers so they run the same everywhere. You build an image once and run it on any host with the Docker runtime.
Install Docker Engine on Ubuntu and verify the service.
sudo apt update && sudo apt install -y docker.io
sudo systemctl status docker # check Docker status
sudo systemctl enable --now docker # enable and start
docker --version # verify binary
Pull images, run containers, list and manage them.
docker pull nginx
docker run -d -p 8080:80 nginx # start in background
docker ps -a # list all containers
docker stop <container_id> # stop container
docker rm <container_id> # remove container
Define a custom image with a Dockerfile, then build and run it.
FROM ubuntu:22.04
COPY . /app
RUN apt update && apt install -y python3
CMD ["python3", "/app/app.py"]
docker build -t myapp . # build image
docker run myapp # run the image
Define and run multi-container applications using a YAML file.
version: "3"
services:
web:
image: nginx
ports:
- "8080:80"
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: example
docker compose up -d # start services
docker compose logs -f # follow logs
docker compose down # stop and remove
Persist data and connect containers across networks.
docker volume create mydata
docker run -d -v mydata:/data nginx # mount named volume
docker network create mynet
docker run -d --network mynet --name web nginx
Remove unused items to free disk space.
# remove stopped containers
docker container prune
# remove dangling images
docker image prune
# everything unused (be careful)
docker system prune -a