DEV Community

Antoine
Antoine

Posted on • Updated on

Set up WordPress with Docker CLI

Before installing WordPress with Docker you will need to have somewhere to store the data. MariaDB is a community-developed relational database management system and a drop-in replacement for MySQL. It is officially available on Docker and provides easy instructions with up to date images.

MariaDB with Docker

Start off by making a new directory where you wish to store the files for WordPress and MariaDB for example in your home directory.

mkdir ~/wordpress && cd ~/wordpress

Downloading and installing a new MariaDB container can all be performed with a single command. Before jumping in check the required parameters.

MariaDB Environment variables are marked in the Docker command with -e:

  • -e MYSQL_ROOT_PASSWORD=- set your own password here.
  • -e MYSQL_DATABASE= - creates and names a new database e.g. wordpress.

Docker parameters:

  • –name wordpressdb – names the container;
  • -v “$PWD/database”:/var/lib/mysql – creates a data directory linked to the container storage to ensure data persistence;
  • -d – tells Docker to run the container in daemon;
  • mariadb:latest – finally defines what to install and which version.

Then run the command below while replacing the with your own.

docker run -e MYSQL_ROOT_PASSWORD=<password> -e MYSQL_DATABASE=wordpress --name wordpressdb -v "$PWD/database":/var/lib/mysql -d mariadb:latest
Enter fullscreen mode Exit fullscreen mode
...
Status: Downloaded newer image for mariadb:latest
23df0ec2e48beb1fb8704ba612e9eb083f4193ecceb11102bc91232955cccc54
Enter fullscreen mode Exit fullscreen mode

If Docker was successful at creating the container, you should see a code at the end of the output similar to the example above. You can confirm that the MariaDB container is running by using the following command:
docker ps

Check the status for your MariaDB install, it should show “Up” and the time it has been running like in the example output below.

CONTAINER ID IMAGE          COMMAND                CREATED        STATUS        PORTS      NAMES
14649c5b7e9a mariadb:latest "/docker-entrypoint.s" 12 seconds ago Up 12 seconds 3306/tcp   wordpressdb
Enter fullscreen mode Exit fullscreen mode

Other useful commands for working with containers are start, stop and remove.

docker start <container name>
docker stop <container name>
docker rm <container name>
Enter fullscreen mode Exit fullscreen mode

You can find out more about available commands and options for specific commands.

docker --help
docker <command> --help
Enter fullscreen mode Exit fullscreen mode

Full command-line documentation is also available over at the Docker support page.

WordPress with Docker

Applications in containers run isolated from one another in the user's space of the host operating system sharing the kernel with other containers. This reduces the overhead required to run packaged software while also enabling the containers to run on any kind of infrastructure. To allow applications within different containers to work with one another Docker supports container linking.

WordPress is also made officially available on Docker Hub, pull the image using the command below. When the version to download is not specified Docker will fetch the latest available.
docker pull wordpress

WordPress container also takes environment variables and Docker parameters:

  • -e WORDPRESS_DB_PASSWORD= - sets the same database password here; – -name wordpress – gives the container a name; – -link wordpressdb:mysql – links the WordPress container with the MariaDB container so that the applications can interact;
  • -p 80:80 – tells Docker to pass connections from your server’s HTTP port to the containers internal port 80;
  • -v “$PWD/html”:/var/www/html – sets the WordPress files accessible from outside the container. The volume files will remain even if the container was removed;
  • -d – makes the container run on background
  • -wordpress – tells Docker what to install. Uses the package downloaded earlier with the docker pull wordpress -command.

Run the command below while replacing the as you did for the MariaDB container.

docker run -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=<password> --name wordpress --link wordpressdb:mysql -p 80:80 -v "$PWD/html":/var/www/html -d wordpress
Enter fullscreen mode Exit fullscreen mode

Then open your server’s domain name or IP address in a web browser to test the installation. You should be redirected to the initial WordPress setup page at http:///wp-admin/install.php. Go through the setup wizard and you are done.

If you get an error linking your server’s public IP address to the WordPress container’s internal address, remove the failed container using the following command:
docker rm wordpress

Top comments (0)