Dockerizing WordPress

20
Dockerizing WordPress

description

 

Transcript of Dockerizing WordPress

Page 1: Dockerizing WordPress

Dockerizing WordPress

Page 2: Dockerizing WordPress

Requirements• In order to complete this tutorial, you should make sure that you

have docker installed. For more information about installation please visit the Docker website: http://docs.docker.io/en/latest/

• You should also be familiar with the dockerfile instructions covered in the dockerfile tutorial: http://www.docker.io/learn/dockerfile/

• In addition to WordPress, we first need to install Apache, MySQL and PHP inside our container. To keep this presentation straightforward all these different software will be install in one single docker container

Page 3: Dockerizing WordPress

• Throughout this tutorial I use docker with Vagrant and a VirtualBox VM on a Mac computer. To get Docker and WordPress running, the first step is to edit our Vagrantfile with the line in red here below:

# Setup virtual machine box. This VM configuration code is always executed. config.vm.box = BOX_NAME config.vm.box_url = BOX_URI config.vm.forward_port 80, 8880

Computers-MacBook-Air:~ communityPC$ cd dockerComputers-MacBook-Air:docker communityPC$ emacs Vagrantfile

Page 4: Dockerizing WordPress

• Now that we have specified in our Vagrantfile that accessing "localhost:8080" will access port 80 on the guest machine, we can start the VM. We see that our change here below in red has been taken into consideration

Computers-MacBook-Air:docker communityPC$ vagrant upBringing machine 'default' up with 'virtualbox' provider...[default] Setting the name of the VM...[default] Clearing any previously set forwarded ports...[default] Creating shared folders metadata...[default] Clearing any previously set network interfaces...[default] Preparing network interfaces based on configuration...[default] Forwarding ports...[default] -- 22 => 2222 (adapter 1)[default] -- 80 => 8880 (adapter 1)[default] Booting VM...[default] Waiting for VM to boot. This can take a few minutes.[default] VM booted and ready for use![default] Mounting shared folders...[default] -- /vagrant

Page 5: Dockerizing WordPress

• The following command will SSH into our running VM and give us access to a shell

• At this point it is important to become “root” to have permission to start a container

Computers-MacBook-Air:docker communityPC$ vagrant sshWelcome to Ubuntu 12.04 LTS (GNU/Linux 3.8.0-29-generic x86_64)

* Documentation: https://help.ubuntu.com/Welcome to your Vagrant-built virtual machine.Last login: Fri Sep 20 20:56:40 2013 from 10.0.2.2

vagrant@precise64:~$ sudo suroot@precise64:/home/vagrant#

Page 6: Dockerizing WordPress

• Lets create a new directory to proceed with our WordPress installation

• We are now ready to open emacs and build our Dockerfile

root@precise64:/home/vagrant# mkdir wordpressroot@precise64:/home/vagrant# cd wordpressroot@precise64:/home/vagrant/wordpress#

root@precise64:/home/vagrant/wordpress# emacs Dockerfile

Page 7: Dockerizing WordPress

# Install LAMP stack and Wordpress

# use the latest ubuntu imageFROM ubuntu:12.04

MAINTAINER: Victor Coisne “[email protected]"

# make sure the package repository is up to dateRUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.listRUN apt-get update

# install apache, mysql, php, emacs, wget and wordpressRUN apt-get install -y mysql-server mysql-client

RUN apt-get install -y apache2 apache2-mpm-prefork apache2-utils apache2.2-common libapache2-mod-php5 libapr1 libaprutil1 libdbd-mysql-perl libdbi-perl libnet-daemon-perl libplrpc-perl libpq5 mysql-common php5-common php5-mysql

RUN apt-get install -y emacs23 wget

# download the lastest version of wordpress and move the sources to the default apache serveur host /var/wwwRUN wget http://wordpress.org/latest.tar.gz && mv latest.tar.gz /var/www

# Setting ports to be publicly exposed when running the image (Wordpress uses ports 80) EXPOSE 80

• So this is how our finished Dockerfile looks like. Note that the lines starting with # are comments that give explanation about what will happen when building a docker container from that Dockerfile

If you are not comfortable with the Dockerfile syntax, follow this link http://www.docker.io/learn/dockerfile/

Page 8: Dockerizing WordPress

• It is now time to build our container from the current directory and use the option -t to give it a name: wordpresstuto

• Now that we have built our container lets run it !

• –p 80:80 tells docker to forward port 80 from the container to the VM and connect to that VM on the same port 80. Since we are running the host OS inside Vagrant, the Vagrantfile that we have modified will forward that back to port 8880 on our main system.

root@precise64:/home/vagrant/wordpress# docker build -t wordpresstuto .

root@precise64:/home/vagrant/wordpress# docker run -i -t -p 80:80 wordpress_tuto /bin/bashroot@bd4b8fdbd37f:/#

Page 9: Dockerizing WordPress

• Note that port 80 should not be already in use. You can enter the following command to be sure that there are no other processes listening upon port 80:

• As you can see there are no processes listening upon port 80. If there are any, you first have to kill them if you want to run your docker container and forward port 80 from the container to the host.

