Linux > Virtualization

How to Install WordPress and PhpMyAdmin using Docker Compose (CentOS 7)


Docker is a lightweight container system that lets you run an application in a container, which is like virtual machine. You can refer the below steps to install docker on CentOS 7 and then set up a WordPress instance using a docker file.

Access your server via CLI and run below command to update package database

--- Code: ---yum check-update
--- End code ---

Run the below command to add official Docker repository, download and install the latest version of Docker:

--- Code: ---curl -fsSL | sh
--- End code ---

Once the installation is over run the below command to start the Docker service

--- Code: ---systemctl start docker
--- End code ---

Verify that the service is active

--- Code: ---systemctl status docker
--- End code ---


--- Code: ---● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2018-11-18 08:29:49 UTC; 10s ago
 Main PID: 4352 (dockerd)
    Tasks: 18
   Memory: 55.7M
   CGroup: /system.slice/docker.service
           ├─4352 /usr/bin/dockerd -H unix://
           └─4363 containerd --config /var/run/docker/containerd/containerd.toml --log-level info
--- End code ---

Please Note: Most of the cloud service like DigitalOcean and AWS etc offers OS  images that comes with Docker preinstalled. Hence you are using cloud hosting service like AWS, DiditalOcean then you can use the Docker image while deploying the server so that you do not have to do the above steps manually.

Once the Docker is installed you can use the below command to check the Docker version

--- Code: ---docker version
--- End code ---


--- Code: ---Client:
 Version:           18.09.0
 API version:     1.39
 Go version:      go1.10.4
 Git commit:      4d60db4
 Built:                Wed Nov  7 00:48:22 2018
 OS/Arch:          linux/amd64
 Experimental:   false

Server: Docker Engine - Community
  Version:            18.09.0
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.4
  Git commit:       4d60db4
  Built:                 Wed Nov  7 00:19:08 2018
  OS/Arch:          linux/amd64
  Experimental:   false
--- End code ---

Now that Docker is installed we need to install Docker-Compose. Please install python-pip as prerequisite using below commands:

--- Code: ---yum install epel-release
yum install -y python-pip
--- End code ---

Then you can install Docker Compose:

--- Code: ---pip install docker-compose
--- End code ---

Once docker compose is installed we need to create a new folder for WordPress installation and move into the folder. Please use the below command:

--- Code: ---mkdir ~/wordpress && cd $_
--- End code ---

Now use the below command to create a docker compose yml file for WordPress installation

--- Code: ---vim docker-compose.yml
--- End code ---

Then pase the below content into the yml file and save it.

--- Code: ---version: '2'
      - db
    image: wordpress:4.7.1
    restart: always
      - ./wp-content:/var/www/html/wp-content
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_PASSWORD: p4ssw0rd!
      - 80:80 # Expose http and https
      - 443:443
      - back
    image: mysql:5.7
    restart: always
       - ./db_data:/var/lib/mysql
      MYSQL_ROOT_PASSWORD: p4ssw0rd!
      - back
      - db
    image: phpmyadmin/phpmyadmin
    restart: always
      - 8080:80
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: p4ssw0rd!
      - back
--- End code ---

Please Note The above code will download docker images of WordPress 4.7.1, MySQL 5.7 and PhpMyAdmin images from docker repository and run it.

Please Note It will also create folder ./db_data and ./wp-content  in the wordpress directory that we created earlier and sync the wp-content folder and the database contact into it. This will help us save website data on the server so that even if we remove the containers later the website data will be still safe on the server.

You can use the below command to execute the Docker compose file

--- Code: ---docker-compose up -d
--- End code ---


--- Code: ---Pulling db (mysql:5.7)...
5.7: Pulling from library/mysql
a5a6f2f73cd8: Pull complete
936836019e67: Pull complete
283fa4c95fb4: Pull complete
1f212fb371f9: Pull complete
e2ae0d063e89: Pull complete
5ed0ae805b65: Pull complete
0283dc49ef4e: Pull complete
a7905d9fbbea: Pull complete
cd2a65837235: Pull complete
5f906b8da5fe: Pull complete
e81e51815567: Pull complete
Pulling wordpress (wordpress:4.7.1)...
4.7.1: Pulling from library/wordpress
5040bd298390: Pull complete
568dce68541a: Pull complete
6a832068e64c: Pull complete
3b0f3d176a5b: Pull complete
20cc248a5690: Pull complete
6ff565538ee6: Pull complete
9f1077228581: Pull complete
57359f144a19: Pull complete
9754ef36b033: Pull complete
e156df35b624: Pull complete
df09daa2224a: Pull complete
bfa0d8031302: Pull complete
fe629d500af7: Pull complete
2761420c8e70: Pull complete
71580740e433: Pull complete
22ff3670a5e6: Pull complete
39f4427993e5: Pull complete
9ab2843f4934: Pull complete
Pulling phpmyadmin (phpmyadmin/phpmyadmin:)...
latest: Pulling from phpmyadmin/phpmyadmin
911c6d0c7995: Pull complete
2527cc05836d: Pull complete
3299172a2d6d: Pull complete
05782106624f: Pull complete
3e3261543ec8: Pull complete
ce1c845d4246: Pull complete
e005e70f55dc: Pull complete
84403a106e16: Pull complete
e4fc13f7949f: Pull complete
2a53e72ea7b9: Pull complete
d791f6b71f27: Pull complete
576bdfd7cc75: Pull complete
20768032f0fb: Pull complete
fc8aa3d46064: Pull complete
bade7addd704: Pull complete
4b30a4dd0b24: Pull complete
b02d6e91d2b5: Pull complete
Creating wordpress_db_1_56e97f6fca35 ... done
Creating wordpress_phpmyadmin_1_45b68f706ee4 ... done
Creating wordpress_wordpress_1_f11d769b43d1  ... done
--- End code ---

You can run the below command to see the list of containers that are running on the server:

--- Code: ---docker ps
--- End code ---


--- Code: ---CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                      NAMES
e0fcb5383ea3        phpmyadmin/phpmyadmin   "/ supervisord…"   23 minutes ago      Up 23 minutes       9000/tcp,>80/tcp             wordpress_phpmyadmin_1_5cb4745b8656
d97b84e9174f        wordpress:4.7.1         "docker-entrypoint.s…"   23 minutes ago      Up 23 minutes>80/tcp,>443/tcp   wordpress_wordpress_1_ae4ee5d1d3b1
f87672746548        mysql:5.7               "docker-entrypoint.s…"   23 minutes ago      Up 23 minutes       3306/tcp, 33060/tcp                        wordpress_db_1_67afaa299feb
--- End code ---

Run the below command to see the list of images downloaded by the docker compose the file.

--- Code: ---docker images -a
--- End code ---


--- Code: ---REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
mysql                   5.7                 ae6b78bedf88        2 days ago          372MB
phpmyadmin/phpmyadmin   latest              126b8717cebb        2 months ago        166MB
wordpress               4.7.1               dfbefe759772        22 months ago       400MB
--- End code ---

You can then access the server IP to start the WordPress installation. Please refer the below image:


[0] Message Index

Go to full version