You may be familiar with Docker already, but of course in order to run any app successfully you will also need to juggle more than one container. The easiest way to handle this is using Docker Compose. Once the structure of your application is defined, you can then simultaneously create and initiate those containers with one simple command.
At the time of the publishing this article, the latest version is 1.26.2, and the latest version can be downloaded using curl:
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Make sure that Docker Compose has executable permissions:
sudo chmod +x /usr/local/bin/docker-compose
You can test to make sure that the latest version of Docker Compose is installed:
The response should be something along the lines of:
docker-compose version 1.26.2, build eefe0d31
version: '3.8' services: wp_mysql: image: mysql:8 container_name: wp_mysql ports: - "3336:3306" volumes: - db_data:/var/lib/mysql restart: always command: --default-authentication-plugin=mysql_native_password environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: wpdb MYSQL_USER: wpuser MYSQL_PASSWORD: wppass wp: depends_on: - wp_mysql image: wordpress:latest container_name: wp ports: - "8888:80" restart: always environment: WORDPRESS_DB_HOST: wp_mysql WORDPRESS_DB_NAME: wpdb WORDPRESS_DB_USER: wpuser WORDPRESS_DB_PASSWORD: wppass WORDPRESS_DEBUG: 1 volumes: db_data:
This sets up two docker containers — one for the MySQL database, and another for the WordPress installation.
First, we need to define the Docker Compose version. As of the writing of this post, “3.8” is the latest, which maps to the Docker Engine 19.03.0+. If you use an older version, it can still successfully run on newer versions of Docker.
Following that, we have the “services” section, where we define each of our containers. Inside that, we have two sections.
First, let’s see the database that we have named wp_mysql. This is important so we can reference this container later on.
We base the container on the mysql:8 image which maps to the latest version of MySQL 8.
The ports are only important if you want to be able to easily access this database from your host. This maps the 3306 port to the host’s 3336.
Next, we have the volumes list where we map the db_data volume to the /var/lib/mysql location within the MySQL container.
We have restart: always which will ensure that our container is always running and even if something causes it to fail, it will automatically restart.
Do note that we need to include the following command:
Lastly, we have the environment settings that define the database, user, and passwords. Make sure that you use the same settings later when configuring the WordPress container.
Next, we define the WordPress container:
Initializing our Docker containers
docker-compose up -d
mysql -u wpuser -pwppass -h 127.0.0.1 -P 3336 -D wpdb
This will connect to our MySQL container and give us full access to the database.
Navigating the browser to the http://localhost:8888 URL we should be able to see the WordPress installation page.
Once that’s done, you have your WordPress up and running without any need to locally install PHP or MySQL.
Shutting down our Docker Containers
This maps the local folder to the WordPress /var/www/html, allowing us to easily edit the files afterwards.
About the Author