WordPress with Docker Applications in containers run isolated from one another in the userspace 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.
Mar 15, 2017 Deploying containers with Docker isn't nearly as complicated as you might think. Jack Wallen walks you through the process of installing and deploying WordPress, with the help of Docker. Jun 01, 2020 Local WordPress using Docker – Running a local WordPress development environment is crucial for testing themes and plugin before we push into staging or production environment. To run WordPress locally, we need to install and setup PHP, MySQL (or MariaDB) and WordPress which is not straightforward. Nov 02, 2019 In this guide, let’s go through the process of installing and configuring WordPress on a Docker container using docker-compose. The primary goals are to install a bare-bones install with the ability to customise wp-config.php, php.ini and my.cnf.
AWSOnTop runs wordpress on EC2 and MySql on RDS. In this post ‘Migrate WordPress to Docker’, you will find how to migrate an existing wordpress site into a docker container.
You can check my earlier posts on how to create a website using WordPress @ EC2 here.
Dockerizing your site makes it easy to port to any server and spinning up the application in few minutes.
To perform this, you will have to have a Docker installed in your local machine. I already have installed Docker in my local.
You can use this Docker-Download-link to dowload and install Docker Desktop.
Tasks you will perform are listed below,
- Export your Mysql Database dump from RDS.
- Tar & copy your ‘/var/www/html’ folder from EC2.
- Replace your site url to your local host url,so as to make the wordpress site work locally before replacing back to original site url.
- Fix any broken links/url.
- Create a single Docker Compose file to dowload wordpress docker image and mysql docker image to create respective docker containers.
- Run your wodpress site from the container locally.
- You can migrate this container back to a new EC2 and host your site via a container — This is not part of this post, but will be part of my next post.
Migrate WordPress to Docker Container:
It is advised to turn-off or disable all your plugins before starting the migration process on your wordpress. However if you wish to go with your plugins you can still export and install the plugins in your local to address any issues.
Export MySql Database from RDS
Login into EC2 server – CLI and perform below (and get your database endporint)
There are multiple ways to move this database export dump file into your local. I have used S3 here to move the file from EC2 to S3 and from there to my local.
To move the files to S3, create a new bucket. I have created ‘awsontopbackup’ bucket and executed below cli at ec2 ,
Copying WP-Content:
You have now taken the export of your wordpress database. Next you will have to copy the wordpress content from EC2 to your local. You will follow the same process of copying to your S3 and then to your local after tar’ing the source content.
You can start copying from the main ‘/var’ directory itself as below,
WordPress Backup
Now you have got both the front end and database exported to S3. Download he same from S3 to your local.
Docker Wordpress Mysql
After downloading from S3, you should untar the awsontop.tar. You can use 7Zip for extracting the content on you local machine.
Create a backup file for your databasedump.sql so as to keep it safe from corruption. I have created a file ‘awsontop_usethis.sql’ from the original databasedump.sql. Open awsontop_usethis.sql l in your favourite text editor and replace the site url to localhost example,
Find ‘http://54.160.112.93/’ and replace with ‘http://localhost:8000’ and save the file. You can have port number as desired by you.
Now you are all set to write a Docker-Compose file and create a container for your wordpress site.
Docker Compose:
The full source code of Docker compose file is available at my github repo: Sourcecode.
Now lets break down the docker compose,
Docker Wordpress Https
Basically you will need 2 services here,
- WordPress
- MySql
DB Service:
- Name of the service = db.
- You will download mysql verion 5.7 from docker hub.
- You have already downloaded the mysql export, use the same to import into mysql database. In our case it is ‘awsontop_usethis.sql’.
- ‘docker-entrypoint-initdb.d’ is the place where the import will be copied to and mysql starts with this file.
- Data persistance for database is taken care via volume:db_data located at /var/lib/mysqlpersist.
- Command: default-authentication-plugin=mysql_native_password. This is optional, to validate all database users (including root) using mysql_native_password authentication. The mysql_native_password authentication plugin is the default authentication plugin that will be used for an account created when no authentication plugin is explicitly mentioned.
Above environment details should be self-explanatory. Actually, we will store these passwords in a separate secret file, for ease you can hard here.
WordPress Service:
- Your wordpress services is dependent on Mysql DB service, hence the same should be mentioned on ‘depends_on:’ segment.
- The Docker image for wordpress, you can download either the latest version or version of your choice. You can find all official wordpress image here.
- Ports 8000 is the port on which your browser will be hit. And-in turn it should look for a port:80, which is WordPress port inside Docker container.
- Environment details are ver much self-explanatory, to connect to mysql service.
- Local Volume to be mapped to container volume for data persistence – ./var/www/html:/var/www/html.
Once you have completed your docker compose file, all you need to do is jus run below comand
This will download both the wordpress & mysql docker image from docker hub. Docker compose will automatically establish network connection between these services.
You can run docker ps -all command to check the containers which got just launched in another shell.
Under the wordpress container service, you can clearly see the port mapping ‘0.0.0.0:8000->80/tcp ‘, local host port:8000 is mapped to port:80 of docker container.
Now check the same in your browser by entering : http://localhost:8000/
WordPress Development Environment In Docker • Yoast Developer ...
Thats all is required to move a wordpress to Docker container.
Conclusion:
Docker Wordpress
Migrating wordpress to Docker makes it more portable and ease the blue green deployments, not just with wordpres for that matter a lot of websites can be upgrade without any downtime. If you developed these steps in your local, it is important to remove the images and volumes free up space. Use the below commands for same.
Docker Wordpress Hub
In my next post will be on how to move this docker image from your local to AWS ec2.