Docker

Remote Docker

Docker for Mac uses a lot of resources, especially in terms of CPU and disk space. Connecting to a remote instance of Docker means my MacbookPro can put those resources towards my IDE.

Requirements

  1. A server with SSH access.

  2. docker and docker-machine on your computer.

Ansible Role

I created a role to automate the process; It's setting up a user to do the bootstrapping, opening up port 2376 on the server and then calling docker-machine on your computer to bootstrap docker.

---
- name: user (dockerbootstrap)
  user:
    name: dockerbootstrap
    comment: Docker Bootstrap
    password: ...
    group: sudo
    shell: /bin/bash
    
- name: authorized key (dockerbootstrap)
  authorized_key:
    user: dockerbootstrap
    state: present
    key: "{{ lookup('file', lookup('env','HOME') + '/.ssh/id_rsa.pub') }}"
    
- name: passwordless sudo (dockerbootstrap)
  lineinfile:
    path: /etc/sudoers
    line: dockerbootstrap  ALL=(ALL) NOPASSWD:ALL
    
- name: allow 2376 (docker)
  tags: ["networking"]
  ufw:
    rule: allow
    port: "2376"
    proto: tcp
    
- name: create docker machine
  tags: ["local"]
  become: no
  local_action:
    module: command
    argv:
      - docker-machine
      - create
      - --driver
      - generic
      - --generic-ip-address
      - "{{ inventory_hostname }}"
      - --generic-ssh-key
      - "{{ lookup('env','HOME') + '/.ssh/id_rsa' }}"
      - --generic-ssh-user
      - dockerbootstrap
      - docker

- name: remove user (dockerbootstrap)
  user:
    name: dockerbootstrap
    state: absent
    remove: yes
    
- name: remove passwordless sudo (dockerbootstrap)
  lineinfile:
    path: /etc/sudoers
    line: dockerbootstrap  ALL=(ALL) NOPASSWD:ALL
    state: absent

Then on your computer it's a case of running

eval $(docker-machine env docker)

and with any luck docker version will give you:

 docker version
Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.14
 Git commit:        afacb8b
 Built:             Thu Mar 12 02:45:41 2020
 OS/Arch:           darwin/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       afacb8b7f0
  Built:            Wed Mar 11 01:24:19 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

Commands

Commands I use a fair amount

  1. Reclaim space

     docker system prune -a --volumes
    WARNING! This will remove:
      - all stopped containers
      - all networks not used by at least one container
      - all images without at least one container associated to them
      - all build cache
    
    Are you sure you want to continue? [y/N]

    Flag

    Description

    -a

    Remove all unused images not just dangling ones

    --volumes

    Prune volumes

Last updated