Tutorials for installing vagrant and Docker on Mac OS

Source: Internet
Author: User
Tags install brew virtual environment brew cask



Reproduced in: http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/128.html?1455808640



I decided to try it out in my development environment when I heard a lot of people talking about how great Docker is and how 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 running OS. It puts the app into an isolated box, presenting only the folders and ports that need to be used.



In this way, containers based on the creation and use of apps are reusable and shared. Currently, there are more than 15,000 containers in the Docker set. Docker is like a store repository, and when you need to create an app you want, you first pick it up and then download it and open it.




Installing 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 me, you'll need a set of VMS.



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 found that the tool had some serious problems, such as that the process of acquiring Docker would be terminated in a stable state. I don't want to waste too much energy and brain cells in this area, so I continue to look for a solution that can be replaced.



Using Vargrant



Since the 1.6 release of Vagrant, the supporting components for Docker have been integrated. Vargrant is a VirtualBox-like virtual software that describes your environment in a way that declares a ruby DSL.



I really 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 missing out on a lot of information like environment variables.
Installing Stuff



First, let's explore the various things we need to install.



Homebrew Installation:





Ruby-e "$ (curl-fssl https://raw.github.com/mxcl/homebrew/go)" #p # pagination title #e#





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 the needs of a virtual machine environment using Ruby DSL. When describing a Docker container, vagrant makes each container appear 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, one file is used to define the proxy virtual machine (Provisioner), and the other file is used to define the Docker container (Providers).
Proxy virtual vagrant File



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 $} ' | 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 the Hashicorp/precise64 Ubuntu 12.04 64-bit to process the proxy virtual machine. It also offers Docker and some magical shell commands to get Docker to work.



The final thing is to set up the forwarding port. It uses Config.vm.network to configure the Redis, Elasticsearch, and postgres to be mapped to OS X with a proxy virtual machine. #p # pagination Title #e#



Vagrant Files for Docker containers



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 = ["5,432:5,432"] 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 pictures Dockerfile/redis, Dockerfile/elasticsearch and Paintedfox/postgresql.



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



The volumes definition of Redis and Postgres is for their information to be stored on the proxy VM, not in the container. This is why containers can be deleted or upgraded more, and data is not lost. The next step is to map these files from the proxy VM to OS X, but there's no need to let it run.



The ports on each container defines which port goes 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, by setting the environment variable Pghost=localhost pguser=root pgpassword=abcdef123456.



Working with vagrant



Within the same directory as your vagrant file, you can run:





Vagrant up--provider=docker#p# pagination title #e#





The first time you run this, vagrant will download and launch the proxy VM, then download and launch the Docker container. Each time you run vagrant after these initial downloads, the existing pictures are reused.



You can view the status of the Docker container:





Vagrant Status





Some things should be output similar to:



The code is as follows:




Current machine states:

Redis Running (Docker)
Elasticsearch Running (Docker)
DB Running (Docker)






To test whether the Docker container is running correctly, you can use Redis and Postgres clients, and curl for Elasticsearch. You only need to check the REDIS-CLI and psql to the server connection, and Curl http://localhost:9200 response.



If you need to connect to a proxy VM (very helpful for debugging), run Vagrant Global-status, which will list all VMS, including proxies. Then call vagrant SSH <id>,id as the proxy ID. It is recommended that you do not manually change this proxy VM, using a chef (or similar) script so that changes can be made easier to test and distribute.



Performance



When using virtualization, the first question always asks, "How much is the impact of performance?" ”。 To find out how bad the performance impact is, my colleagues and I have done a postgres and have done elasticsearch and Redis enhancement tests on the same hardware. The only difference is that one test has native installed software and the other has a Docker custom container. It ran for 2 minutes with the native software, and it ran for 3 minutes with the container.



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



#p # pagination Title #e#
Note: Some other limitations of using vagrant and Docker are listed here
Summarize



I still don't see where the "Vagrant with Docker" Road is. However, after seeing the possible situation, I couldn't help thinking about what other places it could be used for. Plus, it's the best virtualization I've ever had, and the fun is programming.



Tutorials for installing vagrant and Docker on Mac OS


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.