Root@precise64:/home/vagrant/wordpress# lsof -Pni4 | grep LISTENrpcbind 680 root 8u IPv4 948 0t0 TCP *:111 (LISTEN)rpc.statd 726 statd 9u IPv4 8180 0t0 TCP *:48561 (LISTEN)sshd 761 root 3u IPv4 9607 0t0 TCP *:22 (LISTEN)memcached 930 memcache 26u IPv4 9989 0t0 TCP 127.0.0.1:11211 (LISTEN)dnsmasq 962 lxc-dnsmasq 7u IPv4 10007 0t0 TCP 10.0.3.1:53 (LISTEN)

.

Page 10: Dockerizing WordPress

• Lets now go to the directory /var/www that is the location of the website files

• Now that we are in the /var/www we can unzip the latest version of Wordpress downloaded in our dockerfile and found on http://codex.wordpress.org/UNIX_Shell_Skills

• The following command allow us to edit Apache’s default configuration file

root@bd4b8fdbd37f:/var/www# tar zxf latest.tar.gz

root@bd4b8fdbd37f:/var/www# emacs /etc/apache2/sites-enabled/000-default

root@bd4b8fdbd37f:/# cd /var/wwwroot@bd4b8fdbd37f:/var/www#

Page 11: Dockerizing WordPress

• Inside that file we just have to change the Documentroot and Directory lines as you can see here below in red

File Edit Options Buffers Tools Help<VirtualHost *:80> ServerAdmin webmaster@localhost

DocumentRoot /var/www/wordpress <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/wordpress/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all </Directory>

Page 12: Dockerizing WordPress

• First let’s start the MySQL database

• Let’s now start the Apache2 service

• It is now time to change directory to edit the Wordpress configuration file

M

root@bd4b8fdbd37f:/var/www# /usr/sbin/mysqld &

root@bd4b8fdbd37f:/var/www# /etc/init.d/apache2 start

root@bd4b8fdbd37f:/var/www# cd wordpress

Page 13: Dockerizing WordPress

• At this point, if we go to the following url in our web browser: http://localhost:8880/ we see the following message

Page 14: Dockerizing WordPress

• As a result we just have to move our config-sample.php file to a newly created wp-config.php file that Wordpress needed to get started

• Then we log in the mysql monitor as a “root” user

root@bd4b8fdbd37f:/var/www/wordpress# mv wp-config-sample.php wp-config.php

root@bd4b8fdbd37f:/var/www/wordpress# mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 7Server version: 5.5.22-0ubuntu1 (Ubuntu)

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Page 15: Dockerizing WordPress

• Let’s start by creating the database we need to run WordPress and give it a name

• It is now time to create a new user name and password prior to grant permissions to that user for the specific database we have just created. Once this is done we simply reload all the privileges and exit the MySQL shell

• Lets now edit the configuration file using emacs

mysql> create database victor_wordpress ;Query OK, 1 row affected (0.00 sec)

mysql> CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password’ ;mysql> GRANT ALL PRIVILEGES ON victor_wordpress.* TO 'newuser’ @'localhost’ ;mysql> FLUSH PRIVILEGES ;mysql> exitBye

root@bd4b8fdbd37f:/var/www/wordpress# emacs wp-config.php

Page 16: Dockerizing WordPress

//** The name of the database for WordPress */define('DB_NAME', 'wordpress_victor');

//** MySQL database username */define('DB_USER', 'newuser');

//** MySQL database password */define('DB_PASSWORD', 'password');

//** MySQL hostname */define('DB_HOST', 'localhost');

Now that we are inside the configuration file, lets edit the mysql settings with the information previously defined in red here below: database name, username and password

Page 17: Dockerizing WordPress

• Now enter the following url in your favorite web browser: http://localhost:8880 Voilà ! You are now ready to start the Wordpress installation process.

• Wait there is one more thing to do …

Page 18: Dockerizing WordPress

• Don’t forget to share your work with the Docker community. To do so we can push your container on the Docker index to store the filesystem state and make it available for re-use.

• In order to push it on the Docker index, you first have to sign up: https://index.docker.io/account/signup/

• So we first have to exit our container and go back to our host

root@bd4b8fdbd37f:/var/www/wordpress# exitexitThere are stopped jobs.root@bd4b8fdbd37f:/var/www/wordpress# exitExitroot@precise64:/home/vagrant/wordpress#

Page 19: Dockerizing WordPress

• We can use the following Docker command to find the ID of our image (it should be the first one of the list)

• Now that we have the ID we can commit the changes we have made to the image and tag it using your username from the Docker index / the name of your image. Our image is now ready to be pushed to the docker index !

• You can read more about Docker and WordPress here: Slumlord hosting with Docker - WordPress Container

root@precise64:/home/vagrant/wordpress# docker commit bd4b8fdbd37f -t vcoisne/wordpresstuto93ed51f82520root@precise64:/home/vagrant/wordpress# docker push vcoisne/wordpresstuto

root@precise64:/home/vagrant# docker ps -aID IMAGE COMMAND CREATED STATUS bd4b8fdbd37f wordpresstuto:latest /bin/bash 11 minutes ago Exit 0