Install the Vagrant and Docker tutorials on Mac OS

  • 2020-05-07 20:38:28
  • OfStack

When I heard a lot of people talking about how great Docker was and how many cool kids were using it, I decided to try it on my development environment. In the following article, I'll show you how to set up Postgres, Elasticsearch, and Redis in Mac OS X.

what is Docker

Docker separates an APP from the OS it runs in a lightweight container. It puts APP into an isolated box, rendering only the folders and ports that need to be used.

In this way, containers based on building and using APP are reusable and Shared. Currently, more than 15,000 containers exist in the Docker collection. Docker is like a store repository. When you need to build an APP you want, you first pick it out, then download it and open it.


installs Docker on OS X

Docker does not naturally run on OS X; it requires an Linux kernel with an LINUX container. So when you imagine me installing OS X, you will need a virtual appliance.

do not use boot2docker

When trying to get docker to work, I found it "pretty easy" to install. This will use a tool called boot2docker, which is a thin wrapper for a virtual machine like virtualBox.

I immediately noticed that there were some serious problems with this tool, such as: in a stable state it will terminate any process that gets Docker. I didn't want to waste too much energy and brain cells in this area, so I continued to look for an alternative solution.

USES Vargrant

Since version 1.6 of Vagrant, companion components supporting Docker have been integrated. Vargrant is a virtual software similar to VirtualBox that describes your environment in a way that declares Ruby DSL.

I love this way of defining a virtual environment, because when your environment fails, you can record the information and start using it again without missing a lot of information about similar environment variables.
installs Stuff

First, let's take a look at the various things we need to install.

Homebrew installation:
 


ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

Cask installation:
 


brew tap caskroom/homebrew-cask 
brew install brew-cask

Vagrant and VirtualBox installation:


brew cask install virtualbox 
brew cask install vagrant


Vagrant file

1 vagrant file to describe the requirements of 1 virtual machine environment using Ruby DSL. When describing Docker containers, Vagrant makes each container appear to be using its own unique virtual machine 1. This is actually an illusion because each Docker container is actually allowed on various proxy virtual machines.

Therefore, two vagrant files are required, one to define the proxy virtual machine (Provisioner) and one to define the Docker container (Providers).
proxy virtual Vagrant file

The proxy virtual Vagrant file is called: Vagrantfile.proxy
 


VAGRANTFILE_API_VERSION = "2"Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| 
 config.vm.box = "hashicorp/precise64"
 config.vm.provision "docker"
 config.vm.provision "shell", inline:
  "ps aux | grep 'sshd:' | awk '{print $2}' | xargs kill"
 
 config.vm.network :forwarded_port, guest: 6379, host: 6379
 config.vm.network :forwarded_port, guest: 5432, host: 5432
 config.vm.network :forwarded_port, guest: 9200, host: 9200end

This USES 64 bits of hashicorp/precise64 Ubuntu 12.04 to handle the proxy virtual machine. It also offers Docker and some amazing shell commands to make Docker work.

The last thing is to set up the forwarding port. It USES config.vm.network to configure Redis, Elasticsearch, and Postgres, and maps to OS X with the proxy virtual machine.

Docker container Vagrant file

This is the main content of Vagrantfile:

 


VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
 
 config.vm.define "redis" do |v|
  v.vm.provider "docker" do |d|
   d.image = "dockerfile/redis"
   d.volumes = ["/var/docker/redis:/data"]
   d.ports = ["6379:6379"]
   d.vagrant_vagrantfile = "./Vagrantfile.proxy"
  end
 end
 
 config.vm.define "elasticsearch" do |v|
  v.vm.provider "docker" do |d|
   d.image = "dockerfile/elasticsearch"
   d.ports = ["9200:9200"]
   d.vagrant_vagrantfile = "./Vagrantfile.proxy"
  end
 end
 
 config.vm.define "postgres" do |v|
  v.vm.provider "docker" do |d|
   d.image = "paintedfox/postgresql"
   d.volumes = ["/var/docker/postgresql:/data"]
   d.ports = ["5432:5432"]
   d.env = {    USER: "root",    PASS: "abcdEF123456",    DB: "root"
   }
   d.vagrant_vagrantfile = "./Vagrantfile.proxy"
  end
 endend

This file defines three containers: Redis, Elasticsearch, and Postgres with images dockerfile/redis, dockerfile/elasticsearch and paintedfox/postgresql.

Each file defines vagrant_vagrantfile as proxy VM, which makes them run on the same proxy virtual machine.

volumes for Redis and Postgres is defined so that their information can be stored on proxy VM rather than in a container. This is also why the container can delete or upgrade more without losing data. The next step is to map these files from proxy VM to OS X, but there is no need to make them work.

ports on each container defines which port goes to proxy VM. These need to match ports from proxy VM to OS X.

The Postgres container also defines the environment variables that need to be set for its server. These can be used to set the default Postgres server in OS X by setting the environment variable PGHOST=localhost PGUSER=root PGPASSWORD=abcdEF123456.

USES Vagrant to work

In the same directory as your Vagrant file, you can run:
 


vagrant up --provider=docker

The first time you run this, Vagrant will download and then launch proxy VM, then download and start the Docker container. Each run of Vagrant after these initial downloads will reuse the existing images.

You can view the status of the Docker container:
 


vagrant status

Should output 1 something like:
 


Current machine states:
 
redis                     running (docker) 
elasticsearch             running (docker) 
db                        running (docker)

To test that the Docker container is running correctly, use the Redis and Postgres clients, and curl for Elasticsearch. Just check the redis-cli and psql connections to the server, and curl http://localhost:9200 response.

If you need to connect to proxy VM (very helpful for debugging), run vagrant global-status, which lists all VM, including proxy. Then call vagrant ssh < ID > , ID is ID of proxy. It is not recommended to change this proxy VM manually, but to use one Chef (or similar) script so that the changes can be more easily tested and distributed.

performance

When using virtualization, the first question is always "what is the performance impact?" . To find out how bad the performance impact was, my colleagues and I both did an Postgres and enhanced Elasticsearch and Redis tests on the same hardware. The only difference is that one test has native installed software and the other has Docker custom containers. The run with the native software lasted two minutes, and the run with the container lasted three minutes.

The performance impact is not as small as I thought, and could be even worse. Even so, I will continue to use Docker for development, but it is not recommended as a panacea for all development environment problems.


Note: 1 some other restrictions on using Vagrant and Docker are listed here
summary

I can't see the way to "Vagrant with Docker" yet. Still, after looking at the possibilities, I couldn't help but wonder where else it could be used. Plus, it's the most fun virtualization I've ever encountered. The fun is in the programming.


Related articles: