> ## Content Index
> Fetch the complete content index at: https://blog.matthewbrunelle.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Media Center Series Part 1: Install Docker With Ansible
- URL: https://blog.matthewbrunelle.com/media-center-series-part-1-install-docker-with-ansible/
- Published: 2019-10-01T04:00:00.000Z
- Updated: 2025-05-17T15:59:23.000Z
- Description: Using Anisble to provision Docker and Docker Compose
- Author: Matthew Brunelle
- Tags: Projects, Docker, Self-hosting

This series is a collection of notes from my experience setting up a media server. This first post covers using Ansible to setup [Docker](https://www.docker.com/?ref=blog.matthewbrunelle.com) and [Docker Compose](https://docs.docker.com/compose/?ref=blog.matthewbrunelle.com) on the server.

# Ansible

The [full Ansible playbook](https://github.com/ciferkey/media/blob/master/ansible.yml?ref=blog.matthewbrunelle.com) is available but I'm going to breakdown what went in to it. The file is designed so that you can drop it on to a box as a fresh root user and it will handle everything for you.

I'm not coordinating multiple machines so the script will be run locally on the box you want to configure using the default localhost configuration:

```yaml
  - name: Docker
    connection: local
    gather_facts: yes
    hosts: localhost
    vars_prompt:
      - name: password
        prompt: "Password for docker user"
        private: yes
        encrypt: "sha512_crypt"
        confirm: yes
        salt_size: 7

```

I've configured the playbook to [prompt](https://docs.ansible.com/ansible/latest/user%5Fguide/playbooks%5Fprompts.html?ref=blog.matthewbrunelle.com) for a password to use with for the docker user since there is no secretes management tool. That could be a nice to have but would be overkill for such a *simple demo application*.

## Docker User

First we need a non root user to run the containers with. Instead of running these commands:

```bash
adduser docker
usermod -aG sudo docker

```

We can use the [user module](https://docs.ansible.com/ansible/latest/modules/user%5Fmodule.html?ref=blog.matthewbrunelle.com):

```yaml
  - name: Create docker user
    user:
        name: docker
        password: "{{password}}"
        update_password: on_create
        groups:
            - sudo
            - render
            - video
        shell: /bin/bash
        state: present

```

The docker user will also be added to the render and video groups to enable access to hardware transcoding.

## Install Docker

The Docker [documentation recommends](https://docs.docker.com/install/linux/docker-ce/debian/?ref=blog.matthewbrunelle.com) the following installation process:

```bash
sudo apt-get update
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg2 \
    software-properties-common
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/debian \
   $(lsb_release -cs) \
   stable"
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

```

This involves installing some dependencies, adding a key and a new repository to the package manager and installing docker. Ansible has built in support for many of these operations which simplifies tasks.

The [apt module](https://docs.ansible.com/ansible/latest/modules/apt%5Fmodule.html?ref=blog.matthewbrunelle.com) allows us to install dependencies. By using "update\_cache: yes" we can skip having a separate update step:

```yaml
  - name: Install Docker Deps
    apt:
        name:
            - apt-transport-https
            - ca-certificates
            - curl
            - gnupg2
            - software-properties-common
        update_cache: yes

```

The [apt\_key module](https://manpages.ubuntu.com/manpages/bionic/man8/apt-key.8.html?ref=blog.matthewbrunelle.com) lets us pull in a new key from a URL:

```yaml
  - name: Install Docker Repository Key
    apt_key:
        url: https://download.docker.com/linux/debian/gpg

```

The [apt\_repository module](https://docs.ansible.com/ansible/latest/modules/apt%5Frepository%5Fmodule.html?ref=blog.matthewbrunelle.com) lets us add a new repository. Since I specified ["gather\_facts: yes"](https://docs.ansible.com/ansible/latest/modules/gather%5Ffacts%5Fmodule.html?ref=blog.matthewbrunelle.com) at the start of the playbook Ansible collected information about the machine before it started. This includes a "ansible\_distribution\_release" value that lets us use the correct repository for our Debian release:

```yaml
- name: Add Docker Respository
apt_repository:
    repo: "deb [arch=amd64] https://download.docker.com/linux/debian {{ ansible_distribution_release }} stable"

```

Then we can install Docker from the new repository:

```yaml
- name: Install Docker
    apt:
        name:
            - docker-ce
            - docker-ce-cli
            - containerd.io
        update_cache: yes

```

Finally instead having to do this to enable the docker service to run on startup:

```bash
sudo systemctl enable docker
sudo systemctl start docker

```

The [systemd module](https://docs.ansible.com/ansible/latest/modules/systemd%5Fmodule.html?ref=blog.matthewbrunelle.com) can be used instead:

```yaml
- name: Set Docker to Run on Startup
  systemd:
    state: started
    name: docker
    enabled: true

```

## Install Docker Compose

The Docker Compose [documentation recommends](https://docs.docker.com/compose/install/?ref=blog.matthewbrunelle.com) the following installation process:

```bash
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

```

This pulls a binary from Github, saves the binary under /usr/local/bin and sets the appropriate permissions for it.

The [get\_url module](https://docs.ansible.com/ansible/latest/modules/get%5Furl%5Fmodule.html?ref=blog.matthewbrunelle.com) lets us fetch the binary off of GitHub. We use "ansible\_system" and "ansible\_architecture" from the gathered facts to generate the URL as opposed to [uname](http://man7.org/linux/man-pages/man2/uname.2.html?ref=blog.matthewbrunelle.com). As a part of getting the binary we can set the mode to 755 for the docker user to avoid doing this in a second step. Also as per the docs "You must either add a leading zero so that Ansible's YAML parser knows it is an octal number (like 0644 or 01777) or quote it (like '644' or '1777') so Ansible receives a string and can do its own conversion from string into number."

```yaml
  - name: Install Docker Compose
    get_url:
        url: https://github.com/docker/compose/releases/download/1.24.1/docker-compose-{{ ansible_system }}-{{ ansible_architecture }}
        dest: /usr/local/bin/docker-compose
        mode: '755'
        owner: docker

```

# Running the Playbook

At this point you can curl the playbook to a fresh Debian machine and run the following:

```bash
ansible-playbook ansible.yml

```

Now you will have Docker and Docker Compose ready to use on your machine.