Docker compose ngon cành đào cho Wordpress PhpMyAdmin
13th Mar 2022We’ll start of with creating a compose file. The first service we want to add is the database:
version: '2' services: db: image: mysql:5.7 restart: always volumes: - db_data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: p4ssw0rd! networks: - back networks: back: volumes: db_data:
We use the (currently) latest version of MySQL, 5.7, but you could swap that out with MariaDB if that’s your preference. Data flowing into this database will be persisted to a named volume db_data, so that even when I remove the container, the data will still live somewhere on my machine and can be mounted again in a new container. We also connect this service to an overlay network back, so we can connect future containers to it.
Now, I didn’t publish the service on any port. I will, in fact, add another service that will serve as our management interface of the database, phpmyadmin:
version: '2' services: db: image: mysql:5.7 restart: always volumes: - db_data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: p4ssw0rd! networks: - back phpmyadmin: depends_on: - db image: phpmyadmin/phpmyadmin restart: always ports: - 8080:80 environment: PMA_HOST: db MYSQL_ROOT_PASSWORD: p4ssw0rd! networks: - back networks: back: volumes: db_data:
The phpmyadmin will connect to the db database service over the backnetwork and serve on port 8080. A quick docker-compose up -d will bring everything up and if you point your browser to localhost:8080 you will be greeted by the phpmyadmin welcome screen to which you can login with the root user and the password defined in the compose file.
Now comes the money piece of our entire setup, the WordPress service:
version: '2' services: wordpress: depends_on: - db image: wordpress:4.6 restart: always volumes: - ./wp-content:/var/www/html/wp-content environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_PASSWORD: p4ssw0rd! ports: - 80:80 - 443:443 networks: - back db: image: mysql:5.7 restart: always volumes: - db_data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: p4ssw0rd! networks: - back phpmyadmin: depends_on: - db image: phpmyadmin/phpmyadmin restart: always ports: - 8080:80 environment: PMA_HOST: db MYSQL_ROOT_PASSWORD: p4ssw0rd! networks: - back networks: back: volumes: db_data:
We’ll run from version 4.6 and since I like to keep my installations clean, I’ll only mount the `wp-content` folder, containing the themes and plugins, on my local machine into the container. The other files in the base wordpress installation will be provided by the container itself.
Time for a docker-compose up -d again, that will kick off the wordpress service. And when you keep a close eye on your local filesystem, you’ll see that after the docker images have been pulled in, the wp-content folder will be created, and filled with files and folders. This is the base filesystem building up by the wordpress installer. After a few seconds everything will be installed. Open localhost in the browser to see the install screen asking you to configure a few parameters.
WordPress installation screen
Now you can go ahead and make changes in the wp-admin interface or in the filesystem, installing plugins and changing themes, and that will have immediate effect on the wp-contentfolder on your local machine. When you are ready you can commit your changes to your coding repository.
There is one final thing left, though. Because our admin dashboard is alerting us for a new WordPress version, so we’ll have to update. As good citizens and Docker users, we’ll just swap the image version to the newest one and restart:
version: '2' services: wordpress: depends_on: - db image: wordpress:latest restart: always volumes: - ./wp-content:/var/www/html/wp-content environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_PASSWORD: p4ssw0rd! ports: - 80:80 - 443:443 networks: - back db: image: mysql:5.7 restart: always volumes: - db_data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: p4ssw0rd! networks: - back phpmyadmin: depends_on: - db image: phpmyadmin/phpmyadmin restart: always ports: - 8080:80 environment: PMA_HOST: db MYSQL_ROOT_PASSWORD: p4ssw0rd! networks: - back networks: back: volumes: db_data:
Unfortunately, that’s not enough as this comment in the official repo is pointing out. Not only the container needs to be updated, but we also need to fire off an update from the admin interface in the Updates tab. After all the update tasks have been executed, our WordPress installation is fully up to date!
Add new comment