Docker Tutorial - Setup Development Environment with Docker

Docker Tutorial

If your host machine is not a linux-based OS, please download VirtualBox and Vagrant to setup a Virtual Linux Host OS.

This guide will be used to install and config docker on Macbook with a Virtual Linux Host OS of Ubuntu/trusty (VirtualBox+Vagrant). I suggest to use iTerm for terminal replacement.

Setup Virtual Machine

  • Step 1: Install Virtual Box - version 4.3.30 (https://www.virtualbox.org/wiki/Download_Old_Builds_4_3)

  • Step 2: Install Vagrant 1.7.4 (https://www.vagrantup.com/downloads.html)

  • Step 3: Create a folder for virtual machines
    mkdir ~/VMS
    mkdir ~/VMS/ubuntu
    
  • Step 4: Create an Ubuntu virtual machine
cd VMS/ubuntu
vagrant init ubuntu/trusty64; vagrant up --provider virtualbox
  • Step 5: Prepare folder structure
Source code on my laptop: ~/Source

DevOps on my laptop: ~/DevOps (please clone the source from https://github.com/nganhtuan63/DevOps)

Virtual Machines on my laptop: ~/VMS
  • Step 6: Edit the Vagrantfile to config virtual machine

Setup the IP Address for the guest VM. I will use the address 192.168.33.99

# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network "private_network", ip: "192.168.33.99"

Share additional folders to the guest VM

config.vm.synced_folder "~/Source", "/var/www/html", :mount_options => ['dmode=777', 'fmode=777']
config.vm.synced_folder "~/DevOps", "/Devops"

Increase memory for guest VM for 3GB Ram

config.vm.provider "virtualbox" do |vb|
  vb.memory = "3072"
end

Reload vagrant to apply new config

vagrant reload

Setup Docker on guest VM

  • Step 1: Log in to the guest VM and switch to root user
cd ~/VMS/ubuntu
vagrant up
vagrant ssh

When you are inside the virtual machine, switch to root user

sudo -s
  • Step 2: Install Docker (https://docs.docker.com/installation/ubuntulinux/)
apt-get update
apt-get install curl
curl -sSL https://get.docker.com/ | sh

Check docker is installed correctly by typing

sudo docker run hello-world
  • Step 3: Install Docker Composer
curl -L https://github.com/docker/compose/releases/download/1.4.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

Use docker to create containers

  • Step 1: Build a custom phpcontainer images (This step is an example to demonstrate the steps of building an image from a Dockerfile)
cd /DevOps/docker/images/phpcomposer
docker build -t phpcomposer:5.6 .

Please remember to add the period . at the end of the command docker build -t phpcomposer:5.6 .

Check to see the newly created image

docker images

alt text

You will see the new phpcomposer image has been created on the top of the image list like the screenshot above

  • Step 2: Create all containers with docker compose

If you use iTerm as your terminal, open a new screen, and log in to the guest VM. Then execute these commands when you are inside the guest VM

cd /DevOps/docker
/usr/local/bin/docker-compose up

You will see a screen like this which means all containers have been created succesfully

alt text

Remember : Do not turn off the current screen, you need to open a new termnial screen to work. Otherwise, you will stop all running containers by turning off the screen.

You need to open new screen like this and you will work on the left screen

alt text

  • Step 3: Check running containers
docker ps -a

Here are the running containers. Containers with “UP xxx” status means they are running

alt text

Test, Config and Connect to running Docker Containers

###Test PHP and Nginx

You can add php files on guest VM (Host OS of containers) at: /var/www/html. The php and nginx containers will found it at /usr/share/nginx/html/ inside them

On the (Host OS of containers), create new php file:

vi /var/www/html/index.php

Add index.php content. Here is an example:

<?php
echo "Welcome to Docker World! \n";

Test it by send a curl command to http://127.0.0.1:80. This port is linked directly to the web container from guest VM (Host OS of containers)

curl http://127.0.0.1:80

You will see the result from php file content like this:

alt text

###Config for PHP and Nginx

You can update php-fpm.conf of php container by updating this file /DevOps/conf/php-fpm/php-fpm.conf

You can update/add new nginx .conf files by updating content of this folder /DevOps/conf/nginx/

###Test and Connect to MySQL or MariaDB**:

MySQL links to guest VM (Host OS of containers) at port: 3306

MariaDB links to guest VM (Host OS of containers) at port: 3307

You can login to mysql by using this information:

mysql -uadmin -padmin123 -P 3306 -h127.0.0.1

You can login to mysql by using this information:

mysql -uadmin -padmin123 -P 3307 -h127.0.0.1

MySQL user information has been declard in env files at mariadb.env, mysqldb.env

###Connect to MySQL/MariaDB from Sequel Pro on Laptop

Open Sequel Pro and add new connection

Name: MySQL/MariaDB Dev Container
MySQL Host: 127.0.0.1
Username: admin
Password: admin123
Database:
Port: 3306 or 3307 (based on your choice)

SSH Host: 192.168.33.99
SSH User: vagrant
SSH Key: ~/.vagrant.d/insecure_private_key

alt text

Save the connection and connect to your database server.

That’s it. Happy coding and Feel free to contact me.

Written on August 20, 2015