Linux OS / Other / Software

Create Your First WordPress in Container using Docker

Share this post

WordPress is the most popular CMS on the internet, and in this guide we’re going to show you how to run your WordPress site in a Docker container and what are the benefits of it.

Docker is the fastest way to set up your local environment without installing any apps like Apache, MySQL server, XAMPP, Wamp or something like that. Not necessary to manage a number of virtual machines for their different projects, just install docker pull img and that is it. Host your WordPress in few seconds.

docker
Docker

Docker is a type of virtualization faster and lighter than other VMs. Using docker we can have dozens of Docker containers on a single PC. To start using docker we need to install it on our PC. It’s one of the fastest-growing tools out there and we can install it on all platforms including Windows, Mac or Linux. In this tutorial we can install it on Linux – Ubutnu Light version ( Lubutntu 18.10 ).

To do that we need to install docker on our PC. Easiest way for me is to use Get Docker Install Script but if you prefered some other method be free and use it. Go to https://get.docker.com/ and in terminal type:

curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh
check-docker-version
Check Docker version

After the installation complete we can check Docker version typing command like on the picture above. To be able to just run Docker commands without having to type sudo or go in his actual root, we can do this particular command, where we are just adding our user account to the Docker group on our PC.

sudo usermod -aG docker {username}

Now we need to install docker-machine, to do that we need type code below in terminal. Check the version of machine and go to the next step.

base=https://github.com/docker/machine/releases/download/v0.14.0 &&
  curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine &&
  sudo install /tmp/docker-machine /usr/local/bin/docker-machine

Also, we need to install Docker Composer. Easiest way fo do that is to go on https://github.com/docker/compose/releases and copy code in terminal, but first we need to install git.

sudo apt-get install git
sudo curl -L https://github.com/docker/compose/releases/download/1.23.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

With this step we are finished Docker installation and now we can build our first WordPress container but first i think that is importarnt to understand Docker archutecture. Best documentation about that we can find on https://docs.docker.com/ but if you too lazy you can read this paragraph below.

Docker uses a client-server architecture. The Docker client talks to the Docker daemon, which does the heavy lifting of building, running, and distributing your Docker containers. The Docker client and daemon can run on the same system, or you can connect a Docker client to a remote Docker daemon. The Docker client and daemon communicate using a REST API, over UNIX sockets or a network interface.

Docker architecture
Docker architecture

The first thing we need to do is to define how our image will look like. To do this we will create a Dockerfile. It’s a text file that is added to the directory with the code for your application.

Dockerfile will consist of two commands:

  1. FROM – to build the application, use the official image from https://hub.docker.com
  2. COPY – copy my code to the defined directory in my image

Open terminal and type following code. In the first line we create directory called wordpress-site then put our site in this directory. Then go to this directory and create file named Dockerfile and put that two lines in it.

sudo mkdir wordpress-site 
cd wordpress-site
touch Dockerfile
cat > Dockerfile <<EOL
FROM wordpress:php7.1-apache
COPY . /var/www/html
EOL

Once we define our Dockerfile , we can build the image in the terminal. The command below will build an image named “wordpress-img“.

docker build -t 'wordpress-img' .

Now it’s time to run our first container. Using command below we starts a MYSQL Docker container and named it “mysql“. It is running in the background with -d and we passing MYSQL_ROOT_PASSWORD variable which defines the root password for MYSQL server.

docker container run --name mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.24

If all is good we need to start another container based on the image “wordpress-img” and named it “wordpress-cont” then runs in the background using -d and has port 8080 mapped from the host to port 80. This means that if you go to localhost:8080 in your browser, you will be able to open your wordpress. Also we use command –link it means that the MYSQL container will be visible inside the “wordpress-cont“.

docker container run --name wordpress-cont --link mysql:mysql -p 8080:80 -d wordpress-img

After completing this we are able to start our wordpress installation and after few steps we have our first wordpress dockerization.

Wordpress-dockerization
WordPress Dockerization

Related Stories

%d bloggers like this: