Running a WordPress container on an Ubuntu 22 machine can be a convenient way to set up and manage your WordPress website. Containers provide a lightweight and isolated environment, making it easier to deploy and maintain applications. In this guide, we will walk you through the steps to run a WordPress container on your Ubuntu 22 machine.
To run a WordPress container from an Ubuntu 22.04 machine, you’ll need to have Docker installed. Here’s a step-by-step guide to accomplish this:
Step 1: Install Docker
If Docker is not already installed on your Ubuntu 22.04 machine, you can install it by following the official Docker documentation. Here’s a brief outline:
1 2 3 |
sudo apt update |
Step 2: Install necessary dependencies to allow apt to use a repository over HTTPS:
1 2 3 4 5 6 7 8 |
sudo apt install -y \ apt-transport-https \ ca-certificates \ curl \ gnupg \ lsb-release |
Add Docker’s official GPG key:
1 2 3 |
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg |
Set up the stable Docker repository:
1 2 3 4 5 |
echo \ "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null |
Update the apt package index again:
1 2 3 |
sudo apt update |
Install Docker:
1 2 3 |
sudo apt install docker-ce docker-ce-cli containerd.io |
Verify that Docker is installed correctly by running:
1 2 3 |
sudo docker --version |
Step 3: Create a Docker Compose File
Create a docker-compose.yml
file with the WordPress configuration.
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 |
version: '3.3' services: db: image: mysql:5.7 volumes: - db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: somewordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress wordpress: depends_on: - db image: wordpress:latest ports: - "8000:80" restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_NAME: wordpress volumes: db_data: {} |
Step 4: Run Docker Compose
Navigate to the directory where your docker-compose.yml
file is located and run the following command to start the WordPress container:
1 2 3 |
sudo docker-compose up -d |
The -d
flag runs the containers in detached mode, meaning they will run in the background. If you omit -d
, you’ll see logs from the containers in your terminal.
Step 5: Access WordPress
Once the containers are up and running, you can access WordPress by opening a web browser and navigating to http://localhost:8000
. This assumes that you’ve mapped the container’s port 80 to port 8000 on your host machine, as specified in the docker-compose.yml
file.
Step 6: Complete WordPress Setup
Follow the on-screen instructions to complete the WordPress setup, including configuring the database connection using the credentials specified in the docker-compose.yml
file.
That’s it! You now have a WordPress instance running in a Docker container on your Ubuntu 22.04 machine.
Conclusion
Running a WordPress container on an Ubuntu 22 machine using Docker provides a flexible and efficient way to manage your WordPress website. By following the steps outlined in this guide, you can easily set up and run a WordPress container, allowing you to focus on building and maintaining your website.
All these steps are available as a executable file please make use of it.
Download wordpress_setup.sh
and make it executable:
1 2 3 |
chmod +x wordpress_setup.sh |
Then, run the script with root privileges:
1 2 3 |
sudo ./wordpress_setup.sh |
This script will automatically install Docker and Docker Compose, create a docker-compose.yml
file with the WordPress configuration, and run the WordPress container using Docker Compose.