Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). 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.

The problems:

  • the share configurations for same port for nginx
  • share configuration for default DB and connection (name and users)
  • domains for each project

I have

  • Docker for Mac - edge

For now

Right now simply run up and down -v each time. Mostly, project are on the same PHP version (should be) and 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.

Question

What solution is work with given situation?

  • propose any structure or docker configuration to solve independent configurations?

Update

My docker-compose files

version: '2.1'

Single server setup for 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. Each have same nginx configuration, then need somehow have possibility to run them, easily and simultaneously.

Understand answer of @Vladimir Kovpak, but working this way manually will generate a lot of work. Each project have own docker-compose config.

What would be perfect

Scenario 1

  • Go to project, run command to build correctly by docker-compose
    • Could do dev work
    • could have accessible page via some dynamic domains
  • run command to close project

Scenario 2

  • Go to project, run command to build correctly by docker-compose
  • Go to other project, run command to build correctly by docker-compose
  • Go to other project, run command to build correctly by 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.

like image 660
timiTao Avatar asked Jul 11 '17 10:07

timiTao


2 Answers

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 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. 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.

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.

like image 199
Nigel Ren Avatar answered Nov 14 '22 20:11

Nigel Ren


For 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_names, like:

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

For mysql more simpler:

You can use different ports like with nginx
or different 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...

For load balancing:
you can use haproxy and have config like:

# 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.

like image 36
cn007b Avatar answered Nov 14 '22 21:11

cn007b