简体   繁体   English

使用PHP,MySQL,nginx同时运行多个Dockerize项目

[英]Running multiple Dockerize projects at the same time using PHP, MySQL, nginx

Problem 问题

I have projects with docker-compose configuration with same configuration (PHP, MySQL, nginx). 我有使用相同配置(PHP,MySQL,nginx)的docker-compose配置的项目。 When I run one project with docker-compose up there is not a problem, but I need to switch between them to few times per day for development. 当我使用docker-compose up运行一个项目时没有问题,但我需要每天在它们之间切换几次以进行开发。

The problems: 问题:

  • the share configurations for same port for nginx nginx的相同端口的共享配置
  • share configuration for default DB and connection (name and users) 共享默认数据库和连接的配置(名称和用户)
  • domains for each project 每个项目的域名

I have 我有

  • Docker for Mac - edge Docker for Mac - edge

For now 目前

Right now simply run up and down -v each time. 现在简单地运行up ,并down -v各一次。 Mostly, project are on the same PHP version (should be) and MySQL. 大多数情况下,项目都是相同的PHP版本(应该是)和MySQL。

Ideas 思路

Load balancer 负载均衡器

One of idea is to create configuration in main folder workspace , docker-compose with some loadbalancer that will is some rules will generate domain for given project with volume as main folder and with one MySql server and few DB - but this is raw idea. 其中一个想法是在主文件夹workspace创建配置, docker-compose与一些loadbalancer,这将是一些规则将为给定项目生成域,其中volume为主文件夹,并且有一个MySql服务器和几个DB - 但这是一个原始想法。

Question

What solution is work with given situation? 什么解决方案适用于特定情况?

  • propose any structure or docker configuration to solve independent configurations? 提出任何结构或泊坞窗配置来解决独立配置?

Update 更新

My docker-compose files 我的docker-compose文件

version: '2.1' 版本:'2.1'

Single server setup for dev dev的单服务器设置

services:
  app:
    image: ${PHP_IMAGE}
    volumes:
     - ${COMPOSE_DIR}/../../:/var/www:cached
     - ${COMPOSER_HOME}:/root/.composer:cached
    depends_on:
     - db
    environment:
     - SYMFONY_ENV=${SYMFONY_ENV-prod}
     - SYMFONY_DEBUG
     - SYMFONY_HTTP_CACHE
     - SYMFONY_HTTP_CACHE_CLASS
     - SYMFONY_TRUSTED_PROXIES
     - DATABASE_USER
     - DATABASE_PASSWORD
     - DATABASE_NAME
     - DATABASE_HOST=db

  web:
    image: ${NGINX_IMAGE}
    volumes_from:
     - app:ro
    ports:
     - "8080:80"
    environment:
     - SYMFONY_ENV=${SYMFONY_ENV-prod}
     - MAX_BODY_SIZE=20
     - FASTCGI_PASS=app:9000
     - TIMEOUT=190
     - DOCKER0NET
    command: /bin/bash -c "cd /var/www && cp -a doc/nginx/ez_params.d /etc/nginx && bin/vhost.sh --template-file=doc/nginx/vhost.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"

  db:
    image: ${MYSQL_IMAGE}
    volumes:
     - ${COMPOSE_DIR}/entrypoint/mysql:/docker-entrypoint-initdb.d/:ro
    environment:
     - MYSQL_RANDOM_ROOT_PASSWORD=1
     - MYSQL_USER=$DATABASE_USER
     - MYSQL_PASSWORD=$DATABASE_PASSWORD
     - MYSQL_DATABASE=$DATABASE_NAME
     - TERM=dumb

Given this, As I told, I would want to have running projects with different PHP version. 鉴于此,正如我所说,我希望运行具有不同PHP版本的项目。 Each have same nginx configuration, then need somehow have possibility to run them, easily and simultaneously. 每个都有相同的nginx配置,然后需要以某种方式有可能轻松和同时运行它们。

