Skip to content

Instantly share code, notes, and snippets.

@dwurf
Last active February 28, 2016 09:30
Show Gist options
  • Save dwurf/fba4a253c3f3c7314b81 to your computer and use it in GitHub Desktop.
Save dwurf/fba4a253c3f3c7314b81 to your computer and use it in GitHub Desktop.
Practical Use of Docker
## Docker

Docker is a useful abstraction for processes

  • Web application server (django/rails/tomcat/...)
  • Database
  • Reverse HTTP proxy (nginx/apache)

... each in their own Docker container

This allows a process to package all its code/binaries, libraries and configuration in a portable package.

## Docker Benefits

Why dockerise your service?

  • Remove platform dependencies (essential OS parts are packaged with your app)
  • Simplify installation / running (docker pull, then docker run ...)
  • Manage external dependencies (control over files/directories, network ports, databases and other dependent processes)
  • Make Dev like Prod (develop locally with a full app stack, without complex install and management)
## Installing Docker
## Installing Docker

Or run these commands (Ubuntu):

    sudo apt-get update
    sudo apt-get -y install apt-transport-https
    sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
    sudo sh -c "echo 'deb https://apt.dockerproject.org/repo ubuntu-$(lsb_release -cs) main' > /etc/apt/sources.list.d/docker.list"
    sudo apt-get update
    sudo apt-get -y install docker-engine
    sudo apt-get -y install curl
    sudo bash -c "curl -L https://github.com/docker/compose/releases/download/1.6.0-rc2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose"
    sudo chmod u+x /usr/local/bin/docker-compose
    sudo docker-compose -v

</section>
## Live Demos!
## Live Demo: Personal Wiki

Create + start

    sudo docker run -d \
        --name wiki \
        --rm \
        -p 80:8080 \
        mazzolino/tiddlywiki:latest

Stop + destroy

    sudo docker stop wiki

See what it's doing (here)[https://github.com/djmaze/tiddlywiki-docker/blob/master/Dockerfile]

## Live Demo: Personal Wiki

Create + start

    sudo docker run -d \
        --name wiki \
        --restart always \
        -p 80:8080 \
        -v /srv/docker/wiki:/var/lib/tiddlywiki \
        mazzolino/tiddlywiki:latest

Stop + destroy (but don't lose the data!)

    sudo docker stop wiki
    sudo docker rm wiki 

</section>
## Live Demo: docker-compose

docker-compose.yml

    web:
            container_name: wiki
            image: mazzolino/tiddlywiki
            restart: always
            volumes:
                    - /srv/docker/wiki:/var/lib/tiddlywiki

Start/stop

    sudo docker-compose up -d
    sudo docker-compose down
</section>
## Live Demo: Reverse Proxy

reverse-proxy.yml

    proxy:
            container_name: proxy
            image: jwilder/nginx-proxy
            restart: always
            volumes:
                    - /var/run/docker.sock:/tmp/docker.sock:ro
                    - /srv/ssl:/etc/nginx/certs:ro
            ports:
                    - 80:80
                    # Assumes you have certs in place
                    #- 443:443

</section>
## Live Demo: Wiki behind Reverse Proxy

wiki.yml

    web:
            container_name: wiki
            image: mazzolino/tiddlywiki
            restart: always
            volumes:
                    - /srv/docker/wiki:/var/lib/tiddlywiki
            environment:
                    - VIRTUAL_HOST=wiki.$DOMAIN
                    - VIRTUAL_PORT=8080
</section>
## Live Demo: docker-compose

In the same directory as the docker-compose.yml file, run:

  • export DOMAIN=example.com
  • sudo docker-compose -f proxy.yml up -d
  • sudo docker-compose -f wiki.yml up -d

To shutdown and destroy the containers, run:

  • sudo docker-compose -f proxy.yml down

  • sudo docker-compose -f wiki.yml down

## TLS/DNS Tricks

For quick and easy docker containers:

  • Get a wildcard SSL certificate (~$55 https://ssl2buy.com )
  • Get a cheap domain (~$3/yr https://godaddy.com )
  • Use Wildcard DNS to forward all requests to a single IP
  • Use container jwilder/nginx-proxy for automatic HTTPS routing
## git
## _maybe_ a mail server...
## docker-compose
## backup considerations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment