This article mainly introduces how to set up a complete WordPress site in Docker. Docker is the most popular virtual machine technology. For more information, see
This article mainly introduces how to set up a complete WordPress site in Docker. Docker is the most popular virtual machine technology. For more information, see
1. Install Docker
Before we start, we need to ensure that Docker has been installed on our Linux machine. The host we use is CentOS 7, so we use the following command to install docker using the yum manager.
# Yum install docker
# Systemctl restart docker. service
2. Create Dockerfile for WordPress
We need to create a Dockerfile for automatically installing wordpress and its pre-requirements. This Dockerfile will be used to build the WordPress installation image. This WordPress Dockerfile will get the CentOS 7 image from the Docker Registry Hub and upgrade the system with the latest available updates. It then installs necessary software, such as the Nginx Web server, PHP, MariaDB, and Open SSH server, and other components that ensure the normal operation of Docker containers. At last, it executes a script to initialize WordPress installation.
# Nano Dockerfile
Then, we need to add the following configuration lines to the Dockerfile.
FROM centos: centos7 MAINTAINER The CentOS Project RUN yum-y update; yum clean all RUN yum-y install epel-release; yum clean all RUN yum-y install mariadb-server mariadb-client nginx php-fpm php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-magickwand php-magpierss php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy php-apc pwgen python-setuptools curl git tar; yum clean all ADD. /start. sh/start. sh ADD. /nginx-site.conf/nginx. conf RUN mv/nginx. conf/etc/nginx. conf RUN rm-rf/usr/share/nginx/html/* RUN/usr/bin/easy_install supervisor-stdout ADD. /supervisord. conf/etc/supervisord. conf RUN echo % sudo ALL = NOPASSWD: ALL>/etc/sudoers ADD/wordpress.tar.gz RUN tar xvzf/wordpress.tar.gz RUN mv/wordpress/*/usr/share/nginx/html /. RUN chown-R apache: apache/usr/share/nginx/RUN chmod 755/start. sh RUN mkdir/var/run/sshd EXPOSE 80 EXPOSE 22 CMD ["/bin/bash", "/start. sh "]
3. Create a STARTUP script
After Dockerfile is created, we need to create a script for running and configuring WordPress installation, named start. sh. It creates and configures the database and password for WordPress. Open start. sh in our favorite text editor.
# Nano start. sh
After start. sh is enabled, add the following configuration line to the file.
#! /Bin/bash _ check () {if [-f/usr/share/nginx/html/wp-config.php]; then exit fi} _ create_user () {# create user SSH_USERPASS = 'pwgen-c-n-1 8' useradd-G wheel user echo user: $ SSH_USERPASS | chpasswd echo SSH user password: $ SSH_USERPASS }__ mysql_config () {# enable and run MySQL yum-y erase mariadb-server rm-rf/var/lib/mysql // etc/my. cnf yum-y install mariadb-server mysql_insta Ll_db chown-R mysql: mysql/var/lib/mysql/usr/bin/mysqld_safe & sleep 10 }__ handle_passwords () {# Here we generate a random password (thanks to pwgen ). The first two random keys for the mysql user and the last for the wp-config.php. WORDPRESS_DB = "wordpress" MYSQL_PASSWORD = 'pwgen-c-n-1 12 'WORDPRESS_PASSWORD = 'pwgen-c-n-1 12' # This is the password displayed in the log. Echo mysql root password: $ MYSQL_PASSWORD echo wordpress password: $ WORDPRESS_PASSWORD echo $ MYSQL_PASSWORD>/mysql-root-pw.txt echo $ WORDPRESS_PASSWORD>/wordpress-db-pw.txt # It turns out to be a long line including sed, cat, pipe, and stuff, but thanks to the # @ djfiander https://gist.github.com/djfiander/6141138 # Sed-e "s/database_name_here/$ WORDPRESS_DB/s/username_here/$ WORDPRESS_DB/s/password_here/$ WORDPRESS_PASSWORD/'auth _ key'/s/put your unique phrase here/'pwgen-c-n-1 65' // 'Secure _ AUTH_KEY '/s/put your unique phrase here/'pwgen-c-n-1 65' // 'logged _ IN_KEY '/s/put your unique phrase here/'pwgen-c-n-1 65' // 'nonce _ key'/s/put your unique phrase here/'pwgen-c-n-1 65' // 'auth _ salt'/s/put your unique phrase here/'pwgen-c-n-1 65 '// 'Secure _ AUTH_SALT '/s/put your unique phrase here/'pwgen-c-n-1 65' // 'logged _ IN_SALT'/s/put your unique phrase here/ 'pwgen-c-n-1 65' // 'nonce _ salt'/s/put your unique phrase here/'pwgen-c-n-1 65'/"/usr /share/nginx/html/wp-config-sample.php>/usr/share/nginx/html/wp-config.php} _ httpd_perms () {chown apache: apache/usr/share/nginx/html/wp-config.php} _ start_mysql () {# systemctl start mysqld service mysqladmin-u root password $ MYSQL_PASSWORD mysql-uroot-p $ MYSQL_PASSWORD-e "create database wordpress; grant all privileges on wordpress. * TO 'wordpress '@ 'localhost' identified by' $ WORDPRESS_PASSWORD '; flush privileges; "killall mysqld sleep 10 }__ run_supervisor () {supervisord-n} # Call all functions _ check _ create_user _ mysql_config _ handle_passwords _ httpd_perms _ start_mysql _ run_supervisor
After adding the above configuration, save and close the file.
4. Create a configuration file
Now we need to create a configuration file for the Nginx Web server named nginx-site.conf.
# Nano nginx-site.conf.
Then, add the following configuration information to the configuration file.