Traditional infrastructure often requires dedicated servers for each web service, leading to high resource consumption and slow provisioning. Inconsistenceis between development, testing, and production environments frequently cause unexpected errors. Containerization technology addresses these issues by allowing multiple isolated environments to run on a single Linux host.
Core Architecture
Docker operates using three primary components:
- Registry: A centralized storage location for container images. Users search and retrieve images using command-line tools.
- Image: A read-only template containing the application code and dependencies. It acts as a blueprint for creating containers.
- Container: A runnable instance of an image. Containers are isolated processes that share the host kernel but maintain separate file systems and network stacks. Data persistence is managed through volumes to ensure information survives container restarts.
Essential Command Line Operations
Before beginning, ensure the Docker engine is installed on the Linux host.
curl -fsSL https://get.docker.com/ | sh
Image Management
Retrieve public images from the registry. If a specific version is not specified, the latest tag is used by default.
docker pull httpd:latest
docker pull mysql:5.7
For offline environments, images can be exported to a tar archive and loaded locally.
docker load < custom-image-archive.tar
Assigning aliases to image IDs simplifies management.
docker images
docker tag a1b2c3d4e5f6 my-web-stack:v1
Container Lifecycle
Launch a container in detached mode. Each execution creates a new instance.
docker run -d my-web-stack:v1
Monitor active and stopped instances.
docker ps -a
Access the internal shell of a running process.
docker exec -it <container_id> /bin/bash
To detach from the terminal without stopping the process, use the specific key sequence Ctrl+p followed by Ctrl+q.
Managing state involves stopping, starting, or restarting specific instances. Removal is only permitted for stopped containers.
docker container stop <container_id>
docker container start <container_id>
docker container rm <container_id>
For detailed syntax information, append --help to any command.
Persistent Storage with Volumes
Data written inside a container is ephemeral. Volumes provide a mechanism to persist data outside the container lifecycle.
- Volumes are shared across multiple containers.
- Chenges are immediate.
- Modifications do not alter the underlying image.
- Existence is independent of the container state.
Volume Commmands
docker volume create app_storage
docker volume inspect app_storage
docker volume ls
docker volume rm app_storage
docker volume prune # Removes unused volumes
Mounting a volume during container creation links host storage to the container path.
docker run -d -p 8000:80 -v app_storage:/var/www/html my-web-stack:v1
Practical Deployment: LAMP Stack
This example demonstrates setting up a Linux, Apache, MySQL, and PHP environment using a custom image.
1. Prepare the Image
Load the archived image and assign a recognizable tag.
docker load < /home/user/centos-lamp-backup.tar
docker tag <image_id> local-lamp-env
2. Configure Source Code
Create a project directory and extract the application files.
mkdir web-project
cp source-archive.zip web-project/
cd web-project
unzip source-archive.zip
Ensure the upload directory within the extracted files has appropriate permissions for the web server.
3. Inspect Configuration
Run a temporary container to identify default configuration paths such as DocumentRoot and listening ports within httpd.conf.
docker run -it local-lamp-env /bin/bash
# Inside container
grep DocumentRoot /etc/httpd/conf/httpd.conf
grep Listen /etc/httpd/conf/httpd.conf
exit
Remove the temporary container after inspection.
4. Launch Service
Start the permanent container, mapping host port 8080 to container port 80 and mounting the data volume.
docker run -d -p 8080:80 -v app_storage:/var/www/html local-lamp-env
Access the installation wizard via http://localhost:8080/upload.
5. Multi-Environment Setup
To support parallel environments (e.g., Dev, Test), duplicate the project directory and launch additional containers on different ports.
mkdir web-project-test
cp -r source-files web-project-test/
chmod 777 -R web-project-test/
docker run -d -p 8081:80 -v app_storage:/var/www/html local-lamp-env
docker run -d -p 8082:80 -v app_storage:/var/www/html local-lamp-env
Distinct ports allow simultaneous access to different instances using the same base image.