How to Setup WordPress Docker Containers on AWS

How to Setup WordPress Docker Containers on AWS. This article will look at WordPress and Docker benefits and then the workflow of setting it up on AWS through Docker.

What is WordPress

WordPress is the world’s leading CMS website builder. Since its inception, WordPress site has facilitated millions of users to create and manage their blog. Its popularity is apparent from the fact that around 42% of the internet uses WordPress.

With such a wide user base, it is no surprise that many cloud providers and cloud services have dedicated resources to ensure robust integration with WordPress. Since WordPress websites need server space to be hosted, most cloud services like Azure and AWS offer this service in simple way.  Alternatively, users can set up WordPress on a cloud service using Docker container technology.

This article about how to Setup WordPress Docker Containers on AWS.  This is the WordPress instance on the cloud and how the whole process looks. 

WordPress benefits

  • Open source and easy to Use with multiple plugins (22,000+ plugins)
  • Enable WordPress SSO with any identity provider like Office 365.
  • Stable and innovative.
  • WordPress is browser based so manage your site from anywhere.
  • Very SEO friendly (H1, H2, H3 tags).
  • It is an easy WordPress content management.
  • Sharing ability (widgets for the social media, plug-ins, APIs).
  • 100% Customizable design of your website
  • Multiple users.
  • Stable and innovative.

What is Docker

Docker s a containerization tool that allows you to set up separate “containers” where you setup development environment. The containers are similar to virtual machines, but without the need to install a complete operating system to be able to run them. These are developed and maintained in a virtualized container space, with its libraries and other dependencies that are self contained and separate from the server. 

WordPress Docker

Docker Container technology, especially Docker, is getting increasingly common throughout the industry. Developers have made containers an essential component of their development workflow due to their flexibility, security and ease of use. Additionally, developers can use Docker Hub to get an official container image for some technology and after constructing the container you can start developing within it immediately.

WordPress gives its users an easy and intuitive way around building and designing a website. It follows a modular approach in which it has separated all the aspects of a website into separate modules. For example, the inner workings of the website and the services it uses are configured by the user by adding components called WordPress plugins. On the other hand, the aesthetic and front-end design of the website, along with its user experience, are provided through WordPress templates

WordPress Docker Pros

  • Both services can provide excellent accessibility and ease of use to the user, independently and together.
  • WordPress and Dockers give their users the freedom to configure the services themselves and provide them with premade options as well.
  • In WordPress users can design both components as per preference or utilize the wide variety available at dedicated marketplaces for both. 
  • For Docker, users can make their own container and container image from scratch or take one from Docker Hub.

Docker Benefits

  • No Need to Set Up (Multiple) Server Environments.
  • Less Resource Heavy Than a Virtual Machine.
  • Can Be Used on Any Platform.
  • Share your containers to ease collaborations.

How to Setup WordPress Docker Containers on AWS

Setup WordPress with Docker on AWS

Seeing container technology’s rise in popularity, all major cloud services, like AWS, now offer dedicated container services. These services help configure Docker AWS functionality and simplify all the other container tasks. Regardless of the cloud platform, developers can instantly go from developing their application in a native environment to doing so in the cloud thanks to the container services.

Today, we are looking at the specific case of bringing WordPress to AWS by utilizing the power of Docker containers. After setting up and configuring the WordPress container, we will deploy the container to an instance of the AWS EC2 service. That way we will have a WordPress site up and running on the AWS platform and this process will be a good example of how containers can reduce many issues.

WordPress Docker Compose

We will firstly configure our WordPress container inside a file called a Docker Compose file. Such files are for specifying the services that the container will be running. While you can configure your own per your preference, this example will take a premade file from Docker’s own ‘awesome-compose’ repository. 

Please go to the directory where the Compose file is saved and I run the following command.

				
					$ docker-compose up -d
				
			

Afterward, I can use the following command to check the containers running currently. Ideally, it should show users two containers, the database for WordPress and the main container itself.

				
					$ docker-compose ps
				
			

After confirming that everything is working well locally, it’s time to shoot for the cloud.

Deploy WordPress Docker Container on AWS

Since we are now moving to the cloud, we should look at the Docker Compose file that we worked on locally. It will require some changes to work in the cloud as per the new environment.

