Skip to content

Commit

Permalink
docker tutorials: docker, docker-compose, ansible
Browse files Browse the repository at this point in the history
  • Loading branch information
darkn3rd committed Sep 17, 2018
1 parent bcd0546 commit b69db0a
Show file tree
Hide file tree
Showing 14 changed files with 283 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ These are code related to tutorials created.

## Sie Blogs

* Docker
* Docker Command vs Ansible - https://medium.com/@Joachim8675309/docker-using-shell-or-docker-compose-4ab8ea8ae801
* Docker Command vs. Docker Compose - https://medium.com/@Joachim8675309/docker-using-shell-or-ansible-7cdceb646d3
* Virtual Box
* Windows
* Windows 8.1 - https://medium.com/@Joachim8675309/virtualbox-and-friends-on-windows-8-1-3c691460698f
Expand Down
5 changes: 5 additions & 0 deletions docker-ansible/Brewfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cask_args appdir: '/Applications'
cask 'virtualbox'
cask 'virtualbox-extension-pack'
cask 'vagrant'
cask 'docker-toolbox'
35 changes: 35 additions & 0 deletions docker-ansible/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Docker Command vs Ansible Playbook Tutorial

## Mac OS X

```bash
##### Install
./helper-mac-install.sh
##### Start a VM if needed
docker-machine create --driver 'virtualbox' docker-ansible
eval $(docker-machine env docker-ansible)

##### Test Shell Ansible
./docker-wordpress.sh
curl $(docker-machine ip docker-ansible)
##### Cleanup (removes everything on this guest vm)
docker ps -q | xargs docker stop
docker ps -aq | xargs docker rm

##### Test Docker Ansible
./docker-wordpress-shell.yml
curl $(docker-machine ip docker-ansible)
##### Cleanup (removes everything on this guest vm)
docker ps -q | xargs docker stop
docker ps -aq | xargs docker rm

##### Test Docker Ansible
./docker-wordpress.yml
curl $(docker-machine ip docker-ansible)
##### Cleanup (removes everything on this guest vm)
docker ps -q | xargs docker stop
docker ps -aq | xargs docker rm

##### Stop and Remove Guest VM
docker-machine stop docker-ansible
docker-machine rm docker-ansible
45 changes: 45 additions & 0 deletions docker-ansible/docker-wordpress-shell.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env ansible-playbook
---
- hosts: localhost
gather_facts: no
vars:
docker_volume: db_data
docker_network: ansible_net
db_name: db
wp_name: wordpress
wp_host_port: 8000
wp_container_port: 80
tasks:
- name: Create docker volume
shell: docker volume create -d local --name {{ docker_volume }}
- name: Search for docker network
shell: docker network ls | grep -q {{ docker_network }}
register: test_docker_network
ignore_errors: yes
- name: Create docker network
shell: docker network create {{ docker_network }}
when: test_docker_network.rc != 0
- name: Launch database container
shell: >
docker run -d
-v {{ docker_volume }}:/var/lib/mysql:rw
--network={{ docker_network }}
--network-alias {{ db_name }}
--restart=always
-e MYSQL_ROOT_PASSWORD=wordpress
-e MYSQL_PASSWORD=wordpress
-e MYSQL_USER=wordpress
-e MYSQL_DATABASE=wordpress
--name {{ db_name }}
mysql:5.7
- name: Launch wordpress container
shell: >
docker run -d
--network={{ docker_network }}
--network-alias {{ wp_name }}
-p "{{ wp_host_port }}:{{ wp_container_port }}"
--restart=always
-e WORDPRESS_DB_HOST="{{ db_name }}:3306"
-e WORDPRESS_DB_PASSWORD=wordpress
--name {{ wp_name }}
wordpress:latest
30 changes: 30 additions & 0 deletions docker-ansible/docker-wordpress.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/sh

# create the network if network does not exist
docker network list | grep -q "wordpress_net" || \
docker network create "wordpress_net"

# create volumes
docker volume create -d local --name "db_data"

# start mysql database
docker run -d \
-v db_data:/var/lib/mysql:rw \
--network='wordpress_net' \
--network-alias db \
--restart=always \
-e MYSQL_ROOT_PASSWORD='wordpress' \
-e MYSQL_PASSWORD='wordpress' \
-e MYSQL_USER='wordpress' \
-e MYSQL_DATABASE='wordpress' \
--name db mysql:5.7

# start wordpress application
docker run -d \
--network=wordpress_net \
--network-alias wordpress \
-p "8080:80" \
--restart=always \
-e WORDPRESS_DB_HOST="db:3306" \
-e WORDPRESS_DB_PASSWORD='wordpress' \
--name wordpress wordpress:latest
49 changes: 49 additions & 0 deletions docker-ansible/docker-wordpress.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env ansible-playbook
---
- hosts: localhost
gather_facts: no
vars:
docker_volume: db_data
docker_network: ansible_net
db_name: db
wp_name: wordpress
wp_host_port: 8000
wp_container_port: 80
tasks:
- name: "Create a Volume"
docker_volume:
name: "{{ docker_volume }}"
# ansible 2.2 only
- name: "Create a network"
docker_network:
name: "{{ docker_network }}"
- name: "Launch database container"
docker_container:
name: "{{ db_name }}"
image: mysql:5.7
volumes:
- "{{ docker_volume }}:/var/lib/mysql:rw"
restart: true
networks:
- name: "{{ docker_network }}"
alias:
- "{{ db_name }}"
env:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
- name: "Launch wordpress container"
docker_container:
name: "{{ wp_name }}"
image: wordpress:latest
ports:
- "{{ wp_host_port }}:{{ wp_container_port }}"
restart: true
networks:
- name: "{{ docker_network }}"
alias:
- "{{ wp_name }}"
env:
WORDPRESS_DB_HOST: "{{ db_name }}:3306"
WORDPRESS_DB_PASSWORD: wordpress
9 changes: 9 additions & 0 deletions docker-ansible/helper-mac-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh
# only run on Mac OS X
[ $(uname -s) = "Darwin" ] || { echo 'Only runs on Mac OS X!!!' ; exit 1 ; }
# Install Homebrew
which -s brew || /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

brew bundle --verbose

pip install -r requirements.txt
2 changes: 2 additions & 0 deletions docker-ansible/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ansible
docker
5 changes: 5 additions & 0 deletions docker-docker_compose/Brewfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cask_args appdir: '/Applications'
cask 'virtualbox'
cask 'virtualbox-extension-pack'
cask 'vagrant'
cask 'docker-toolbox'
41 changes: 41 additions & 0 deletions docker-docker_compose/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Docker Command vs Ansible Playbook Tutorial

## Mac OS X

These are the commands you can use to startup and cleanup each test

```bash
##### Install
./helper-mac-install.sh
##### Start a VM if needed
docker-machine create --driver 'virtualbox' docker-compose-tutorial
eval $(docker-machine env docker-compose-tutorial)

