Docker
Source: Dev.to
What is Docker
A Docker container is a running instance of a Docker image. It includes the application, libraries, and runtime, isolated from the host system.
Why use Docker
- Consistency – same environment on your laptop, CI, and production.
- Portability – images run anywhere the Docker engine runs.
- Efficiency – containers share the host kernel, start fast and use less memory than VMs.
- Simplifies CI/CD & microservices – build, ship, and scale small independent services easily.
Core concepts
- Image – immutable blueprint (read‑only).
- Container – running instance of an image (writable layer on top).
- Registry – storage for images (e.g., Docker Hub).
- Dockerfile – text file with build instructions for an image.
- Volume / Bind mount – ways to persist or share data outside a container.
- Network drivers – bridge (default), host, none, overlay (for multi‑host).
- Layer caching – build reuses unchanged layers to speed up builds.
Minimal Dockerfile — what each line means
FROM openjdk:17-jdk-slim # base image with Java runtime
WORKDIR /app # set working directory inside the image
COPY . /app # copy files from host into the image
EXPOSE 8080 # documents a port (does not publish it)
ENTRYPOINT ["java","-jar","app.jar"] # executable used when container starts
- FROM – specifies the base image.
- WORKDIR – directory where subsequent commands run.
- COPY – copies files from the host into the image.
- EXPOSE – declares the port the container listens on (for documentation).
- ENTRYPOINT – defines the command that runs when the container starts.
Tip: add a .dockerignore to exclude build artifacts you don’t want in the image.
Essential commands (clean cheat‑sheet)
General info
docker --version # show Docker client version
docker info # daemon summary (containers, images, storage driver)
Build images
docker build -t myapp:1.0 . # build image from Dockerfile in current dir
docker build --no-cache -t myapp:1.0 . # rebuild without cache
Run & lifecycle (create, start, stop, remove)
docker run --name web -d -p 8080:80 nginx:latest
# -d (detached), -p hostPort:containerPort, --name containerName
docker run -it --rm ubuntu bash
# -it interactive, --rm remove on exit
docker ps # list running containers
docker ps -a # list all containers (including stopped)
docker stop # graceful stop
docker start # start stopped container
docker restart # restart
docker rm # remove container
Images
docker pull nginx:latest # download image from registry
Inspect, exec & logs
docker logs -f # follow logs
docker exec -it bash # open a shell inside a running container
docker inspect # detailed low‑level information
Volumes & mounts (persist data)
docker volume create myvol
docker run -v myvol:/data ... # named volume
docker run -v /host/path:/data ... # bind mount
docker volume ls
docker volume rm myvol
- Named volumes (managed by Docker) are best for databases and portability.
- Bind mounts map a host folder directly into the container (useful for development).
Networking (quick)
docker network ls
docker network create mynet
docker run --network mynet ...
docker network inspect mynet
Cleanup (free disk space)
docker system df
docker image prune # remove dangling images
docker container prune # remove stopped containers
docker system prune -a # remove unused images/containers/networks (use with care)
Compose (multi‑container apps)
docker compose up -d
docker compose down
docker compose ps
docker compose logs -f
Monitoring & debug
docker stats # live resource usage per container