Our Docker Compose file looks like the following at the moment. 

				
					version: '3.7'
services:
 db:
   image: mysql:8.0.19
   command: '--default-authentication-plugin=mysql_native_password'
   restart: always
   volumes:
     - db_data:/var/lib/mysql
   environment:
     - MYSQL_ROOT_PASSWORD=wordpressrootpassword
     - MYSQL_DATABASE=test_wordpress
     - MYSQL_USER=emad
     - MYSQL_PASSWORD=wordpresspassword
 wordpress:
   image: wordpress:latest
   ports:
     - 80:80
   restart: always
   environment:
     - WORDPRESS_DB_HOST=db
     - WORDPRESS_DB_USER=emad
     - WORDPRESS_DB_PASSWORD=wordpresspassword
     - WORDPRESS_DB_NAME=test_wordpress
volumes:
 db_data:
				
			

Before moving on, we need to have an AWS account set up and its ECS service ready. As the service supports Docker extensively, there is no need to install Docker AWS separately. 

We have to choose a database service for the particular EC2 instance because ECS in Docker does not support volumes within Compose support. We go to the AWS console to get an Amazon Relational Database Service (RDS) for us.

We have selected MySQL server  because that is what was running locally.

Next we configure the RDS instance to have the password specified in our Docker Compose file above. We also open the summary of our RDS instance to get its database hostname and put it in our Compose file.

With our RDS instance running, it is time to modify the Compose file. The file will get much smaller because we will remove the db service present and specify that we are using a separate database instance. The file now looks like this:

				
					version: '3.7'
services:
 wordpress:
   image: wordpress:latest
   ports:
     - 80:80
   restart: always
   environment:
     WORDPRESS_DB_HOST: wordpressdbecs.c1unvilqlnyq.eu-west-3.rds.amazonaws.com:3306
     WORDPRESS_DB_USER: wordpress
     WORDPRESS_DB_PASSWORD: wordpress123
     WORDPRESS_DB_NAME: wordpressdbecs
				
			

Now, after our container is ready to be deployed on the cloud environment and go from simply WordPress to an AWS WordPress instance, we head to our final stage. We will use the Docker CLI because it supports direct interaction with ECS.

				
					docker ecs setup
				
			

We will set the following characteristics for the example:

  • Name of the instance: test_ecs 
  • Name of the AWS profile: ecstestprofile 
  • Region: (same as the deployed RDS database).

Afterwards you’ll need to enter AWS access key or, if using CLI, you can use N to put in user credentials.

With all the above steps done, we can use the following command to check if the ECS context is successfully created or not.   

				
					docker context ls
				
			

Once successful, we switch to our ECS context to deploy our container with the following command.

				
					docker context use test_ecs 
				
			

With the following command, we deploy our Docker Compose file through this command. This command then checks the log to see if WordPress is working correctly.

				
					docker test_ecs compose up
docker test_ecs compose logs

				
			

The security group of the RDS instance doesn’t have our WordPress container project; therefore, WordPress will not be able to connect to the database. We access the settings and within choose security groups, choose your instance.

Running the following command would get us the link to our WordPress site if the deployment was successful. The image below it should be what shows up in case of successful deployment.

				
					docker ecs compose ps
				
			

And so, we have WordPress deployed in the cloud and an instance that can run locally. Thanks for reading about how to Setup WordPress Docker Containers on AWS.

How to Setup WordPress Docker Containers on AWS Conclusion

Running WordPress in the cloud is significantly easier than running and maintaining it locally. By connecting it with a cloud database instance like above, a user no longer having to worry about limitations that come with physical resources. If the user chooses to scale up and expand their WordPress website to involve more plugins, that is significantly easier. Traditionally, bringing WordPress to the cloud would have been difficult, but with Docker, the process is massively simplified.

Avatar for Emad Bin Abid
Emad Bin Abid

I'm a software engineer who has a bright vision and a strong interest in designing and engineering software solutions. I readily understand that in today's agile world the development process has to be rapid, reusable, and scalable; hence it is extremely important to develop solutions that are well-designed and embody a well-thought-of architecture as the baseline. Apart from designing and developing business solutions, I'm a content writer who loves to document technical learnings and experiences so that peers in the same industry can also benefit from them.

3 1 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x