Dockerizing WordPress


Published on

Dockerizing WordPress

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Explain that port 80 should not be already in use otherwiselsof -Pni4 | grep LISTEN
  • Dockerizing WordPress

    1. 1. Dockerizing WordPress
    2. 2. 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: • You should also be familiar with the dockerfile instructions covered in the dockerfile tutorial: • 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
    3. 3. • 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: Computers-MacBook-Air:~ communityPC$ cd docker Computers-MacBook-Air:docker communityPC$ emacs Vagrantfile # Setup virtual machine box. This VM configuration code is always executed. = BOX_NAME config.vm.box_url = BOX_URI config.vm.forward_port 80, 8880
    4. 4. • 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 up Bringing 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
    5. 5. • The following command will SSH into our running VM and give us access to a shell Computers-MacBook-Air:docker communityPC$ vagrant ssh Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.8.0-29-generic x86_64) * Documentation: Welcome to your Vagrant-built virtual machine. Last login: Fri Sep 20 20:56:40 2013 from • At this point it is important to become “root” to have permission to start a container vagrant@precise64:~$ sudo su root@precise64:/home/vagrant#
    6. 6. • Lets create a new directory to proceed with our WordPress installation root@precise64:/home/vagrant# mkdir wordpress root@precise64:/home/vagrant# cd wordpress root@precise64:/home/vagrant/wordpress# • We are now ready to open emacs and build our Dockerfile root@precise64:/home/vagrant/wordpress# emacs Dockerfile
    7. 7. • 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 # Install LAMP stack and Wordpress # use the latest ubuntu image FROM ubuntu:12.04 MAINTAINER: Victor Coisne “" # make sure the package repository is up to date RUN echo "deb precise main universe" > /etc/apt/sources.list RUN apt-get update # install apache, mysql, php, emacs, wget and wordpress RUN 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/www RUN wget && mv latest.tar.gz /var/www # Setting ports to be publicly exposed when running the image (Wordpress uses ports 80) EXPOSE 80 If you are not comfortable with the Dockerfile syntax, follow this link
    8. 8. • It is now time to build our container from the current directory and use the option -t to give it a name: wordpresstuto root@precise64:/home/vagrant/wordpress# docker build -t wordpresstuto . • Now that we have built our container lets run it ! root@precise64:/home/vagrant/wordpress# docker run -i -t -p 80:80 wordpress_tuto /bin/bash root@bd4b8fdbd37f:/# • –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.
    9. 9. • 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: Root@precise64:/home/vagrant/wordpress# lsof -Pni4 | grep LISTEN rpcbind 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 (LISTEN) dnsmasq 962 lxc-dnsmasq 7u IPv4 10007 0t0 TCP (LISTEN) . • 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.
    10. 10. • Lets now go to the directory /var/www that is the location of the website files root@bd4b8fdbd37f:/# cd /var/www root@bd4b8fdbd37f:/var/www# • Now that we are in the /var/www we can unzip the latest version of Wordpress downloaded in our dockerfile and found on root@bd4b8fdbd37f:/var/www# tar zxf latest.tar.gz • The following command allow us to edit Apache’s default configuration file root@bd4b8fdbd37f:/var/www# emacs /etc/apache2/sites-enabled/000-default
    11. 11. • 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>
    12. 12. • MFirst let’s start the MySQL database root@bd4b8fdbd37f:/var/www# /usr/sbin/mysqld & • Let’s now start the Apache2 service root@bd4b8fdbd37f:/var/www# /etc/init.d/apache2 start • It is now time to change directory to edit the Wordpress configuration file root@bd4b8fdbd37f:/var/www# cd wordpress
    13. 13. • At this point, if we go to the following url in our web browser: http://localhost:8880/ we see the following message
    14. 14. • 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 root@bd4b8fdbd37f:/var/www/wordpress# mv wp-config-sample.php wpconfig.php • Then we log in the mysql monitor as a “root” user root@bd4b8fdbd37f:/var/www/wordpress# mysql -u root Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 7 Server 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>
    15. 15. • Let’s start by creating the database we need to run WordPress and give it a name mysql> create database victor_wordpress ; Query OK, 1 row affected (0.00 sec) • 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 mysql> CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password’ ; mysql> GRANT ALL PRIVILEGES ON victor_wordpress.* TO 'newuser’ @'localhost’ ; mysql> FLUSH PRIVILEGES ; mysql> exit Bye • Lets now edit the configuration file using emacs root@bd4b8fdbd37f:/var/www/wordpress# emacs wp-config.php
    16. 16. 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 //** 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');
    17. 17. • 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 …
    18. 18. • 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: • So we first have to exit our container and go back to our host root@bd4b8fdbd37f:/var/www/wordpress# exit exit There are stopped jobs. root@bd4b8fdbd37f:/var/www/wordpress# exit Exit root@precise64:/home/vagrant/wordpress#
    19. 19. • We can use the following Docker command to find the ID of our image (it should be the first one of the list) root@precise64:/home/vagrant# docker ps -a ID IMAGE COMMAND bd4b8fdbd37f wordpresstuto:latest /bin/bash CREATED STATUS 11 minutes ago Exit 0 • 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 ! root@precise64:/home/vagrant/wordpress# docker commit bd4b8fdbd37f -t vcoisne/wordpresstuto 93ed51f82520 root@precise64:/home/vagrant/wordpress# docker push vcoisne/wordpresstuto • You can read more about Docker and WordPress here: Slumlord hosting with Docker - WordPress Container
    20. 20. Want to learn more ?!forum /docker-user 108146856671494713993