Last active
January 25, 2022 22:13
-
-
Save tomharvey/7f7f4fa1e79ee37e1198d9d9d8d1b46a to your computer and use it in GitHub Desktop.
A Vagrantfile to start an Ubuntu Docker Host with the docker socket exposed on 192.168.56.10:2375
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- mode: ruby -*- | |
# vi: set ft=ruby : | |
# All Vagrant configuration is done below. The "2" in Vagrant.configure | |
# configures the configuration version (we support older styles for | |
# backwards compatibility). Please don't change it unless you know what | |
# you're doing. | |
HOSTNAME='dockerhost.local' | |
Vagrant.configure("2") do |config| | |
config.vm.hostname = HOSTNAME | |
# The most common configuration options are documented and commented below. | |
# For a complete reference, please see the online documentation at | |
# https://docs.vagrantup.com. | |
# Every Vagrant development environment requires a box. You can search for | |
# boxes at https://vagrantcloud.com/search. | |
config.vm.box = "ubuntu/focal64" | |
# Disable automatic box update checking. If you disable this, then | |
# boxes will only be checked for updates when the user runs | |
# `vagrant box outdated`. This is not recommended. | |
# config.vm.box_check_update = false | |
# Create a forwarded port mapping which allows access to a specific port | |
# within the machine from a port on the host machine. In the example below, | |
# accessing "localhost:8080" will access port 80 on the guest machine. | |
# NOTE: This will enable public access to the opened port | |
# config.vm.network "forwarded_port", guest: 80, host: 8080 | |
# Create a forwarded port mapping which allows access to a specific port | |
# within the machine from a port on the host machine and only allow access | |
# via 127.0.0.1 to disable public access | |
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" | |
# Create a private network, which allows host-only access to the machine | |
# using a specific IP. | |
config.vm.network "private_network", ip: "192.168.56.10" | |
config.vm.network 'forwarded_port', guest: 2375, host: 2375, id: 'dockerd' | |
# Create a public network, which generally matched to bridged network. | |
# Bridged networks make the machine appear as another physical device on | |
# your network. | |
# config.vm.network "public_network" | |
# Share an additional folder to the guest VM. The first argument is | |
# the path on the host to the actual folder. The second argument is | |
# the path on the guest to mount the folder. And the optional third | |
# argument is a set of non-required options. | |
# config.vm.synced_folder "./", "/vagrant_data" | |
config.vm.synced_folder "~/.aws", "/shared/.aws" | |
config.vm.synced_folder "~/.ssh", "/shared/.ssh" | |
# Provider-specific configuration so you can fine-tune various | |
# backing providers for Vagrant. These expose provider-specific options. | |
# Example for VirtualBox: | |
# | |
# config.vm.provider "virtualbox" do |vb| | |
# # Display the VirtualBox GUI when booting the machine | |
# vb.gui = true | |
# | |
# # Customize the amount of memory on the VM: | |
# vb.memory = "1024" | |
# end | |
# | |
# View the documentation for the provider you are using for more | |
# information on available options. | |
# Enable provisioning with a shell script. Additional provisioners such as | |
# Ansible, Chef, Docker, Puppet and Salt are also available. Please see the | |
# documentation for more information about their specific syntax and use. | |
config.vm.provision "shell" do |s| | |
ssh_pub_key = File.open("#{Dir.home}/.ssh/id_rsa.pub", "rb").read | |
ssh_prv_key = File.open("#{Dir.home}/.ssh/id_rsa", "rb").read | |
local_username = ENV['USER'].downcase | |
workspace_dir = Dir.pwd | |
workspace_dir.slice! Dir.home | |
s.inline = <<-SHELL | |
apt-get update -y && apt upgrade -y | |
apt-get install -y install virtualbox-guest-dkms ntp ntpdate | |
# SETUP DOCKER WITH SOCKET EXPOSED ON PORT 2375 | |
wget -qO- https://get.docker.com/ | sh | |
echo '{"hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]}' > /etc/docker/daemon.json | |
mkdir -p /etc/systemd/system/docker.service.d | |
echo "[Service] | |
ExecStart= | |
ExecStart=/usr/bin/dockerd | |
" > /etc/systemd/system/docker.service.d/override.conf | |
systemctl daemon-reload | |
systemctl restart docker.service | |
# SETUP THE USER | |
echo "#{ssh_pub_key}" >> "/home/vagrant/.ssh/authorized_keys" | |
echo "#{ssh_prv_key}" >> "/home/vagrant/.ssh/id_rsa" | |
ln -s /shared/.aws "/home/vagrant/.aws" | |
ln -s "/vagrant" "/home/vagrant/#{workspace_dir}" | |
ln -s /home /Users | |
ln -s /home/vagrant "/home/#{local_username}" | |
usermod -a -G docker vagrant | |
# Extras | |
bash <(curl -Ss https://my-netdata.io/kickstart.sh) | |
SHELL | |
end | |
config.vm.provider "virtualbox" do |v| | |
v.name = HOSTNAME | |
v.memory = 8192 | |
v.cpus = 4 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have a folder in my mac's home dir called 'Work'. Many people have one called 'workplaces' or something. We will call this "Workspace".
vagrant up
and your docker host VM will be created.The docker host will have the Workspace folder shared as well as your ~/.aws directory and your public key (
~/.ssh/id_rsa.pub
) will be copied across to authorized_hosts. So you can shell in, but as the uservagrant
This docker host is at 192.168.56.10 and the docker socket is exposed on port 2375
So, add
export DOCKER_HOST=tcp://192.168.56.10:2375
to your shell to use this docker host instead of Docker Desktop. You can add that line to~/.ssh/bashrc
so it's permanently set.You should be able to ssh into [email protected] and be able to run
docker ps
to see the containers running on that host (none for now).