In the modern era of software development, Docker has become an indispensable tool for developers and DevOps engineers. It streamlines the process of deploying applications by providing a consistent environment across different stages of development, testing, and production. In this blog post, we’ll explore what Docker is, how to install it, and demonstrate a common use case: running a local WordPress site using Docker Compose.
What is Docker?
Docker is an open-source platform that automates the deployment, scaling, and management of applications. It does this by using containerization, which packages an application and its dependencies into a single, lightweight container. Containers are isolated from each other and the host system, ensuring that the application runs consistently regardless of the environment.
Key Benefits of Docker
- Consistency: Docker containers ensure that the application works the same way in any environment, eliminating the “it works on my machine” problem.
- Isolation: Each container is isolated, meaning that different applications can run on the same host without interfering with each other.
- Scalability: Docker makes it easy to scale applications up or down by adding or removing containers.
- Efficiency: Containers share the host OS kernel, making them more lightweight and faster to start than traditional virtual machines.
How to Install Docker
Prerequisites
- A 64-bit version of Windows 10 or later, macOS, or a modern Linux distribution.
Installation Steps
For Windows and macOS
-
Download Docker Desktop:
- Go to the Docker Desktop download page and download the installer for your operating system.
-
Install Docker Desktop:
- Run the installer and follow the on-screen instructions. For Windows, you may need to enable the WSL 2 (Windows Subsystem for Linux) feature during the installation.
-
Start Docker Desktop:
- After installation, launch Docker Desktop. You may need to sign in with your Docker Hub account.
For Linux
For Debian-based Distributions (Ubuntu, Debian)
-
Update your package index:
1
sudo apt-get update
-
Install Docker:
1
sudo apt-get install docker-ce docker-ce-cli containerd.io
-
Start Docker:
1
sudo systemctl start docker
-
Enable Docker to start on boot:
1
sudo systemctl enable docker
-
Post-Installation Steps (Optional):
-
To run Docker commands without
sudo
, create a Docker group and add your user to it:1 2
sudo groupadd docker sudo usermod -aG docker $USER
Log out and back in to apply the group changes.
-
For Arch Linux
-
Update your package database:
1
sudo pacman -Syu
-
Install Docker:
1
sudo pacman -S docker
-
Start Docker:
1
sudo systemctl start docker
-
Enable Docker to start on boot:
1
sudo systemctl enable docker
-
Post-Installation Steps (Optional):
-
To run Docker commands without
sudo
, add your user to the Docker group:1
sudo usermod -aG docker $USER
Log out and back in to apply the group changes.
-
Running a Local WordPress Site with Docker Compose
Docker Compose is a tool that allows you to define and manage multi-container Docker applications. To illustrate its power, we’ll set up a local WordPress site.
Step-by-Step Guide
-
Create a Project Directory:
1 2
mkdir my_wordpress cd my_wordpress
-
Create a
docker-compose.yml
File:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
services: db: # We use a mariadb image which supports both amd64 & arm64 architecture image: mariadb:10.6.4-focal # If you really want to use MySQL, uncomment the following line #image: mysql:8.0.27 command: '--default-authentication-plugin=mysql_native_password' volumes: - db_data:/var/lib/mysql restart: always environment: - MYSQL_ROOT_PASSWORD=somewordpress - MYSQL_DATABASE=wordpress - MYSQL_USER=wordpress - MYSQL_PASSWORD=wordpress expose: - 3306 - 33060 wordpress: image: wordpress:latest volumes: - wp_data:/var/www/html ports: - 8000:80 restart: always environment: - WORDPRESS_DB_HOST=db - WORDPRESS_DB_USER=wordpress - WORDPRESS_DB_PASSWORD=wordpress - WORDPRESS_DB_NAME=wordpress volumes: db_data: wp_data:
-
Run Docker Compose:
1
docker-compose up -d
This command tells Docker Compose to start the containers in detached mode (
-d
). -
Access Your WordPress Site:
- Open your web browser and go to
http://localhost:8000
. You should see the WordPress installation page.
- Open your web browser and go to
Explanation of docker-compose.yml
- version: Specifies the Docker Compose file format version.
- services: Defines the two services:
db
(database) andwordpress
(WordPress application).- db service uses the Mariadb 10.6.4 image, with environment variables for database setup.
- wordpress service uses the latest WordPress image and links it to the
db
service.
- volumes: Creates persistent storage for both the database and WordPress files, ensuring data is retained between container restarts.
Common Issues and Configuration
How to use local proxy
-
Create a folder for configuring the Docker service through systemd
mkdir /etc/systemd/system/docker.service.d
-
Create a service configuration file at
/etc/systemd/system/docker.service.d/http-proxy.conf
and put the following in the newly created file
|
|
-
Reload systemctl so that new settings are read
sudo systemctl daemon-reload
-
Verify that docker service Environment is properly set
sudo systemctl show docker --property Environment
-
Restart docker service so that it uses updated Environment settings
sudo systemctl restart docker
Credentials Error
You may receive error similar to this one:
|
|
Solution
-
first stop docker with this command
sudo systemctl stop docker
-
remove old docker config
rm ~/.docker/config.json
-
start docker
sudo systemctl start docker
Conclusion
Docker simplifies the process of deploying and managing applications by encapsulating them in containers. Using Docker Compose, you can easily define and run multi-container applications like WordPress. Whether you are a developer looking to streamline your workflow or an operations engineer aiming for consistent deployment environments, Docker is an essential tool in your toolkit.