chouseknecht / ansible-docker

setup docker and pull/create, build or restart containers on hosts configured through environment

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DrPsychick.docker-deploy

Build Status

Adding random things here...

Purpose:

  • manage a simple docker environment with ease and flexibility
  • use basic linux and docker commands
  • automate build, create, restart of your containers
  • rollout only required changes
  • use it in your NON-PRODUCTION environment, at home or in your test lab

NOT included:

  • random container name
  • starting container multiple times/load balancing/high availability

Requirements

  • install docker (I use geerlingguy.docker to install docker on the hosts)

Role variables

See defaults/main.yml. Include your own definition of 'containers' dict.

minimum definition:

containers:
  mycontainer:
    hosts: ["all"]
    name: "mytestcontainer"
    image: "cogniteev/echo"
    directory: "mytestcontainer"
    pull: yes

Special variables to limit your play to what you need. Will affect all instance of the role.

container_only=mytestcontainer
container_forcerestart=yes

Example Playbook

- hosts: dockerhosts
  pre_tasks:
    - include_vars: mycontainers_definition.yml
  roles:
    - { role: DrPsychick.docker-deploy }

Use case

I use this to test, create, run and update containers in my local environment and on my dedicated server.

Examples

Setup your own role which defines the docker containers and includes required files and templates. With this you can keep configuration and source files separate from the imported role and control different scenarios.

Directory structure:

container-definition
|- files : files for containers in container.name subdir
   |- mytestcontainer
      |- testfile
|- templates : templates for containers in container.name subdir
   |- mytestcontainer
      |- mytestcontainer.env
      |- Dockerfile
|- tasks : if you want to selectively include variables or set specific variables
|- vars : definition of containers

Variables:

# sets the source directory for files and templates to the current role path
# cannot use "role_path" as it gets reevaluated in "docker-deploy" role somehow
container_default_sourcedir: '{{ playbook_dir + "/roles/containerdefinition" }}'

containers: 
  mytestcontainer:
    name: "mytestcontainer2"
    build: yes
    templates:
      dockerfile: { name: "Dockerfile", mode: "0644" }
      envfile: { name: "mytestcontainer.env", mode: "0644" }

Playbook:

- name: Setup docker containers
  hosts: all
  roles:
    - { role: containerdefinition }
    - { role: DrPsychick.docker-deploy }

Internals

Flow:

  • copy files/templates to target machine (Dockerfile, env-file, ...)
  • pull or build image
  • stop and remove existing container
  • create and restart container
  • cleanup
  • setup aliases to maintain containers easily

Testing

  • create ansible.cfg file with content:
[defaults]
roles_path = ../
  • run lint + tests (see .travis.yml):
    • yamllint - need to somehow pass ansible-galaxy rules...
    • ansible-lint ./
    • ansible-playbook -i tests/inventory tests/test.yml --connection=local --become
    • ansible-playbook -i tests/inventory tests/test_cleanup.yml --connection=local --become

License

GPLv3

About

setup docker and pull/create, build or restart containers on hosts configured through environment

License:GNU General Public License v3.0


Languages

Language:Dockerfile 100.0%