May 22, 2021 Docker From entry to practice
Fig makes it easy to run Wordpress in a stand-alone environment. tall Fig, and then download Wordpress to the current directory:
wordpress.org/latest.tar.gz | tar -xvzf -
This will create a directory called wordpress, and you can rename it to the name you want.
In the directory, create a
Dockerfile
that defines the environment in which your app runs:
FROM orchardup/php5
ADD . /code
The above tells Docker to create a mirror that contains PHP and Wordpress. For more information on how to write a Dockerfile file, you can view Mirror Creation and Dockerfile for use.
Next, the
fig.yml
opens a web service and a separate MySQL instance:
web:
build: .
command: php -S 0.0.0.0:8000 -t /code
ports:
- "8000:8000"
links:
- db
volumes:
- .:/code
db:
image: orchardup/mysql
environment:
MYSQL_DATABASE: wordpress
Two more files are needed to get the app up and running.
The first,
wp-config.php
is a standard Wordpress profile that needs to be modified to point the configuration of the database to
db
container.
<?php
define('DB_NAME', 'wordpress');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', "db:3306");
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
define('AUTH_KEY', 'put your unique phrase here');
define('SECURE_AUTH_KEY', 'put your unique phrase here');
define('LOGGED_IN_KEY', 'put your unique phrase here');
define('NONCE_KEY', 'put your unique phrase here');
define('AUTH_SALT', 'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT', 'put your unique phrase here');
define('NONCE_SALT', 'put your unique phrase here');
$table_prefix = 'wp_';
define('WPLANG', '');
define('WP_DEBUG', false);
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
require_once(ABSPATH . 'wp-settings.php');
The second,
router.php
tells PHP's built-in server how to run Wordpress:
<?php
$root = $_SERVER['DOCUMENT_ROOT'];
chdir($root);
$path = '/'.ltrim(parse_url($_SERVER['REQUEST_URI'])['path'],'/');
set_include_path(get_include_path().':'.__DIR__);
if(file_exists($root.$path))
{
if(is_dir($root.$path) && substr($path,strlen($path) - 1, 1) !== '/')
$path = rtrim($path,'/').'/index.php';
if(strpos($path,'.php') === false) return false;
else {
chdir(dirname($root.$path));
require_once $root.$path;
}
}else include_once 'index.php';
When these profiles are ready, execute the fig up instruction in your Wordpress
fig up
and Fig pulls the image and creates the image we need, starting the web and database containers. T
hen visit the 8000 port where the docker daemon listens and you'll be able to see your Wordpress website.
(If you have to use boot2docker, execute
boot2docker ip
you'll see its address).