L
L
lolrofl012019-11-29 15:05:41
Web development
lolrofl01, 2019-11-29 15:05:41

How to work with multiple projects in docker?

Good afternoon.
I decided to switch from vagrant to docker. I read a lot of manuals, the essence is the same everywhere - we write the config, then docker-compose build and up and go to localhost: 8080 - everything works there. I tried it - it really works. But here questions arise. I worked with openserver before, then switched to vagrant. I have about 20 projects in the same environment (because they are all on php7.3, mysql 5.7, nginx) In general, there are no differences, so they all worked fine on the same vagrant virtual machine. I really liked the process of writing url => path. As a result, all 20 projects could be opened at the same time - they all worked.
But here I am on docker and I have the same questions:
1) How to make all projects work at the same time?
2) How to give projects not localhost:8080, but a normal url like project.dev?
Something about the docker is the same info everywhere, some stories about the advantage of containers, some "let's run localhost: 8080", and step left and right - I can't find any info ... anyway, I didn't succeed.
Thanks in advance for your replies.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
S
Sergey Sokolov, 2019-11-29
@lolrofl01

all on php7.3, mysql 5.7, nginx

Since all projects have one environment, it is enough to launch one instance of php-fpm, mysql, nginx and use them from all developed projects.
"Project", it turns out, is a folder with files, a separate database or tables on a common MySQL server, and registered in the nginx configs
location /project-42 { root /projects/project-42; ... }

### или целый блок

server {
  listen 80;
  server_name  project42.dev;
  ... 
}

Perhaps it’s better to work through docker-compose : the entire config is in one file , the common network, services see each other, but at the same time they don’t shine extra ports out.
Pick up one of the projects first. Then figure out how to add the rest of the files via volumes :
nginx:
  volumes:
    - "/freelance/projects/Project-0/:/var/www/project0"
    - "/freelance/projects/Project-42/:/var/www/project42"
    # ...
php-fpm:
  volumes:
    # то же самое сюда

In the nginx service config, write in docker-compose.ymlinstead of 8080:80- 80:80, and in the local hosts file , add 127.0.0.1 project.dev project42.dev

B
bankinobi, 2019-12-01
@bankinobi

It will be more pleasant to work with so many projects if you pass them all through traefik. Additional 3-4 lines of notifications to the project description in the docker-compose file and everything is seen through one entry point.

T
Tonik, 2019-12-01
@Tonik

You are looking for jwilder/nginx-proxy on the github (for some reason it does not allow you to post a link)

A
andrei_pro, 2019-11-29
@andrei_pro

1. If the projects are different, then you will have to resolve external ports 8081:80, 8082:80, if the project is one of several services, you can make 1 proxy with port 80 and redirect to containers with php-fpm
2. In /etc/hosts add your domains on ip 127.0.0.1. Use local instead of dev

D
dmitriy, 2019-12-01
@dmitriylanets

And why do you need to work with several projects at the same time, raised the containers through the docker-compose of the site, worked, paid off, switched to another project, raised the network, worked, closed, etc.

A
Alexander Grebenshchikov, 2019-12-05
@archerz

The default port and host can be set via variables in docker-compose.yml:

version: '3.4'

services:
  app:
    build:
      context: ..
      dockerfile: ./docker/Dockerfile
    restart: ${APP_RESTART_MODE:-always}
    volumes:
      - static:/usr/src/app/static
      - ${APP_PUBLIC_DIR:-./data}:/usr/src/app/public:rw
    environment:
      APP_SERVER_HOSTS: ${APP_SERVER_HOSTS:-www.project.com}
      APP_SERVER_PORT: ${APP_SERVER_PORT:-8080}
  nginx:
    build:
      context: .
      dockerfile: ./Dockerfile-nginx
    restart: ${APP_RESTART_MODE:-always}
    environment:
      NGINX_HOST: ${APP_SERVER_HOSTS:-www.project.com}
      NGINX_PORT: ${APP_SERVER_PORT:-8080}
    ports:
      - "${APP_SERVER_PORT:-8080}:${APP_SERVER_PORT:-8080}"
    volumes:
      - static:/usr/src/app/static
      - ${APP_PUBLIC_DIR:-./data}:/usr/src/app/public:ro
    command: /bin/ash -c "envsubst < /etc/nginx/conf.d/default.conf.tpl > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
    depends_on:
      - app
    links:
      - app

And you can change the default values ​​in the .env file (should be in the same folder as docker-compose.yml):
APP_RESTART_MODE=unless-stopped
APP_SERVER_HOSTS=project42.local www.project42.com
APP_SERVER_PORT=8181

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question