Introduction to Dockerfiles

In this tutorial we will walk through the process of creating a wordpress docker container using a Dockerfile to build the container. We will also cover quick tidbits on the Dockerhub, quay.io, maintaining your Dockerfiles, Dockerhub CI, and customizing your docker images.


Click Here for the full Step By Step Text Tutorial

Step by Step Guide:
———————

1. SSH into your CoreOS server or open the boot2docker command shell.

2. cd into a directory where you will store and build your Dockerfile project.

3. Create a Dockerfile to build WordPress

#########################
# WordPress installation
#########################

# Set the base image for this installation
FROM appcontainers/centos65

# File Author / Maintainer
MAINTAINER dockcore rnason@dockcore.com

# Pre-reqs
RUN yum clean all && \
yum -y update && \
yum -y install httpd mod_rewrite mod_ssl mod_env php php-common php-cli php-mysql mysql-server unzip && \
rm -fr /var/cache/*

# Files that need to be added
ADD mysql_setup.sql /tmp/
ADD wordpress.conf /etc/httpd/conf.d/

# Application Install
RUN wget -P /var/www/html/ https://wordpress.org/latest.zip && \
unzip /var/www/html/latest.zip -d /var/www/html/ && \
rm -fr /var/www/html/latest.zip

# Run Mysql install
RUN service mysqld start && \
mysql < /tmp/mysql_setup.sql && \
rm -fr /tmp/mysql_setup.sql* && \
service mysqld stop

# Copy the WP-config file
RUN cp /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php

# Edit the wp-config file
RUN sed -ie 's/database_name_here/wordpress/g' /var/www/html/wordpress/wp-config.php && \
sed -ie 's/username_here/wpuser/g' /var/www/html/wordpress/wp-config.php && \
sed -ie 's/password_here/P@ssword/g' /var/www/html/wordpress/wp-config.php

# Set Permissions
RUN chown -R apache:apache /var/www/html/wordpress && \
chmod -R 775 /var/www/html/wordpress

# Start mysql and apache on boot
RUN echo "service mysqld start" >> ~/.bashrc && \
echo "service httpd start" >> ~/.bashrc

# Expose Necessary Ports
EXPOSE 80

4. Save the file and create another file in the same directory called mysql_setup.sql

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('P@ssw0rd');

CREATE DATABASE wordpress;

GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost' IDENTIFIED BY 'P@ssword';

DROP DATABASE test;

5. Save the mysql install file, and create one more file called wordpress.conf, this will be the apache config file that apache uses to server the wordpress instance.

<VirtualHost *:80>
        DocumentRoot "/var/www/html/wordpress/"
        ServerName wordpress.local
        ServerAlias www.wordpress.local
        ErrorLog "/var/log/httpd/wordpress_error.log"
        CustomLog "/var/log/httpd/wordpress_access.log" combined
</VirtualHost>

6. Build the image

docker build -t yournamespace/wordpress .

7. Once the container builds successfully, then run the container

docker run -it --name wordpress -h wordpress -p 80:80 yournamespace/wordpress /bin/bash

* -d can be used after the -it to run the container as a daemon, this will not connect you to the console, however you can attach to the container at any time using docker attach

8. Exit the container by hitting CTL P, CTL Q. Typing exit will stop the container.

9. Use the boot2docker command to get the boot2docker IP and then connect to that IP from your browser

boot2docker ip

0 comments