项目布局与流浪汉,码头和git [英] Project layout with vagrant, docker and git

查看:180
本文介绍了项目布局与流浪汉,码头和git的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我最近发现了docker和vagrant,而且我正在开始一个新的Php项目,我想使用这两个项目:



Vagrant为了有一个可互换的所有开发人员可以使用的环境。



Docker用于生产,也可以在流动的机器内部使开发环境尽可能接近生产环境。

第一种方法是将所有定义文件与源代码一起放在同一个存储库中,具有此布局:

  / docker 
/ machine1-web_server
/ Dockerfile
/ machine2-db_server
/ Dockerfile
/ machineX
/ Dockerfile
/ src
/ app
/ public
/ vendors
/ vagrant
/ Vagrantfile

所以,流动的机器在配置上运行所有docker机器,并正确设置数据库和源代码。



<这是一个好办法吗?我仍然试图找出在部署到生产环节的工作方式。

解决方案



是的,至少它适用于我几个月以来。



区别在于我还有一个 docker-compose .yml 文件。



在我的 Vagrantfile 中有一个安装docker,pip和docker-compose的配置部分:

  config.vm.provisionshell,inline:<< -SCRIPT 
if!类型docker> / dev / null;然后
echo -e\\\
\\\
=========安装docker ...
curl -sL https://get.docker.io/ | sh
echo -e\\\
\\\
=========安装docker bash完成...
curl -sL https://raw.githubusercontent.com/dotcloud / docker / master / contrib / completion / bash / docker> /etc/bash_completion.d/docker
adduser vagrant docker
fi
如果!键入pip> / dev / null;然后
echo -e\\\
\\\
=========安装pip ...
curl -sk https://bootstrap.pypa.io/get-pip .py | python
fi
如果!类型docker-compose> / dev / null;然后
echo -e\\\
\\\
=========安装docker-compose ...
pip install -U docker-compose
echo -e \\\
\\\
=========安装docker-compose命令完成...
curl -sL https://raw.githubusercontent.com/docker/compose/$(docker -compose --version | awk'NR == 1 {print $ NF}')/ contrib / completion / bash / docker-compose> /etc/bash_completion.d/docker-compose
fi
SCRIPT

和最后一个启动docker-compose的配置部分:

  .vm.provisionshell,inline:<< -SCRIPT 
cd / vagrant
docker-compose up -d
SCRIPT
/ pre>

其他方式从vagrant构建和启动码头集装箱,但使用docker-compose可以让我将Vagrantfile中的任何码头特征外在化。因此,这个Vagrantfile可以重用于其他项目,而无需更改;您只需要提供一个不同的 docker-compose.yml 文件。



另一件我做的不同的是将 Vagrantfile 放在项目的根目录(而不是 vagrant 目录中),因为它是一个人的地方和工具(某些IDE)期望找到它。 PyCharm做的,PhpStorm可能是这样的。



我还把我的 docker-compose.yml 文件放在我的根目录项目最后,为了开发,我只需要去我的项目目录,并启动vagrant,告诉docker-compose(最终建立)运行docker容器。







我仍然试图找出在部署到生产。


为了部署到生产,一个常见的做法是通过将它们发布到>私人 码头注册表。您可以在自己的基础架构上托管此类注册表,也可以使用提供 Docker Hub 的在线服务。



还向ops团队提供 docker-compose.yml 文件,将定义如何运行容器并链接它们。请注意,此文件不应使用 build: 指示,但依靠 image: 说明。谁要在部署到生产阶段时构建/编译东西?



这个

So I recently discovered docker and vagrant, and I'm starting a new Php project in which I want to use both:

Vagrant in order to have a interchangeable environment that all the developers can use.

Docker for production, but also inside the vagrant machine so the development environment resembles the production one as closely as possible.

The first approach is to have all the definition files together with the source code in the same repository with this layout:

/docker
   /machine1-web_server
       /Dockerfile
   /machine2-db_server
       /Dockerfile
   /machineX
       /Dockerfile
/src
   /app
   /public
   /vendors
/vagrant
   /Vagrantfile

So the vagrant machine, on provision, runs all docker "machines" and sets databases and source code properly.

Is this a good approach? I'm still trying to figure out how this will work in terms of deployment to production.

解决方案

Is this a good approach?

Yes, at least it works for me since a few months now.

The difference is that I also have a docker-compose.yml file.

In my Vagrantfile there is a 1st provisioning section that installs docker, pip and docker-compose:

config.vm.provision "shell", inline: <<-SCRIPT
    if ! type docker >/dev/null; then
        echo -e "\n\n========= installing docker..."
        curl -sL https://get.docker.io/ | sh
        echo -e "\n\n========= installing docker bash completion..."
        curl -sL https://raw.githubusercontent.com/dotcloud/docker/master/contrib/completion/bash/docker > /etc/bash_completion.d/docker
        adduser vagrant docker
    fi
    if ! type pip >/dev/null; then
        echo -e "\n\n========= installing pip..."
        curl -sk https://bootstrap.pypa.io/get-pip.py | python  
    fi
    if ! type docker-compose >/dev/null; then
        echo -e "\n\n========= installing docker-compose..."
        pip install -U docker-compose
        echo -e "\n\n========= installing docker-compose command completion..."
        curl -sL https://raw.githubusercontent.com/docker/compose/$(docker-compose --version | awk 'NR==1{print $NF}')/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose
    fi
SCRIPT

and finally a provisioning section that fires docker-compose:

config.vm.provision "shell", inline: <<-SCRIPT
    cd /vagrant 
    docker-compose up -d 
SCRIPT

There are other ways to build and start docker containers from vagrant, but using docker-compose allows me to externalize any docker specificities out of my Vagrantfile. As a result this Vagrantfile can be reused for other projects without changes ; you would just have to provide a different docker-compose.yml file.

An other thing I do differently is to put the Vagrantfile at the root of your project (and not in a vagrant directory) as it is a place humans and tools (some IDE) expect to find it. PyCharm does, PhpStorm probably does.

I also put my docker-compose.yml file at the root of my projects.

In the end, for developing I just go to my project directory and fire up vagrant which tells docker-compose to (eventually build then) run the docker containers.


I'm still trying to figure out how this will work in terms of deployment to production.

For deploying to production, a common practice is to provide your docker images to the ops team by publishing them on a private docker registry. You can either host such a registry on your own infrastructure or use online services that provides them such as Docker Hub.

Also provide the ops team a docker-compose.yml file that will define how to run the containers and link them. Note that this file should not make use of the build: instruction but rely instead on the image: instruction. Who wants to build/compile stuff while deploying to production?

This Docker blog article can help figuring out how to use docker-compose and docker-swarm to deploy on a cluster.

这篇关于项目布局与流浪汉,码头和git的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