I decided to try it on my development environment when I heard a lot of people say how great the Docker is and that many trendy kids are using it. In the following article, I'll explain how Mac OS x builds Postgres,elasticsearch and Redis.
What is Docker
Docker uses a lightweight container to separate an app from its operating OS. It puts apps into an isolated box that renders only the folders and ports that need to be used.
Thus, containers based on the creation and use of the app are reusable and shared. At present, more than 15,000 kinds of containers exist in Docker concentration. Docker is like a store repository, when you need to create an app you want, you first pick it up and then download it and open it.
install Docker on OS X
Docker is not inherently capable of running on OS X, it needs a Linux kernel that contains a Linux container. So when you want to install OS X like I do, you will need a set of virtual device.
Do not use Boot2docker
When trying to get Docker to work, I found "very easy" to install. This will use a tool called Boot2docker, which is a thin wrapper on a virtual machine like VirtualBox.
I immediately discovered that the tool had some serious problems, such as: In a stable state, it would terminate any process that acquired Docker. I don't want to waste too much physical and brain cells on this, so I continue to look for an alternative solution.
Using Vargrant
Since the 1.6 version of Vagrant, the supporting components that support Docker have been integrated. Vargrant is a VirtualBox-like virtual software that describes your environment in a way that declares a ruby DSL.
I like this way of defining a virtual environment, because when your environment fails, you can record the information at that time and start using it again without leaving out a lot of information about the environment variables.
Install Stuff
First, let's browse through the various issues 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
A vagrant file to describe a requirement for using a Ruby DSL virtual machine environment. When describing the Docker container, vagrant makes each container seem to be using its own unique virtual machine. In fact, this is an illusion, because each Docker container is actually allowed on a variety of proxy virtual machines.
Therefore, two vagrant files are very necessary, a file is used to define the proxy virtual machine (Provisioner), and another file is used to define the Docker container (Providers).
Agent virtual vagrant file
Agent 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 $} ' | Xargs kill"
Config.vm.network:forwarded_port, guest:6379, host:637 9
Config.vm.network:forwarded_port, guest:5432, host:5432
config.vm.network:forwarded_port, guest:9200, Host:9200end
This uses the hashicorp/precise64 Ubuntu 12.04 64 bits to handle the proxy virtual machine. It also provides Docker and some magical shell commands to get Docker to work.
The last thing to do is set the turn of the mouth. It uses Config.vm.network to configure Redis, Elasticsearch, and Postgres to map to OS X with the agent virtual machine.
Vagrant file for Docker container
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_vagrantfil
E = './vagrantfile.proxy ' 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 en
D 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 Enden D
This file defines three containers: Redis, Elasticsearch, and Postgres with pictures Dockerfile/redis, Dockerfile/elasticsearch and Paintedfox/postgresql.
Each file definition Vagrant_vagrantfile as a proxy VM file, which allows them to run on the same proxy virtual machine.
The volumes definitions for Redis and Postgres are for their information to be stored on the proxy VM, not in the container. This is why the container can be deleted or upgraded and the data will not be lost. The next step is to map these files from the proxy VM to OS X, but there is no need to keep it running.
The ports on each container defines which port to go to the proxy VM. These need to match the port of the proxy VM to OS X.
The Postgres container also defines the environment variables for the server on which it needs to be set. These can be used to set the default Postgres server in OS X, which is implemented by setting the environment variable Pghost=localhost pguser=root pgpassword=abcdef123456.
Working with vagrant
In the same directory as your vagrant file, you can run:
Vagrant up--provider=docker
The first time this is run, vagrant will download and then start the proxy VM, and then download and start the Docker container. The existing pictures are reused each time the vagrant is run after these initialization downloads.
You can view the status of the Docker container:
Something similar should be exported:
Copy Code code as follows:
Current machine states:
Redis Running (Docker)
Elasticsearch Running (Docker)
DB Running (Docker)
To test that the Docker container is running correctly, you can use the Redis and Postgres clients, and the curl for Elasticsearch. You only need to check the REDIS-CLI and Psql connections to the server, and the Curl http://localhost:9200 response.
If you need to connect to a proxy VM (which is very helpful for debugging), run Vagrant Global-status, which lists all VMS, including proxy. Then call the vagrant SSH <id>,id as the proxy ID. It is recommended that you do not manually change this proxy VM and use a chef (or similar) script to make the changes easier to test and distribute.
Performance
When using virtualization, the first question always asks, "How much is the performance impact?" ”。 To find out how bad the performance impact is, my colleagues and I have both done a postgres on the same hardware and elasticsearch and Redis to strengthen the tests. The only difference is that one test has a native-installed software and the other has a Docker custom container. The native software was run for 2 minutes, with the container running for 3 minutes.
This performance impact is not as small as I thought it would be, or even worse. Even so, I will continue to use Docker for development, but I do not recommend it as a panacea for all development environmental problems.
Note: Some other restrictions on the use of vagrant and Docker are listed here
Summary
I still can't see where the "vagrant with Docker" Road is. However, after looking at the possible situation, I couldn't help thinking about what else it could be used for. Plus, it's the best virtualization I've ever encountered, and the fun is programming.