Welcome To The Course!
Use these commands to verify your Docker setup:
docker versionShows the client and server (Engine) versions:
docker infoDisplays detailed information about your Docker environment (containers, images, storage, etc.).
By default only root (or users in the docker group) can access the Docker socket. To let a non-root user run Docker:
# Add your user to the docker group
sudo usermod -aG docker <your-user>
# Apply the new group without logging out
sudo newgrp docker
# (Optional) Verify the docker group in /etc/group
cat /etc/group | grep docker- Note: You may need to log out and back in (or reboot) for the group change to take full effect.
Use docker pull to download an image from a registry (default: Docker Hub):
docker pull centos:7When you use docker run with an image name, Docker will:
- Check if the image exists locally.
- If not found, it automatically pulls it from the registry.
docker imagesDisplays all locally available images:
- REPOSITORY – image name
- TAG – version/tag
- IMAGE ID – unique identifier
- SIZE – image size
# Optional: list only image IDs
docker images -qRun a CentOS container in interactive mode:
# Start a shell session
docker run -it centos /bin/bash
# — or —
docker run -it centos
# Why do both commands have the same output? becuase main process of centos is /bin/bash
# Run the top command
docker run -it centos top
# Ping Google DNS
docker run -it centos ping 8.8.8.8-ikeeps STDIN open-tallocates a pseudo-TTY
Inside the container:
- Press
Ctrl + dto exit and stop the container - Press
Ctrl + pqto detach without stopping
You can use ps to find Docker-related processes:
# Show all processes with “docker” in their name
ps aux | grep docker
# Show all processes with “container” in their name
ps aux | grep containerCommon Docker processes you’ll see:
dockerd– the Docker daemoncontainerd– core container runtimedocker-containerd-runc– low-level OCI runtime shimdocker-containerd-shim– keeps containers running after daemon exits
(Optional) To check the Docker service status:
systemctl status dockerEvery container have a lifecycle with a main process and a container stops when its main process ends(see the COMMAND column in docker ps).
# List running containers
docker ps- Show these columns: CONTAINER ID, IMAGE, COMMAND, CREATED, STATUS, PORTS, NAMES
The switches of the docker ps
# List exited containers
docker ps -f status=exited
# List all containers (running + stopped)
docker ps -a
# Show the last 2 containers (by creation time)
docker ps -n 2
# Show the most recently created container
docker ps -l
# Display container sizes (first = filesystem, second = virtual size)
docker ps -s
# Show only container IDs (Useful)
docker ps -q
# Example: remove all exited containers
docker rm $(docker ps -q -f status=exited)-
Remove a stopped container
docker rm <container>
-
Force-remove a running or stopped container
docker rm -f <container>
-
Force-remove all containers
docker rm -f $(docker ps -qa) -
Remove a container and its associated volumes
docker rm -v <container>
Live Restore keeps your containers running and stay up when the Docker daemon stops or restarts.
# See that containers stop when daemon stops
systemctl stop docker
# You can check with `ps aux` that all containers go down
# Prepare daemon config
sudo mkdir -p /etc/docker
sudo echo "
{
"live-restore": true
}
" >> /etc/docker/daemon.json
# Start (or restart) Docker with live-restore enabled
sudo systemctl start docker
# — or —
sudo systemctl restart docker
# Verify setting
docker info | grep "Live Restore"
# Should show: Live Restore Enabled: true- Now you can test and see that containers running and stay up when the Docker daemon stops or restarts.
Limit container resources at runtime:
# Set memory to 400 MiB and memory-swap to 1 GiB (400 MiB RAM + 600 MiB swap)
docker run -itd \
-m 400M \
--memory-swap 1G \
centos
# Pin container to use just CPU 0
docker run -itd \
--cpuset-cpus="0" \
centos
# Limit container writable layer to 5 GiB
docker run -itd \
--storage-opt size=5G \
centos-m/--memory=> defines the maximum RAM.--memory-swap=> sets total RAM+swap (use-1for unlimited swap).--cpuset-cpus=> binds the container to specific CPU cores.--storage-opt size=> limits the container’s filesystem size, but it only works with the overlay2 storage driver on an xfs filesystem with pquota enabled.
You can see and verify your resource limits with docker inspect:
docker inspect <container> \
--format='
Memory Limit : {{.HostConfig.Memory}} bytes
Memory + Swap : {{.HostConfig.MemorySwap}} bytes
CPU Set : {{.HostConfig.CpusetCpus}}
Storage Size : {{index .HostConfig.StorageOpt "size"}}'Use docker stats to view real-time resource usage:
# Live update for all running containers
docker stats
# For a specific container
docker stats <container_name_or_id>
# Single snapshot
docker stats --no-streamRun Heavy CPU Load Test
- Start a CentOS container with CPU and memory limits:
docker run -it --cpuset-cpus="0" -m 300M --name test-centos centos - Inside the container:
- Check CPU and memory and swap info:
cat /sys/fs/cgroup/cpuset.cpus cat /sys/fs/cgroup/memory.max cat /sys/fs/cgroup/memory.swap.max
- Use this command to generate heavy CPU load in the background:
yes > /dev/null &
- Run
topto see processes.
- Check CPU and memory and swap info:
- Exit the container without stop container (
Ctrl + pq). - On the host, run
docker statsto see how “test-centos” used CPU and memory.
Assign or change an image’s name and tag:
# Rename or retag an existing image
docker tag <old_image>:<old_tag> <new_image>:<new_tag>Handle dangling images (<none>:<none>) by using the image ID:
# Tag a dangling image
docker tag <image_id> <new_image>:<new_tag>List dangling images before tagging:
docker images -f “dangling=true”Use docker search to find images on Docker Hub:
docker search <image_name>docker inspect returns JSON details for containers, images, networks, volumes, and more.
# Inspect a container
docker inspect <container_name_or_id>
# Inspect an image
docker inspect <image_name_or_id>Key fields you might check:
State.Status– running, exited, etc.NetworkSettings.IPAddress– container IP.HostConfig.Binds– volume mounts.- ...
# Example: show only the IP address of a container
docker inspect -f '{{.NetworkSettings.IPAddress}}' my-containerUse docker attach to connect your terminal’s stdin, stdout, and stderr to a running container:
docker attach <container_name_or_id>- You’ll see the container’s live console output.
- Keyboard input is sent directly to the container’s main process.
- To detach without stopping the container, press
Ctrl + pq.
docker exec starts a new process inside a running container.
If this process exits, the container keeps running (because the main process is still alive).
# Run a command (example: /bin/bash shell)
docker exec -it mycontainer /bin/bash
# Run a simple command
docker exec -it mycontainer echo "hello"
# Set working directory for the command
docker exec -it -w /var mycontainer ls
# - Lists contents of /var inside the container
# Set an environment variable for the command
docker exec -it -e myvar=123 mycontainer bash
# - Inside bash, 'echo $myvar' will output 123Note: Use docker exec instead of docker attach when you just want to run extra commands without interfering with the main process.
docker cp lets you copy files or directories to or from a container.
# Copy a file from host to container
docker cp myfile mycontainer:/path/in/container
# Copy a file from container to host
docker cp mycontainer:/path/in/container/myfile .- Works for both running and stopped containers.
- Paths can be absolute or relative.
docker stop -t 20 mycontainer- Sends a
SIGTERMsignal, giving the container up to 20 seconds to exit before forcingSIGKILL.
docker wait mycontainer- Blocks until the container stops and return its exit code.
# Attach to stdout/stderr (no stdin)
docker start -a mycontainer
# Attach with stdin, stdout, stderr
docker start -ai mycontainerdocker restart -t 20 mycontainer- Stops (gracefully) and then starts the container again.
docker kill mycontainer- Sends a
SIGKILLsignal (no cleanup time).
stop vs kill:
stop→ Graceful shutdown, gives time to finish work.kill→ Immediate termination. (Forceful)
Note:
- Restarting or starting a container does not remove its files.
- Files are deleted only when the container is removed (
docker rm).
Use -e (or --env) to set environment variables when starting a container:
docker run -e key=value <image_name>You can set multiple variables:
docker run -e VAR1=foo -e VAR2=bar centos envUse docker logs to read a container’s stdout and stderr output.
# Basic logs
docker logs mycontainer
# Follow logs in real-time
docker logs -f mycontainer
# Show timestamps
docker logs -t mycontainer
# Follow logs with timestamps
docker logs -ft mycontainer
# Show logs since a certain time
docker logs --since 2025-08-14T15:00:00 mycontainer
docker logs --since 10m mycontainer # last 10 minutes
# Show logs until a certain time
docker logs --until 2025-08-14T16:00:00 mycontainer
docker logs --until 5m mycontainer # until 5 minutes agodocker events streams real-time events from the Docker daemon (such as container start/stop, image pull, network changes).
# Show all events as they happen
docker events
# Show events since a specific time
docker events --since '2025-08-14T15:00:00'
docker events --since 10m # last 10 minutes
# Filter by event type or object
docker events --filter 'event=stop' # only stop events
docker events --filter 'container=mycontainer' # events for a specific container
docker events --filter 'type=network' # only network-related eventsPause a container: Suspends all processes in the container using cgroups freezer.
docker pause mycontainer- The container remains running but its processes are frozen (no CPU execution).
Unpause a container: Resumes the processes.
docker unpause mycontainerNote: Useful for temporarily halting resource usage without stopping the container.
Use --rm with docker run to automatically delete the container when it stops.
docker run --rm <image_name>- Saves cleanup work for short-lived or test containers.
docker commit saves the current state of a container as a new image.
# Copy a file into the container first
docker cp file1 mycontainer:/path/in/container
# Commit the container to a new image
docker commit mycontainer newimagename:X
# Run a container from the new image
docker run newimagename:Xdocker commit --change='CMD ["/bin/sh"]' mycontainer newimagename:XNote: docker commit is useful for quick snapshots, but for reproducible builds and version control, it’s better to use a Dockerfile and docker build.
Use docker rename to change a container’s name without recreating it.
docker rename <old_name> <new_name>Note: The container ID stays the same — only the name changes.
docker save -o myimage.tar.gz myimage:tag- Exports the image as a
.tar.gz(or.tar) file for backup or transfer.
docker load < myimage.tar.gz- Imports the image back into Docker (used only for images saved with
docker save).
Note: docker save/load works for images.
# Using output redirection
docker export mycontainer > myimage.tar
# Using --output option
docker export --output="myimage.tar" mycontainer- Saves the container’s filesystem (without history, layers, or metadata).
docker import myimage.tar- Creates a new dangling image (
<none>:<none>). - Tag it with a proper name:
docker tag <image_id> newname:newtagSince docker export/import delete metadata (including the default CMD), you must set the command manually:
docker run -it newname:newtag /bin/bashNote: Use docker save/load for full image backups with history. Use docker export/import for a clean snapshot of a container’s filesystem.
Docker uses logging drivers to handle container logs.
Changing the logging driver does not change the output of docker logs — it only changes how logs are stored or where they are sent.
docker run --log-driver <driver_name> centos- none – disables logging entirely.
- local – stores logs in a binary format on the host (efficient).
- json-file – default logging driver; stores logs as JSON text files.
- syslog – sends logs to the system’s syslog daemon.
- journald – sends logs to
systemd-journald. - gelf – sends logs to Graylog Extended Log Format endpoints.
- fluentd – sends logs to a Fluentd collector.
- awslogs – sends logs to Amazon CloudWatch Logs.
- splunk – sends logs to Splunk.
- etwlogs – sends logs to Event Tracing for Windows.
- gcplogs – sends logs to Google Cloud Logging.
Note: Default log storage location for local and json-file:
/var/lib/docker/containers/<container-id>/<container-id>-json.log
docker inspect -f '{{.HostConfig.LogConfig.Type}}' mycontainer# Non-blocking mode with max buffer size
docker run --log-driver json-file \
--log-opt mode=non-blocking \
--log-opt max-buffer-size=4m \
centos- Blocking – container waits until logs are written.
- Non-blocking – logs are buffered; if full, old logs are dropped.
You can add custom metadata to logs that show when use docker logs:
docker run --log-driver json-file \
--log-opt tag="{{.ImageName}}/{{.Name}}" \
centos- Available placeholders include
.ID,.Name,.ImageName,.ImageID,.DaemonName, etc.
Docker supports three main storage types for persisting or sharing data between host and containers.
- Managed by Docker.
- Stored under:
/var/lib/docker/volumes/
- Best choice for persistent data (backups, migration).
- Maps a host file or directory into the container.
- Path on the host is fully controlled by you.
- Changes affect both host and container immediately.
Tmpfs mounts store data temporarily in host memory (never written to disk).
- Data is lost when the container stops.
- Useful for سensitive and security data like passwords or temporary files.
- Cannot be shared between containers.
- Supported only on Linux hosts.
docker volume create myvol --label testvol# Shows all volumes on the host
docker volume ls# Mount the volume to a path inside the container
docker run -d -v myvol:/mydata myimage
# Mount the same volume to two paths inside the container
docker run -d -v myvol:/mydata1 -v myvol:/mydata2 myimage
# Alternative syntax with --mount (recommended)
docker run -itd --mount type=volume,source=myvol,target=/mydata myimage# List volumes by name
docker volume ls -f name=myvol
# Show detailed info about a volume
docker volume inspect myvol# Lists all containers that are using the volume named 'myvol'
docker ps -a --filter volume=myvol
# Shows detailed mount points (volumes, bind mounts) of the specified container
docker inspect --format="{{.Mounts}}" mycontainerdocker volume create \
--driver local \
--opt type=nfs \
--opt o=addr=192.168.100.10,rw \
--opt device=:/data/ \
mynfsvol- Creates a Docker volume that mounts an NFS export from 192.168.100.10:/data/.
- The NFS server must be reachable and allow the host to mount the export.
- If the server is not available, Docker still creates the volume without error, but attaching it to a container will fail until the NFS server is accessible.
# Remove a specific volume
docker volume rm myvol
# Remove all unused volumes
docker volume pruneNote:
- Deleting a volume removes all its data.
- Containers using that volume must be stopped or removed first. If you don’t stop or remove the containers using the volume first, Docker will not allow the volume to be removed and will return an error.
Bind mounts map a host directory into a container. Permissions control what the container can do.
docker run -itd --read-only \
--mount type=bind,source=/mnt,target=/mnt \
myimage- Container’s root filesystem is read-only.
/mntin host is writable.
# Read-only mount
docker run -v /host/path:/container/path:ro myimage
# Read-write mount (default)
docker run -v /host/path:/container/path:rw myimagedocker run -itd \
--read-only \
--mount type=bind,source=/mnt,target=/mnt,rw \
myimage- Container’s root filesystem is read-only.
/mntin host and container is writable.
Note: Use --mount for complex setups; easier to read and avoids mistakes compared to -v.
docker run -itd --tmpfs /tmp myimagedocker run -itd --mount type=tmpfs,destination=/tmp myimageNote: Tmpfs is fast and secure for temporary in-memory storage.
Docker networks let containers communicate with each other and with the host.
They provide isolation, security, and flexible connection options.
# This shows all existing networks (bridge, host, none, or custom).
docker network lsNote: By default, Docker creates this three networks: bridge, host, none
-
bridge (default): Creates a private network on the host. Containers on the same bridge can talk to each other.
docker run -it --network bridge myimage
-
host: Shares the host’s network stack. The container uses the same IP as the host. No isolation.
docker run -it --network host myimage
-
none: No network is assigned. Useful for security or manual network setups.
docker run -it --network none myimage
-
overlay: Used for multi-host Docker Swarm. Connects containers across different machines (hosts).
# Create a custom overlay network docker network create -d overlay my_overlay # Run a container attached to the custom overlay network docker run -it --network my_overlay myimage
-
macvlan: Gives each container its own MAC and IP, making it appear as a physical device on the network.
# Create a custom macvlan network with subnet and gateway docker network create -d macvlan \ --subnet=192.168.1.0/24 \ --gateway=192.168.1.1 \ -o parent=eth0 \ my_macvlan # Run a container attached to the custom macvlan network docker run -it --network my_macvlan myimage
-
Inspect a container’s network:
Shows the container’s network configuration like Gateway, IP, network type, and MAC address (inside"NetworkSettings"section).docker inspect mycontainer
-
Inspect a network: Displays details of the network and the containers connected to it (inside
"Containers"section).docker network inspect my_network
-
On Docker host:
You can see Docker networks (likedocker0) and container interfaces using:ip a
-
Inside a container:
-
If the
ipcommand exists:ip a
-
If not available, use:
cat /proc/net/dev
This shows network interfaces inside the container so you can verify the container’s network setup.
-