How to use vagrant and Docker to manage a concise tutorial on creating Virtual environments under OS X

Source: Internet
Author: User
Tags openssl sha1 ssh git clone docker ps docker run in python

Virtualization: In fact, it is because of the technology of cloud computing, the purpose is to let the application running on the above feel that they monopolize all the resources. The plain thing is resource management/isolation and namspace isolation.

Hypervisor: This is one of the virtualization, that is, the operating system running one or more operating systems, the underlying operating system is Hypervisor. It manages and assigns the resources that are required for the operating system that is created. This virtualization can also be divided into 2 kinds: native ( Run the virtual operating system directly on bare metal, such as KVM and Xen, to determine the need for specific hardware support and the host (software level, such as the VirtualBox I will use here). But there's a problem: running multiple operating system instances is expensive, and starting a system takes away a portion of the resources

LXC (LinuX containers): An operating system-level virtualization technology. It runs only one kernel, and a virtual execution environment is a container. You can bind specific CPU and memory nodes to a container, assign a specific percentage of CPU time, IO time, limit the amount of memory that can be used, and so on. Unfortunately LXC only supports Linux and does not support bsd,osx,windows, so I am following in my Mac VirtualBox to build the container. As for why choose LXC, I think in addition to the cost is small, is convenient quick deployment

Docker: A Linux Container (LXC) technology that adds advanced APIs to DotCloud, the official website describes: Provides a lightweight virtualization solution that can run UNIX processes independently. It provides a way to automate the deployment of software in a secure, repeatable environment. In Infoq you can find the main features, of course, 3 months later, it must have grown a lot

Vagrant: is a Ruby language tool. Used to create and deploy a virtualized development environment. We use vagrant to install Docker in VirtualBox's virtual machine.

Some ideas for what system to use Docker

With OS X, it's a bit ... Well, it's too round. In fact, Docker has been officially supported by Ubuntu, adding a docker ppa and then installing it.

You can also encapsulate the installation process into Dockfile, and use Git as a version control

Who needs to learn this article

I think the Universiade need some more. or hardware resources lack of the program members

Programmers can learn to put the development of the application into a virtual machine template, you can use Vagrant Management

Why do you say that? We have a virtualenv+virtualenvwrapper in python for the sake of not polluting the development environment.

For a programmer like me, I don't need so many virtual machines, then I have a test environment for physical machines, and we have a fairly consistent operating system on the line. This article is to understand Docker

install vagrant and VirtualBox

Vagrant is based on VirtualBox, you can use gem to install

Gem Install vagrant

I like the latest version, manually installed

wget https://github.com/mitchellh/vagrant/archive/v1.2.2.tar.gz
Tar zxvf v1.2.2.tar.gz
CD vagrant-1.2.2
sudo gem install bundler
Bundle Install
Rake Install

cloning Docker from GitHub

git clone https://github.com/dotcloud/docker.git
CD Docker
Vagrant up

' Vagrant up ' is actually calling the VirtualBox terminal interface to open your virtual machine, like the following command, rather than clicking ' Start ' on the interface

/applications/virtualbox.app/contents/macos/vboxheadless--comment docker_1373340358--STARTVM 6e4ea638-77b2-400d-9fbe-3b4e8d4fba21--vrde Config

At this point, you can see the virtual machine starting in the VirtualBox interface, the name is the docker_1373340358

In the Docker directory has a vagrantfile file, you think of makefile, meaning very similar, in fact, why this Docker source, a big reason is to use this file, because it has configured the default you use the box file, similar to the implementation of the following command

Vagrant Init ubuntu Http://files.vagrantup.com/http://files.vagrantup.com/precise64.box
. #一些配置
Vagrant up

First of all, there may be an error, similar to the content of the Web page pasted, the content has similar hints

INFO Interface:error:There was a error executing the following command with
Vboxmanage:

["Import", "C:/users/kwladyka/desktop/my Dropbox/vagrant/vagrant.d/boxes/base/bo
X.OVF "]

For more information on the failure, enable detailed logging with
Vagrant_log.

The solution is:

cd/users/dongwm/.vagrant.d/boxes/ubuntu/virtualbox/
OpenSSL SHA1 *.vmdk *.ovf > BOX.MF
CD-

But the official website said this will automatically help us to increase the Docker PPA, update the source to install Lxc-docker, but did not appear, I later are the implementation of their own, as follows

Why Use this box? Of course, other systems are available, you can download other virtual machine images, and then install the Lxc-docker under its system (I will prepare a gentoo version of the article)

Log on to a virtual machine

Vagrant SSH

In fact, through 2222 of the port forwarding log on to the virtual machine 22 port, the actual command similar to this:

SSH vagrant@127.0.0.1-p 2222-o Loglevel=fatal-o stricthostkeychecking=no-o Userknownhostsfile=/dev/null-o
Identitiesonly=yes-i/users/dongwm/.vagrant.d/insecure_private_key

My own way of installing Lxc-docker

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:dotcloud/lxc-docker
sudo apt-get update
sudo apt-get install Lxc-docker

Build the container (virtual machine) and create a sshd process so that the user can link to the virtual machine

Website said download dhrp/sshd This container, but I spend half a day to download after the Magic 404 ... Then I changed another one, this pull is actually to download a has been done by others to implement a certain function of the virtual machine, and then you use this implementation of the basic functions of the virtual machine, submit your changes will become your custom version, of course, you can make a container

Docker Pull Base #pull基本的ubuntu镜像
Docker run-i-T Base/bin/bash #-i to enter interactive mode,-T to start a terminal, in fact, based on the base image into a container
root@4a61c288be0f:/# apt-get Update && apt-get install Openssh-server #注意前面的PS, root permissions, enter the container, need to install sshd first
root@4a61c288be0f:/# mkdir/var/run/sshd #要不然启动不了sshd
root@4a61c288be0f:/#/usr/sbin/sshd #启动sshd进程, different systems may be in different locations
root@4a61c288be0f:/# passwd #设置root密码, later to SSH login drops
root@4a61c288be0f:/# exit #退出容器 But remember a row of random numbers in front of PS this is 4a61c288be0f.
Docker ps-a |less #查看都有那些容器, can also see the above
Docker commit 4a61c288be0f dongweiming/sshd #这里的commit号码就是刚才记住的. This state is saved to dongwm/sshd, then you don't have to download openssh-server, set the root password

If you want something simple, just do it.

Docker Pull Johnfuller/sshd
Docker Run-i-T Johnfuller/sshd/bin/bash
root@2fd1aabac314:/#/usr/sbin/sshd
root@2fd1aabac314:/# passwd
root@2fd1aabac314:/# exit
Docker commit 2fd1aabac314 Dongweiming/sshd

Here is a status column, exit code has 0,1,2,127 and so on, verify that your container is running normally, there are ports columns behind, if sshd start will show

Start the container with my own sshd container

rst=$ (Docker run-d dongwm/sshd-p 22/usr/sbin/sshd-d) #执行这个会返回类似的字符数字串
Docker PS #你会发现它启动了
port=$ (Docker port $rst) #这会显示在实际的虚拟机的这个容器专用的端口

SSH link this container


Ifconfig #找你本机的ip
SSH root@192.168.33.10-p $port #port seems to be logged in by default from 49153, so enter the root password.

OK, that's it, then of course you can do this for this virtual machine and your native (note three keywords: native (my Mac), Virtual machine (vagrant created vbox), container (virtual machine created in VBox, virtual machine in VM))
Push your source to Docker and you need to register. Can be used by others, of course, for now.

Docker Login
Docker push Dongweiming/sshd #要确保这个源没有被用, I'm here dongweiming and dongweiming/sshd

so you want to find a source of sshd in the future, you can do this.

Docker Pull Dongweiming/sshd

But there's a lot of other stuff in there, look down.

Port Forwarding

You think, you're creating a bunch of containers inside a virtual machine, he can connect the local, and even the Internet, but you can not directly connected to it, then the need for magic port forwarding, your local port data forwarding to this VBox port, and then it can look straight, in fact, they have achieved

Back to the top of the Vagrantfile, is the root directory of Docker. The last Side

if! Forward_docker_ports.nil?
Vagrant::version < "1.1.0" and Vagrant::config.run do |config|
(49000..49900). Each do |port|
Config.vm.forward_port Port, Port
End
End

Vagrant::version >= "1.1.0" and Vagrant.configure ("2") do |config|
(49000..49900). Each do |port|
Config.vm.network:forwarded_port,: Host => Port,: Guest => port
End
End

What do you mean? In fact, you set the environment variable forward_docker_ports, the default will be 49000. 49900 Port Forwarding Open

Export forward_docker_ports=true and then vagrant up

Other system mirrors


I wonder if you noticed that the virtual machine we installed on OS X is Ubuntu, and that the Docker managed container is also Ubuntu, if you want to use other operating system containers?

Like looking for Debian, you can use

Docker Search Debian

or search the index Web site at Docker.

Https://index.docker.io

I chose Tianon/debian.

Docker Pull Tianon/debian

For example, or SSH just now, T does not refer to base but Tianon/debian

Docker Run-i-T Tianon/debian/bin/bash

Use Docker to create a Python web environment where I write the flask_reveal


The above is a container, when you have root permissions to install anything can, for me can also set a specific application settings for the environment, according to the official website instructions

Docker pull Dongweiming/sshd #一个构建python的web环境的镜像, according to the above sshd I added some scripts inside, and then downloaded Curl,python-pip and so built an environment
Url=https://github.com/dongweiming/flask_reveal/archive/v1.0.tar.gz #这是我提供的release版本
build_job=$ (Docker run-d-T Dongweiming/sshd:latest/usr/local/bin/buildapp $URL) #其实就是创建一个容器, download this tar.gz installation package
# decompression, enter the directory under the pattern needs to install dependent PIP Install-r requirements.txt
Docker Attach $BUILD _job #因为刚才的任务就是下载解压你可以这样去查看这个任务的执行过程
build_img=$ (Docker commit $BUILD _job reveal) #把刚才的下载提交后commit到新的镜像 named reveal. Here please note [commit] (http://docs.docker.io/en/ The use of Latest/commandline/command/commit),
# Plus '-run ' can run some kind of service you want, set memory, host name, port forwarding, DNS, environment variables, etc.
web_worker=$ (Docker run-d-P 5000 $BUILD _img/usr/local/bin/runapp) #应用跑起来, this runapp was written by myself, starting MongoDB, according to my reveal boot process
Docker logs $WEB _worker #可以查看manage. PY has been activated.
web_port=$ (Docker PORT $WEB _worker 5000) #使用虚拟机的一个端口映射到这个容器的5000端口
http://127.0.0.1: $WEB _port #好吧 You can access it through this machine

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.