Originally published by Rob Allen at dzone.com
I recently set up a new WordPress-based website and local Docker-based development environment. This post documents what I did so that I can do it again next time! As I'm not in the WordPress world, many things are strange to me and I'm indebted to Jenny Wong for pointing me in the right direction on numerous occasions and being very patient with my questions! Thanks Jenny!
There's always ancillary files and directories in a project that aren't part of the actual website, so I have put the WordPress site in a subdirectory called app and then I have room for other stuff. My project's root directory looks like this:
$ tree . --dirsfirst -FL 1 . ├── app/ ├── bin/ ├── data/ ├── docker/ ├── README.md └── docker-compose.yml
This is what each item is for:
I put everything into git, with a .gitignore file to ignore everything in data along with various other WordPress files/directories that shouldn't be in version control.
A pair of Docker containers is used to run the site locally for development. I'm slowly getting my feet wet with Docker, so I'm not sure if this is the best way to do things. The docker-compose command allows you to spin up multiple containers in one go and join them together. This is done via the docker-compose.yml file.
Mine looks like this:
version: '3' services: db: image: mysql:5.7 ports: - 127.0.0.1:3306:3306 command: [ '--default_authentication_plugin=mysql_native_password', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci' ] volumes: - db_data:/var/lib/mysql environment: MYSQL_DATABASE: wordpress MYSQL_ROOT_PASSWORD: 123456 wp: build: context: ./docker dockerfile: Dockerfile-wp ports: - 127.0.0.1:80:80 volumes: - ./docker/php.conf.ini:/usr/local/etc/php/conf.d/conf.ini - ./app:/var/www/html depends_on: - db links: - db environment: DB_NAME: wordpress DB_USER: root DB_PASSWORD: 123456 DB_HOST: db WP_DEBUG: 1 LIVE_URL: https://project1.com DEV_URL: http://dev.project1.com volumes: db_data: # store database into a volume so that we can pause the containers
There are two containers: wp for the Apache/PHP and db for the MySQL.
The db container uses the default Docker MySQL container. I picked version 5.7, as that's what's running in my live environment. As this is a single-purpose development container, I just use the MySQL root user and set its password.
I want to persist the MySQL database between invocations of the container, so to do this, I create a volume called db_data and then map the /var/lib/mysqldirectory to that volume. I also expose MySQL on 3306 so that I can connect to it from my desktop.
FROM wordpress:php7.3-apache # Install xdebug RUN pecl install xdebug && docker-php-ext-enable xdebug # Install Less for WP-CLI RUN apt-get update && apt-get -y install less # Install WP-CLI RUN curl -s -o /usr/local/bin/wp \ https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar \ && chmod +x /usr/local/bin/wp
docker-compose will now create a container from our Dockerfile-wp and use that.
I map the docker/php.conf.ini into the container so that the PHP picks up our additional php.ini configuration settings:
upload_max_filesize = 10M post_max_size = 10M xdebug.overload_var_dump = 1 xdebug.remote_enable = 1 xdebug.remote_autostart = 0 xdebug.remote_connect_back = 0 xdebug.remote_host = host.docker.internal
Note that I'm on Mac, so the magic host.docker.internal domain name is available so that XDebug works. Incredibly, this hostname isn't available on Linux (yet?), so you'll have to find a workaround if you're on Linux.
For the wp container, I also set some environment variables for the WordPress configuration. I also set LIVE_URL and DEV_URL for use later when restoring a database dump file from live.
Set up a hostname pointing at 127.0.0.1 in your /etc/hosts for the dev site. I use dev.live-site.name personally.
Start up with:
$ docker-compose up
You can now go to http://dev.project1.com and start work. Put WordPress into app/, and the install process will start and create a database for you.
ctrl+c will stop them.
To rebuild the containers: docker-compose up --force-recreate --build
To delete the db_data volume: docker-compose down -v
There's a couple of obvious database operations that we need to perform: exporting the data to dump file and restoring a dump file. I've implemented a couple of scripts for these tasks in my bin directory.
Firstly, to export the database into a MySQL dump file from the db container:
#!/usr/bin/env bash this_dir=$(cd `dirname $0` && pwd) file="$this_dir/../data/dump.sql" # Create dump file cmd='exec mysqldump "$MYSQL_DATABASE" -uroot -p"$MYSQL_ROOT_PASSWORD"' docker-compose exec db sh -c "cmd" > $file # Remove password warning from the file sed -i '.bak' 1,1d $file && rm "$file.bak"
The container already has the database and password as environment variables, so we can avoid hard-coding them. As mysqldump gets upset about using the password on the command line nowadays, we remove its warning with sed.
The master database for my project is the live one and I want to use it on dev, so I have a script that retrieves a dump from live and puts it into the data directory. I then restore it using:
$ bin/restore-db live-dump.sql
This is the restore script:
#!/usr/bin/env bash file=$1 if [ -z "$file" ]; then echo "USAGE: restore-db <filename>" exit 1; fi # Restore database to db container cmd='exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE"' docker exec -i $(docker-compose ps -q db) sh -c "$cmd" < $file # Replace LIVE_URL using WP-CLI in wp container cmd='wp --allow-root search-replace "$LIVE_URL" "$DEV_URL" --skip-columns=guid' docker-compose exec wp sh -c "$cmd"
We have two operations in this script.
Firstly, we need to restore the database. This is complicated slightly by docker-compose exec not providing an input TTY, so we have to use docker exec and find the container from the compose name (db) with docker-compose ps -q db. Again, we take advantage of the environment variables so that we don't need to store the database name or password in the script.
Secondly, we need to replace all instances of the live domain name in the data (i.e. fully qualified links in posts and also config data) with the dev domain name. This is done with the WP-CLI command:
$ wp --allow-root search-replace $from $to --skip-columns=guid
We need --allow-root as our container runs with user root. $from and $to are our search and replace terms, and finally, we need to ensure we don't change any GUIDs using --skip-columns=guid. (If ever there should have been a default column to skip, guid was it!)
The end result is that we can restore our database and start working on the next feature.
WordPress uses a set of constants in the wp-config.php file for configuration. I updated my wp-config.php to look for environment variables in preference:
/** The name of the database for WordPress */ define('DB_NAME', $_SERVER['DB_NAME'] ?? $_ENV['DB_NAME'] ?? null);
/** MySQL database username */
define(‘DB_USER’, $_SERVER[‘DB_USER’] ?? $_ENV[‘DB_USER’] ?? null);
/** MySQL database password */
define(‘DB_PASSWORD’, $_SERVER[‘DB_PASSWORD’] ?? $_ENV[‘DB_PASSWORD’] ?? null);
/** MySQL hostname */
define(‘DB_HOST’, $_SERVER[‘DB_HOST’] ?? $_ENV[‘DB_HOST’] ?? null);
// … later …
define(‘WP_DEBUG’, (bool) ($ENV[‘WP_DEBUG’] ?? false));
define(‘WP_DEBUG_LOG’, (bool) ($ENV[‘WP_DEBUG’] ?? false));
The environment variables we set in docker-compose.yml are used here. My live server uses nginx and configures these using fastcgi_param which puts them into $_SERVER, so I check that superglobal too. There’s no need to do that for WP_DEBUG, as I never turned that on on live!
I’m not sure if this is best-practice for WordPress development, but it’s working for me so far. I particularly like that all the configuration changes that I need to make for different sites are all in docker-composer.yml which makes it reasonably easy to use this system for future projects too.
Originally published by Rob Allen at dzone.com
#web-development #docker #wordpress
Whether you want to develop a blog or you want a feature-rich, interactive WordPress website?
HourlyDeveloper.io is a distinguished leader in the WordPress development market. Hire WordPress Developer that develop easy-to-manage and high-performance WordPress websites that deliver the kind of results you have always wished for!
Consult with experts: https://bit.ly/3hiHIqj
#hire wordpress developer #wordpress #wordpress developer #wordpress development company #wordpress development services #wordpress development
Looking for a WordPress Development Company to revamp your website?
Hire Dedicated WordPress Developers from HourlyDeveloper.io, with absolute flexibility as per your business requirements such as hourly basis, monthly basis, part-time, full-time, or just according to their project requirements, with vast experience of WordPress projects of all sizes & industry verticals with complete client satisfaction.
Consult with experts:- https://bit.ly/2YMesRJ
#hire dedicated wordpress developer #wordpress development company #wordpress development services #wordpress development #wordpress developer #wordpress
One survey revealed that more than 70% of websites worldwide are based on WordPress and it is considered as the most trusted, easy to use, and a matured technology for building websites. With a huge library of plugins the work carried out on WordPress is particularly easy fast compared to other website development technologies.
Want to build a website on the most popular website development platform WordPress?
WebClues Infotech with its expertise in WordPress has developed more than 300+ websites on WordPress. With a skilled development team of 150+ members, WebClues Infotech has got what it takes to develop an elegant and high-performing website for you.
Want to find out more information about our WordPress development service?
Share your requirements https://www.webcluesinfotech.com/contact-us/
View Portfolio https://www.webcluesinfotech.com/portfolio/
#wordpress development company #top wordpress website development company in usa #wordpress website development #wordpress website development company #hire wordpress developer #hire wordpress developers
WordPress is used by many bloggers during the world which could be the explanation for some believe it to be the foremost prevalent blogging apparatus on the web. If you’re thinking of putting up a blog site but don’t know any coding languages (or even what code is) then your next best choice can be signing up for a WordPress website. you’ll easily register for a free WordPress blog account if you’re just starting and getting a for-pay WordPress blog account in a while.
WordPress is extremely user-friendly so beginners won’t find it hard to deal with fixing their new blog account. you simply got to give the proper information about yourself and choose the blog theme for your site from the list WordPress will offer you. There are various WordPress development companies within the market offering high-quality, fast, and all-inclusive WordPress services to clients. But, only some of them are genuine. So, it becomes difficult for a standard individual to decide on a competent WordPress development company for web development projects? Are you searching for an appropriate WordPress Development Company that would help you get the required WordPress site with all essential features? Skenix infotech is a prominent solution for you. Skenix Infotech is a leading IT company located in the USA & India and offers you all-inclusive WordPress Development Services at affordable prices and with quality to all needy individuals around the World.
Benefits of using WordPress for your website:
Since it’s starting in 2003, WordPress has been the foremost popular blogging platform within the market, and over the past few years, the platform is also getting appreciated as a content management software by non-blogging web platforms. Here are some reasons why WordPress is getting the most attention in very little time.
Search Engine Loves WordPress Sites:
WordPress is endued with a clean and easy coding behind it, by which the search engines don’t need to hustle much in reading and indexing any site’s content. Moreover, you’ll add meta tag keywords, descriptions, and titles for every page, post, and image, which can help in making precise search engine optimization.
The blog is Built-in and prepared to Go:
WordPress made its debut as a blogging platform, hence, it comes with some easy to use built-in blogging capabilities like fixing RSS / email subscriptions to your blog, commenting capability & automatically adding the foremost recent blog posts to other pages of the site are very simple while functioning on this platform.
Easy to use:
WordPress has been designed in an especially easy to use and intuitive interface. Adding blog posts, new pages, and pictures, etc regularly is simply as a piece of cake on this platform. The easiness of this platform reduces an excellent amount of your working time.
Have multiple users:
The administrator of a WordPress website also has the correct to set up multiple users for the website. Moreover, he/she also can assign different access levels and capabilities to each user.
Customizable Web Design:
Another advantage of the WordPress interface (Admin panel) is that it helps you to make any changes to the design and layout of the website, such as background image/color, display of sidebars, etc. without even using any FTP software.
No HTML Editing or FTP Software Required:
Since WordPress has its inbuilt editor, you’ll perform all the coding operations right from your website’s admin panel. No got to pay money for or use any external editing software like Dreamweaver or Notepad++. WordPress editor features a very friendly user-interface where you won’t even need to edit the code for performing various tasks.
Hire WordPress Developers from Skenix Infotech:
Hire dedicated, skilled, and highly experiences WordPress developer at affordable rates at Skenix Infotech which is the prominent WordPress Development Company in India and the USA, Click here to know about: Hire WordPress Developers
#wordpress development services #wordpress development company in india #hire wordpress developers #wordpress development company usa #wordpress web development #wordpress
More than 70% of Websites in the world use WordPress as the CMS. The simple fact gives us an overview of how mature and popular WordPress Technology is.
Want a website in WordPress for your business needs?
You couldn’t have been anywhere better. WebClues Infotech offers dedicated WordPress developers to the clients as per their business needs and most importantly at highly flexible competitive rates. After serving more than 600 clients we are ready to serve you with our knowledge and expertise
Develop a WordPress Website with WebClues Infotech!!
Share your requirements here https://www.webcluesinfotech.com/contact-us/
Book Free Interview with WordPress developer: https://bit.ly/3dDShFg
#hire wordpress experts #hire wordpress developers #hire the best freelance wordpress developers #hire wordpress developers #hire wordpress developers in 2021 #hire dedicated wordpress developers