Understand answer of @Vladimir Kovpak, but working this way manually will generate a lot of work. 理解@Vladimir Kovpak的答案,但手动这种方式会产生很多工作。 Each project have own docker-compose config. 每个项目都有自己的docker-compose配置。

What would be perfect 什么是完美的

Scenario 1 场景1

  • Go to project, run command to build correctly by docker-compose 转到项目,运行命令以通过docker-compose正确构建
    • Could do dev work 可以做开发工作
    • could have accessible page via some dynamic domains 可以通过一些动态域访问页面
  • run command to close project 运行命令关闭项目

Scenario 2 情景2

  • Go to project, run command to build correctly by docker-compose 转到项目,运行命令以通过docker-compose正确构建
  • Go to other project, run command to build correctly by docker-compose 转到其他项目,运行命令以通过docker-compose正确构建
  • Go to other project, run command to build correctly by docker-compose 转到其他项目,运行命令以通过docker-compose正确构建
    • Could do dev work 可以做开发工作
    • could have accessible page via some dynamic domains 可以通过一些动态域访问页面
  • run command to close project 运行命令关闭项目

Could work with all project at same time. 可以同时使用所有项目。 The first step is proposition - any initialisation and closing would be fine, as not painful - should be quick and easy. 第一步是提议 - 任何初始化和结束都没关系,因为不痛苦 - 应该快速而简单。

I started to use Portainer which once the containers are set-up has a web page which allows you to start, stop and manage the running containers. 我开始使用Portainer ,一旦设置了容器,就会有一个网页,允许您启动,停止和管理正在运行的容器。

Portainer itself is a docker machine, so I start this at machine start-up and then in the web browser I can see the MySQL, PHP and phpmyadmin containers I've already set-up. Portainer本身就是一个docker机器,所以我在机器启动时启动它,然后在Web浏览器中我可以看到我已经设置的MySQL,PHP和phpmyadmin容器。 You can start and stop them individually and also execute a console into the machine via the browser. 您可以单独启动和停止它们,也可以通过浏览器将控制台执行到机器中。

The browser also shows you machine usage stats, IP addresses and port forwarding details, so plenty of useful info in your browser. 浏览器还会显示机器使用情况统计信息,IP地址和端口转发详细信息,以及浏览器中的大量有用信息。

Lastly, it also allows you to remotely manage docker machines, so I've set-up my desktop with docker and I can manage the containers on my laptop. 最后,它还允许您远程管理docker机器,因此我使用docker设置我的桌面,我可以管理笔记本电脑上的容器。

For nginx : 对于nginx
you can specify different ports like: 你可以指定不同的端口,如:

docker run -ti --name nginx-1 -p 8081:80 nginx:latest
docker run -ti --name nginx-2 -p 8082:80 nginx:latest

Or in nginx config you can specify different server_name s, like: 或者在nginx配置中,您可以指定不同的server_name ,例如:

nginx-1 -> server_name one.dev;
nginx-2 -> server_name two.dev;

For mysql more simpler: 对于mysql更简单:

You can use different ports like with nginx 您可以使用与nginx类似的不同端口
or different hostnames 或不同的hostnames

docker run -it --rm -p 3308:3306 --name mysql-1 --hostname mysql-1 mysql:latest
docker run -it --rm -p 3308:3306 --name mysql-2 --hostname mysql-2 mysql:latest

Or you can have 1 mysql, but with different databases, or with different users... 或者你可以有1个mysql,但是有不同的数据库,或者有不同的用户......

For load balancing: 用于负载平衡:
you can use haproxy and have config like: 你可以使用haproxy并具有如下配置:

# nginx
mode tcp
balance roundrobin
server nginx-1 localhost:8081 check port 8081 inter 1000
server nginx-2 localhost:8082 check port 8082 inter 1000
# mysql
# etc

PS: I've provided examples not in docker-compose format, but I hope you will get a gist... And hope it isn't difficult to convert this commands into docker-compose format. PS:我提供的示例不是docker-compose格式,但我希望你能得到一个要点...并希望将这些命令转换为docker-compose格式并不困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM