docker - 如何使用 Docker 让 Laravel 和 Lumen 项目同时运行?

标签 docker

我想知道是否可以使用 Docker 同时运行 Laravel + Lumen?因此,我在单独的文件夹中创建了 Laravel 和 Lumen 的全新安装,因此我将使用 Lumen 作为 API,并且我想知道是否可以仅使用一个容器来运行它们。

我已经尝试过更改端口,但仍然没有运气,Laravel 运行顺利,但 Lumen 运行不佳。

这是文件,它们是相同的,但区别只是端口(我不知道我所做的是否正确)

docker-compose.yml

version: '2'
services:
  web:
    build:
      context: ./
      dockerfile: web.dockerfile
    volumes:
      - ./:/var/www
    ports:
      - "8080:80"
    links:
      - app

  app:
    build:
      context: ./
      dockerfile: app.dockerfile
    volumes:
      - ./:/var/www
    links:
      - database
    environment:
      - "DB_PORT=3306"
      - "DB_HOST=database"

  database:
    image: mysql:5.6
    environment:
        MYSQL_ROOT_PASSWORD: secret
        MYSQL_DATABASE: dockerApp
    ports:
        - "33061:3306"

  cache:
    image: redis:3.0
    ports: 
      - "63791:6379"

app.dockerfile

FROM php:7.1.19-fpm

RUN apt-get update && apt-get install -y libmcrypt-dev mysql-client \
    && docker-php-ext-install mcrypt pdo_mysql

WORKDIR /var/www

web.dockerfile

FROM nginx:1.10

ADD ./vhost.conf/ /etc/nginx/conf.d/default.conf

WORKDIR /var/www

vhost.conf

server {
    listen 80;
    index index.php index.html;
    root /var/www/public;

    location / {
        try_files $uri /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;

        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}
docker ps

CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS              PORTS                                      NAMES
33987802df30        test-restful-api_web            "nginx -g 'daemon of…"   34 hours ago        Up About a minute   80/tcp, 443/tcp, 0.0.0.0:32768->8001/tcp   test-restful-api_web_1
7f4b767ff2af        test-restful-api_app            "docker-php-entrypoi…"   34 hours ago        Up About a minute   9000/tcp                                   test-restful-api_app_1
be0675a6804d        redis:3.0                       "docker-entrypoint.s…"   34 hours ago        Up About a minute   6379/tcp, 0.0.0.0:63793->63792/tcp         test-restful-api_cache_1
f0b5fe2db9a4        mysql:5.6                       "docker-entrypoint.s…"   34 hours ago        Up About a minute   3306/tcp, 0.0.0.0:33063->33062/tcp         test-restful-api_database_1
bdb308c7288f        test_web                        "nginx -g 'daemon of…"   34 hours ago        Up About a minute   443/tcp, 0.0.0.0:8080->80/tcp              test_web_1
a74a1b5c2ec7        test_app                        "docker-php-entrypoi…"   34 hours ago        Up About a minute   9000/tcp                                   test_app_1
690b553eae36        mysql:5.6                       "docker-entrypoint.s…"   34 hours ago        Up About a minute   0.0.0.0:33061->3306/tcp                    test_database_1
fc47a3a84484        redis:3.0                       "docker-entrypoint.s…"   34 hours ago        Up About a minute   0.0.0.0:63791->6379/tcp                    test_cache_1

我想要实现的目标

我想在托管 test.com (主网站)api.test.com (对于restful api),但是在我的机器上,我想像这样运行它(如果可能的话) 本地主机:8080(网站) 本地主机:8000/8001(restful api)

更新

我只是对目录进行了更改,之前是这样的

all_project_folder
---- test // front end project folder which is using Laravel 5.8
-------- * bunch of laravel things
-------- web.dockerfile
-------- app.dockerfile
-------- vhost.conf
-------- docker-compose.yml
---- test-restful-api // restful api project folder which is using Lumen 5.8
-------- * bunch of laravel things
-------- web.dockerfile
-------- app.dockerfile
-------- vhost.conf
-------- docker-compose.yml

但是现在我已经把它改成了这个结构

all_project_folder
---- test // folder for grouping the api and frontend
-------- frontend // test frontend folder which is laravel
-------- * bunch of laravel things
------------ web.dockerfile
------------ app.dockerfile
------------ vhost.conf
-------- restful-api // test api folder which is lumen
------------ web.dockerfile
------------ app.dockerfile
------------ vhost.conf
-------- docker-compose.yml // now there's only 1 docker-compose file, and it's outside those 2 folders (and the project can be added something like backend)

那么现在如何使用版本3设置dockerfile?我已经尝试阅读文档,但我仍然没有明白,并且有多个示例彼此不同,所以我不确定应该使用哪一个。 (抱歉,我对这个服务器很陌生)

enter image description here

docker-compose.yml我的最新方法返回错误错误:服务“web”使用未定义的网络“前端”

version: '3'

services:  
  web:
    build:
      context: ./
      dockerfile: frontend/web.dockerfile
    volumes:
      - ./:/var/www
    ports:
      - "8080:80"
    networks:
      - frontend
      - api

  frontend:
    build:
      context: ./
      dockerfile: frontend/app.dockerfile
    volumes:
      - ./:/var/www
    networks:
      - database
    environment:
      - "DB_PORT=3306"
      - "DB_HOST=database"

  api:
    build:
      context: ./
      dockerfile: restful-api/app.dockerfile
    volumes:
      - ./:/var/www
    networks:
      - database
    environment:
      - "DB_PORT=3306"
      - "DB_HOST=database"

  database:
    image: mysql:5.6
    environment:
        MYSQL_ROOT_PASSWORD: secret
        MYSQL_DATABASE: dockerApp
    ports:
        - "33061:3306"

  cache:
    image: redis:3.0
    ports: 
      - "63791:6379"

我刚刚将网络添加到我的 docker-compose.yml 但出现此错误

ERROR: The Compose file '.\docker-compose.yml' is invalid because: Unsupported config option for services.networks: 'frontend'

networks:
    frontend: 
      driver: asisten-pajak-frontend
    api:
      driver: asisten-pajak-api

最佳答案

经过几天的尝试了解docker,我终于找到了答案,最后,我得到的答案是使用Laradock ,你可以关注这个Laradock documentation对于多个项目/容器。

关于docker - 如何使用 Docker 让 Laravel 和 Lumen 项目同时运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55028721/

相关文章:

sockets - 从 docker 容器中打开 DGRAM 套接字失败(权限被拒绝)

docker - 如何从 ubuntu 14.04 中完全删除 docker

linux - 在Linux上的Docker中查看交互式Cypress测试运行器

amazon-web-services - 使 Docker 容器内的目录可从另一个容器访问

r - R Studio在未初始化时崩溃。错误:传输期间发生错误

docker - 为什么我得到 Host is unreachable from React code 而不是浏览器?

docker - 如何在 Gitlab CI 中使用 Dockerfile

ubuntu - 如何在 docker 中更改 Ubuntu 的主机名?

ruby-on-rails - docker sh : 1: yarn: not found

jenkins - 在 Windows 中运行 jenkins 容器的 docker 命令是什么