Docker Container Lifecycle Management and Image Configuration
Container runtimes encapsulate application code, system libraries, and runtime dependencies into isolated, executable packages. This architecture guarantees consistant execution across diverse host environments.
Environment Provisioning
On Debian-based distributions, install the container engine directly from the package repository:
sudo apt update
sudo apt install -y docker.io
sudo systemctl enable --now docker
Desktop environments on Windows or macOS require the official client application, which manages a lightweight virtualized Linux subsystem.
Defining the Image Blueprint
Images are generated from declarative configuration files named Dockerfile. The following specification uses a lightweight Alpine base, installs an HTTP server, and establishes the execution entrypoint:
FROM alpine:3.18
RUN apk add --no-cache nginx curl
COPY index.html /usr/share/nginx/html/
EXPOSE 8080
ENTRYPOINT ["nginx", "-g", "daemon off;"]
The RUN directive executes during the build layer creasion. EXPOSE documents the network binding, while ENTRYPOINT specifies the foreground process.
Building the Artifact
Execute the builder utility within the directory containing the blueprint. The -t parameter assigns a repository tag:
docker build -t web-backend:latest .
The daemon processes instructions sequentially, caching intermediate layers. The final read-only image is registered locally.
Executing the Container
Instantiate the image as an active process. Network port binding routes external traffic into the isolated environment:
docker run -d -p 9090:8080 --name api-instance web-backend:latest
Detach mode (-d) runs the process asynchronously. Host port 9090 forwards requests to the container's 8080. The --name flag assigns a static handle for management commands.
Monitoring Active Instances
Query the runtime daemon for active processes:
docker container ls
Append the --all flag to display terminated or exited instances alongside running ones:
docker container ls --all
The terminal output lists identifiers, image references, port mappings, status states, and creation timestamps.
Graceful Shutdown and Cleanup
Send a termination signal to an active instance using its identifier or assigned name:
docker container stop api-instance
Remove the stopped filesystem layer to reclaim disk space:
docker container rm api-instance
Orphaned resources can be cleared using the system maintenance command:
docker system prune -a --volumes
Delete specific cached images by verifying they lack active references, then execute:
docker image rm web-backend:latest