【问题标题】:How to use Laravel docker container & MySQL DB with a Vue one?如何将 Laravel docker 容器和 MySQL DB 与 Vue 一起使用?
【发布时间】:2021-02-11 18:02:44
【问题描述】:

我有一个使用 Vue CLI 作为前端Laravel 作为后端的应用程序。现在我正在尝试使用 docker 在服务器上启动我的应用程序。

我的 docker 技能只能让我做一件事:Vue docker container。但就我必须使用 Laravel 作为后端而言,我也必须为此创建一个容器(当然是 MySQL)。

所以我得到了:Dockerfile

FROM node:lts-alpine
WORKDIR /app
COPY package*.json ./
RUN  npm install
EXPOSE 8080
CMD ["npm", "run", "serve"]

docker-compose.yml

version: '3'
services:
    web:
        build: .
        stdin_open: true
        tty: true
        ports: 
            - "8080:8080"
        volumes:
            - "/app/node_modules" 
            - ".:/app"

问题是我了解如何将 Laravel 连接到 Dockerfile。它只是在我的脑海中没有加起来。

我应该使用 Ubuntu,而不仅仅是 node?无论如何,我再次请求您的支持

【问题讨论】:

  • 您需要添加 2 个新服务:laravelmysql
  • @Stefano,我猜你是对的,但我不知道该怎么做:(
  • 当前的可以正常工作。我看到的唯一问题是在与后端通信时定义前端使用的 url。我建议阅读代理反向和 nginx
  • 你已经走上了一条好路。添加东西到你的 docker-compose.yml,如果你有一个包含 laravel 的文件夹,为它生成一个 Dockerfile(如果你需要根据你的需要调整原始图像)等等
  • 没有进展兄弟.. @Stefano

标签: laravel docker vue.js docker-compose dockerfile


【解决方案1】:

您将需要 4 个容器,在 docker-compose 文件中定义:

  • 前端(您已经拥有的 Vue 应用程序)
  • 后端(Laravel 应用程序)
  • Web 服务器(例如 Nginx 或 Apache)
  • 数据库(MySQL)

可以将 'web-server' 和 'backend' 容器合二为一,但这通常是个坏建议。 您的 compose 文件将类似于以下内容:

version: '3'
services:
  frontend:
    build: ./frontend
    ports: 
      - 8080:8080
    volumes:
      - ./frontend:/app

  backend:
    build: ./backend
    volumes:
      - ./backend:/var/www/my_app
    environment:
      DB_HOST=db
      DB_PORT=3306

  webserver:
    image: nginx:alpine
    ports:
      - 8000:8000
    volumes:
      - ./backend:/var/www/my_app

  database:
    image: mariadb:latest
    container_name: db
    ports:
      - 3306:3306
    environment:
      MYSQL_DATABASE: dbname
      MYSQL_ROOT_PASSWORD: dbpass
    volumes:
      - ./sql:/var/lib/mysql

其中./backend 包含 Laravel 应用程序代码,./frontend 包含 Vue 应用程序,两者都包含 Dockerfile。请参阅Docker Hub,了解所需每个图像的具体说明。这会向您的主机系统公开 3 个端口:8080(Vue 应用)、8000(Laravel 应用)和3306(MySQL)。

或者,如果您在 Laravel 容器中使用 artisan cli 的 serve 命令,则可以省略 Web 服务器,类似于您在 Vue 应用程序中已经在 Dockerfile 中执行的操作。

图片必须包含CMD php artisan serve --host=0.0.0.0 --port=8000之类的内容

【讨论】:

  • 非常感谢。我给你赏金,但我有一个问题.. laravel 迁移怎么样?我该如何运行它?
  • @TyomaInagamov 最好的方法是让你的 Laravel 容器镜像在启动时执行此操作;使您的图像的入口点运行 bash 脚本,而不是直接运行 artisan serve,并让该脚本在服务之前运行迁移。这样,每次启动容器时都会迁移最新的架构(例如,当您部署新版本的应用程序时)。
【解决方案2】:

根据this article,您需要按照以下步骤操作。

  1. 使您的项目文件夹如下所示:(d: 目录,f: 文件)
d: backend
d: frontend
d: etc
    d: nginx
        d: conf.d
            f: default.conf.nginx
    d: php
        f: .gitignore
    d: dockerize
        d: backend
            f: Dockerfile
f: docker-compose.yml
  1. 添加 docker-compose.yml
    version: '3'
    services:
    www:
        image: nginx:alpine
        volumes:
            - ./etc/nginx/conf.d/default.conf.nginx:/etc/nginx/conf.d/default.conf
        ports:
            - 81:80
        depends_on:
            - backend
            - frontend

    frontend:
        image: node:current-alpine
        user: ${UID}:${UID}
        working_dir: /home/node/app
        volumes:
            - ./frontend:/home/node/app
        environment:
            NODE_ENV: development
        command: "npm run serve"

    backend:
        build:
            context: dockerize/backend
        # this way container interacts with host on behalf of current user.
        # !!! NOTE: $UID is a _shell_ variable, not an environment variable!
        # To make it available as a shell var, make sure you have this in your ~/.bashrc (./.zshrc etc):
        # export UID="$UID"
        user: ${UID}:${UID}

        volumes:
            - ./backend:/app
            # custom adjustments to php.ini
            # i. e. "xdebug.remote_host" to debug the dockerized app
            - ./etc/php:/usr/local/etc/php/local.conf.d/
        environment:
            # add our custom config files for the php to scan
            PHP_INI_SCAN_DIR: "/usr/local/etc/php/conf.d/:/usr/local/etc/php/local.conf.d/"
        command: "php artisan serve --host=0.0.0.0 --port=8080"

    mysql:
        image: mysql:5.7.22
        container_name: mysql
        restart: unless-stopped
        tty: true
        ports:
            - "4306:3306"
        volumes:
            - ./etc/mysql:/var/lib/mysql
        environment:
            MYSQL_DATABASE: tor
            MYSQL_USER: root
            MYSQL_PASSWORD: root
            MYSQL_ROOT_PASSWORD: root
            SERVICE_TAGS: dev
            SERVICE_NAME: mysql
  1. 添加default.conf.nginx
    server {
        listen 81;
        server_name frontend;
        error_log  /var/log/nginx/error.log debug;

        location / {
            proxy_pass http://frontend:8080;
        }

        location /sockjs-node {
            proxy_pass http://frontend:8080;
            proxy_set_header Host $host;
            # below lines make ws://localhost/sockjs-node/... URLs work, enabling hot-reload
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
        }

        location /api/ {
            # on the backend side, the request URI will _NOT_ contain the /api prefix,
            # which is what we want for a pure-api project
            proxy_pass http://backend:8080/;
            proxy_set_header Host localhost;
        }
    }
  1. 添加 Dockerfile
FROM php:fpm-alpine

RUN apk add --no-cache $PHPIZE_DEPS oniguruma-dev libzip-dev curl-dev \
    && docker-php-ext-install pdo_mysql mbstring zip curl \
    && pecl install xdebug redis \
    && docker-php-ext-enable xdebug redis
RUN mkdir /app
VOLUME /app
WORKDIR /app

EXPOSE 8080
CMD php artisan serve --host=0.0.0.0 --port=8080
  1. 不要忘记将 vue.config.js 添加到您的 frontend 文件夹
// vue.config.js
module.exports = {
    // options...
    devServer: {
        disableHostCheck: true,
        host: 'localhost',
        headers: {
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
        },
        watchOptions: {
            poll: true
        },
        proxy: 'http://localhost/api',
    } 
}
  1. 运行sudo docker-compose up

  2. 如果您想进行迁移,请运行:sudo docker-compose exec backend php artisan migrate

【讨论】:

    猜你喜欢
    • 2014-03-10
    • 2020-06-01
    • 2020-04-13
    • 2019-11-22
    • 2019-08-21
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    相关资源
    最近更新 更多