【问题标题】:docker compose nginx not starting during builddocker compose nginx 在构建期间没有启动
【发布时间】:2018-11-07 03:58:39
【问题描述】:

我觉得很奇怪,我已经设置了一个 Rails 应用程序、posgres db 和一个 nginx 服务器,但只有当我输入时 ngix 才能启动

docker-compose -f docker-compose.yml -f production.yml up --build

但不是预构建

docker-compose -f docker-compose.yml -f production.yml build

那么 docker-compose up rails 应用程序和 db 启动得很好,只是 nginx 没有启动,端口恢复到端口 3000 而不是 80,我觉得很奇怪,他们不是在做同样的事情吗?

nginx.conf

# This is a template. Referenced variables (e.g. $INSTALL_PATH) need 
# to be rewritten with real values in order for this file to work. 

upstream rails_app {
  server unix:///webapp/tmp/sockets/puma.sock;
}

server {
  listen 80;
  # define your domain
  server_name 127.0.0.1 localhost www.example.com;

  # define the public application root
  root   /providre_api/public;


  # define where Nginx should write its logs
  access_log /providre_api/log/nginx.access.log;
  error_log /providre_api/log/nginx.error.log;

  # deny requests for files that should never be accessed
  location ~ /\. {
    deny all;
  }

  location ~* ^.+\.(rb|log)$ {\
    deny all;
  }

  # serve static (compiled) assets directly if they exist (for rails production)
  location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/ {
    try_files $uri @rails;

    access_log off;
    gzip_static on; # to serve pre-gzipped version

    expires max;
    add_header Cache-Control public;

    # Some browsers still send conditional-GET requests if there's a
    # Last-Modified header or an ETag header even if they haven't
    # reached the expiry date sent in the Expires header.
    add_header Last-Modified "";
    add_header ETag "";
    break;
  }

  # send non-static file requests to the app server
  location / {
    try_files $uri @rails;
  }

  location @rails {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://rails_app;
  }
}

web.Dockerfile

# Base image:
FROM nginx
# Install dependencies
RUN apt-get update -qq && apt-get -y install apache2-utils

# establish where Nginx should look for files
ENV INSTALL_PATH /providre_api

# Set our working directory inside the image
WORKDIR $INSTALL_PATH

# create log directory
RUN mkdir log

# copy over static assets
COPY public public/

# Copy Nginx config template
COPY docker/web/nginx.conf /tmp/docker.nginx

# substitute variable references in the Nginx config template for real values from the environment
# put the final config in its place
RUN envsubst '$INSTALL_PATH' < /tmp/docker.nginx > /etc/nginx/conf.d/default.conf

EXPOSE 80

# Use the "exec" form of CMD so Nginx shuts down gracefully on SIGTERM (i.e. `docker stop`)
CMD [ "nginx", "-g", "daemon off;" ]

docker-compose.yml

version: '3'
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    restart: always
    ports:
     - "5433:5432"
    environment:
      POSTGRES_USER: 'postgres'
      POSTGRES_PASSWORD: ''
  app:
    command: bundle exec puma -C config/puma.rb
    ports:
      - "3000"
    depends_on:
      - db

docker-compose.override.yml

version: '3'
services:
  app:
    build: 
       context: .
       dockerfile: ./docker/app/Dockerfile
    volumes:
      - .:/providre_api
    ports:
      - "3000:3000"

生产.yml

version: '3'
services:
  app:
    build: 
       context: .
       dockerfile: ./docker/app/prod.Dockerfile
    volumes:
      - .:/providre_api
    ports:
      - "3000"
  nginx:
    container_name: web
    build: 
      context: .
      dockerfile: ./docker/web/web.Dockerfile 
    depends_on:
      - app
    volumes:
      - ./docker/web/nginx.conf:/etc/nginx/conf.d/default.conf
    ports:
      - 80:80

【问题讨论】:

    标签: ruby-on-rails docker nginx docker-compose puma


    【解决方案1】:

    对不起,我没有完全测试它,所以docker-compose up 将使用正常的docker-compose.yml,所以我必须重复并改用docker-compose -f docker-compose.yml -f production.yml up

    【讨论】:

      猜你喜欢
      • 2016-12-08
      • 2017-09-09
      • 1970-01-01
      • 2020-01-22
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多