##### Test Shell Ansible
./docker-wordpress.sh
curl $(docker-machine ip docker-compose-tutorial):8080
##### Cleanup (removes everything on this guest vm)
docker ps -q | xargs docker stop
docker ps -aq | xargs docker rm

##### Test Docker Compose
pushd compose_static
docker-compose up -d
curl $(docker-machine ip docker-compose-tutorial):8000
##### Cleanup (removes everything on this guest vm)
docker-compose stop
docker-compose rm
popd

##### Test Docker Compose
pushd compose_w_envars
docker-compose up -d
curl $(docker-machine ip docker-compose-tutorial):8000
##### Cleanup (removes everything on this guest vm)
docker-compose stop
docker-compose rm
popd

##### Stop and Remove Guest VM
docker-machine stop docker-compose-tutorial
docker-machine rm docker-compose-tutorial
25 changes: 25 additions & 0 deletions docker-docker_compose/compose_static/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: '2'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data:
2 changes: 2 additions & 0 deletions docker-docker_compose/compose_w_envars/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
WP_HOST_PORT=8000
WP_CONTAINER_PORT=80
25 changes: 25 additions & 0 deletions docker-docker_compose/compose_w_envars/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: '2'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "${WP_HOST_PORT}:${WP_CONTAINER_PORT}"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data:
7 changes: 7 additions & 0 deletions docker-docker_compose/helper-mac-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
# only run on Mac OS X
[ $(uname -s) = "Darwin" ] || { echo 'Only runs on Mac OS X!!!' ; exit 1 ; }
# Install Homebrew
which -s brew || /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

brew bundle --verbose

0 comments on commit b69db0a

Please sign in to comment.