【问题标题】:Set up docker for nuxt app + nuxt admin + api为 nuxt app + nuxt admin + api 设置 docker
【发布时间】:2020-05-13 14:13:36
【问题描述】:

我有一个带有laravel api 的nuxt.js 应用程序。一切都在docker 中运行。你可以在这里查看我的配置https://github.com/niqitos/laravel-nuxt-docker。 Nuxt 应用是/client 文件夹和/api 中的laravel api。

docker-compose.yml

version: '3'

######### Services ###################################

services:
  # Server container
  nginx:
    build:
      context: docker/nginx
      dockerfile: Dockerfile
    volumes:
      - ./:/var/www
      - ./docker/nginx/logs:/var/log/nginx
    ports:
      # Nuxt port
      - 80:80
      # Laravel port
      - 8081:81
    links:
      - node
      - php-fpm

  # PHP FastCGI Process Manager container
  php-fpm:
    build:
      context: docker/php-fpm
      dockerfile: Dockerfile
    volumes:
      - ./api:/var/www/api
    environment:
      # If you down want to use xDebug, set remote_enable=0
      XDEBUG_CONFIG: "remote_enable=1"
      PHP_IDE_CONFIG: "serverName=Docker"
    links:
      - postgres
      - redis
    expose:
      - 81
    labels:
      traefik.frontend.rule: PathPrefixStrip:/api
      traefik.port: 81

  # Supervisor container (schedule and queue runner)
  supervisor:
    build:
      context: docker/supervisor
      dockerfile: Dockerfile
    volumes:
      - ./:/var/www/
      - ./docker/supervisor/conf.d:/etc/supervisor/conf.d
      - ./docker/supervisor/logs:/var/log
    links:
      - postgres
      - redis

  # Node container
  node:
    build:
      context: docker/node
      dockerfile: Dockerfile
    volumes:
      - ./client:/var/www/client
    expose:
      - 80
    environment:
      NUXT_HOST: 0.0.0.0
      API_URL: http://nginx:80/api/
      CLIENT_URL: http://localhost:80
      GOOGLE_MAPS_API_KEY: 
    labels:
      traefik.frontend.rule: PathPrefixStrip:/
      traefik.port: 80

  # PostgreSQL database container
  postgres:
    build:
      context: docker/postgres
      dockerfile: Dockerfile
    volumes:
      # Database volume
      - database:/var/lib/postgresql/data
      # Temp volume to allow using dumps
      - ./docker/postgres/dumps/:/tmp/
    ports:
      - 5432:5432
    environment:
      - LC_ALL=C.UTF-8
      - POSTGRES_DB=app
      - POSTGRES_USER=app
      - POSTGRES_PASSWORD=app

  # Redis container
  redis:
    build:
      context: docker/redis
      dockerfile: Dockerfile
    volumes:
      - redis:/data
      - ./docker/redis/redis.conf:/data/redis.conf
    ports:
      - 6379:6379

  # Node command line container
  node-cli:
    build:
      context: docker/node-cli
      dockerfile: Dockerfile
    volumes:
      - ./client:/var/www/client
    tty: true

  # PHP Command line container
  php-cli:
    build:
      context: docker/php-cli
      dockerfile: Dockerfile
    volumes:
      - ./api:/var/www/api
    environment:
      # If you down want to use xDebug, set remote_enable=0
      XDEBUG_CONFIG: "remote_enable=1"
      PHP_IDE_CONFIG: "serverName=Docker"
    links:
      - postgres
      - redis
    tty: true

  proxy:
    build:
      context: docker/php-cli
      dockerfile: Dockerfile
    command: --docker
    ports:
      - "80:80"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

  elasticsearch:
    build:
      context: docker/elasticsearch
      dockerfile: Dockerfile
    volumes:
      - ./docker/elasticsearch/data:/usr/share/elasticsearch/data
    environment:
      - node.name=elasticsearch
      - discovery.seed_hosts=elasticsearch
      - cluster.initial_master_nodes=elasticsearch
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    ports:
      - 9200:9200

######### Volumes ###################################

volumes:
  database:
    driver: local
  redis:
    driver: local

docker/nginx/default.conf

#--------------------------------------------------------
# Nuxt.JS server configuration
#--------------------------------------------------------

map $sent_http_content_type $expires {
    "text/html"                 epoch;
    "text/html; charset=utf-8"  epoch;
    default                     off;
}

server {
    listen 80;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    charset utf-8;

    gzip            on;
    gzip_types      text/plain application/xml text/css application/javascript;
    gzip_min_length 1000;

    location / {
        # Proxy to Node.JS instance
        proxy_pass http://node:3000;

        expires $expires;

        proxy_redirect                      off;
        proxy_set_header X-Real-IP          $remote_addr;
        proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto  $scheme;
        proxy_read_timeout                  1m;
        proxy_connect_timeout               1m;

        # Websocket support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    # Proxy all API requests
    location /api {
        proxy_pass http://nginx:81;
    }
}

#--------------------------------------------------------
# Laravel server configuration
#--------------------------------------------------------
server {
    #listen 443 ssl;
    listen 81;

    index index.php index.html;
    root /var/www/api/public;
    charset utf-8;

    client_max_body_size 20m;

    # SSL for 443
    #ssl_certificate /etc/nginx/ssl/ssl-cert-snakeoil.pem;
    #ssl_certificate_key /etc/nginx/ssl/ssl-cert-snakeoil.key;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    # Handle all php files (which will always be just /index.php)
    # via factcgi PHP-FPM unix socket
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php-fpm:9000;
        fastcgi_index index.php;
        # For comfortable debugging
        fastcgi_read_timeout 1000;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

我想在 /admin 文件夹中为管理员添加另一个 nuxt.js 应用程序。但是我现在一个星期都无法解决它。所有尝试最终都会出现构建错误。我应该在docker-compose.ymlnginx 配置中添加什么?

【问题讨论】:

  • 现在这些文件中有什么?请在问题本身中添加这些详细信息,而不是在外部链接后面(并且作为文本,而不是任何图像或屏幕截图)。
  • 更新了我的帖子。谢谢

标签: laravel docker nuxt.js


【解决方案1】:

我不确定,但我认为您需要为您的服务设置一个“container_name”,如下所示:

  node:
    container_name: node_container
    build:
      context: docker/node
      dockerfile: Dockerfile
    # ...

而nginx需要检查proxy_pass中的http://node_container:3000

location / {
        # Proxy to Node.JS instance
        proxy_pass http://node_container:3000;

希望我能理解并帮助您的要求

【讨论】:

  • 这个解决方案给了我一个新的错误2020/07/10 19:29:13 [emerg] 1#1: host not found in upstream "container_name" in /etc/nginx/conf.d/app.conf:25 但我的 nuxtjs 应用程序通过 localhost:3000(nginx 外部)运行良好
  • 也许你忘记在 docker-compose.yaml 中暴露节点服务中的 3000 端口?
猜你喜欢
  • 2019-09-05
  • 2021-02-07
  • 2021-10-30
  • 1970-01-01
  • 2019-04-14
  • 2021-12-07
  • 1970-01-01
  • 2019-12-13
  • 1970-01-01
相关资源
最近更新 更多