无法通过Rails启动和Nginx启动Docker容器

如何解决无法通过Rails启动和Nginx启动Docker容器

我已经尝试了好几天,用docker运行我的前端和后端应用程序,但是没有成功,我还需要通过nginx来提供它。这是我当前的配置,如果有人可以告诉我这是怎么回事,那太好了。

项目结构

/frontend
/backend
/nginx
docker-compose.yml

我可以使用cd frontend && docker build -t frontend . && docker run -p 2000:80 frontend启动Dockerfile前端,但是当我运行docker-compose up时从项目的根目录开始,后端和nginx退出,代码为0

docker-compose.yml

version: '3.6'
services:
  db:
    container_name: postgres
    image: postgres:11
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    environment:
      POSTGRES_HOST_AUTH_METHOD: trust
  backend:
    build: ./backend
    volumes:
      - './backend:/myproject/backend'
    depends_on:
      - db
    ports:
      - 3000:3000
    environment: 
      - RAILS_ENV=production
      - MASTER_KEY=XXXXXXXXXX
  frontend:
    build: ./frontend
    environment:
      - NODE_ENV=production
    depends_on:
      - backend
    ports:
      - 4000:80
  nginx:
    build: ./nginx
    restart: always
    ports:
      - 80:80
    depends_on:
      - backend
      - frontend

后端/ Dockerfile

FROM ruby:2.7.1

RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

RUN mkdir -p /myproject/backend

WORKDIR /myproject/backend

COPY Gemfile /myproject/backend/Gemfile

COPY Gemfile.lock /myproject/backend/Gemfile.lock

RUN bundle install

COPY . /myproject/backend

RUN bundle exec rails db:create

RUN bundle exec rails db:migrate

EXPOSE 3000

CMD ["bundle","exec","rails","s","-p","3000","-b","'0.0.0.0'"]

前端/ Dockerfile

FROM node:12.18.3-alpine as builder

WORKDIR /myproject/frontend

COPY package.json /myproject/frontend/package.json

RUN yarn global add webpack

RUN yarn install

COPY . .

RUN yarn build

FROM nginx:1.15.2-alpine

COPY --from=builder /myproject/frontend/dist /var/www

COPY nginx.conf /etc/nginx/nginx.conf

EXPOSE 80

CMD ["nginx","-g","daemon off;"]

frontend / nginx.conf

# auto detects a good number of processes to run
worker_processes auto;

#Provides the configuration file context in which the directives that affect connection processing are specified.
events {
    # Sets the maximum number of simultaneous connections that can be opened by a worker process.
    worker_connections 8000;
    # Tells the worker to accept multiple connections at a time
    multi_accept on;
}


http {
    # what times to include
    include       /etc/nginx/mime.types;
    # what is the default one
    default_type  application/octet-stream;

    # Sets the path,format,and configuration for a buffered log write
    log_format compression '$remote_addr - $remote_user [$time_local] '
        '"$request" $status $upstream_addr '
        '"$http_referer" "$http_user_agent"';

    server {
        # listen on port 80
        listen 80;
        # save logs here
        access_log /var/log/nginx/access.log compression;

        # where the root here
        root /var/www;
        # what file to server as index
        index index.html index.htm;

        location / {
            # First attempt to serve request as file,then
            # as directory,then fall back to redirecting to index.html
            try_files $uri $uri/ /index.html;
        }

        # Media: images,icons,video,audio,HTC
        location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
            expires 1M;
            access_log off;
            add_header Cache-Control "public";
        }

        # Javascript and CSS files
        location ~* \.(?:css|js)$ {
            try_files $uri =404;
            expires 1y;
            access_log off;
            add_header Cache-Control "public";
        }

        # Any route containing a file extension (e.g. /devicesfile.js)
        location ~ ^.+\..+$ {
            try_files $uri =404;
        }
    }
}

nginx / Dockerfile

FROM nginx:alpine

RUN rm /etc/nginx/conf.d/default.conf
COPY /default.conf /etc/nginx/conf.d

nginx / default.conf

server {

  listen 80;

  location / {
    proxy_pass http://frontend:80;
    proxy_redirect    default;
    proxy_set_header  Host $host;
    proxy_set_header  X-Real-IP $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Host $server_name;
  }

  location /api {
    proxy_pass        http://backend:3000;
    proxy_redirect    default;
    proxy_set_header  Host $host;
    proxy_set_header  X-Real-IP $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Host $server_name;
  }

}

docker-compose up 命令输出

docker-compose up
Creating network "myproject_default" with the default driver
WARNING: Found orphan containers (yaichi) for this project. If you removed or renamed this service in your compose file,you can run this command with the --remove-orphans flag to clean it up.
Creating postgres ... done
Creating myproject_backend_1 ... done
Creating myproject_frontend_1 ... done
Creating myproject_nginx_1    ... done
Attaching to postgres,myproject_backend_1,myproject_frontend_1,myproject_nginx_1
nginx_1     | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty,will attempt to perform configuration
nginx_1     | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx_1     | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
backend_1   | ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]
backend_1   | The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32,x86-mswin32,x64-mingw32,java. To add those platforms to the bundle,run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
nginx_1     | 10-listen-on-ipv6-by-default.sh: error: /etc/nginx/conf.d/default.conf is not a file or does not exist
nginx_1     | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
backend_1   | The Gemfile's dependencies are satisfied
nginx_1     | /docker-entrypoint.sh: Configuration complete; ready for start up
myproject_frontend_1 exited with code 0
myproject_backend_1 exited with code 0
nginx_1     | 2020/08/21 07:41:44 [emerg] 1#1: host not found in upstream "frontend" in /etc/nginx/conf.d/nginx.conf:6
nginx_1     | nginx: [emerg] host not found in upstream "frontend" in /etc/nginx/conf.d/nginx.conf:6
postgres    |
postgres    | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres    |
postgres    | 2020-08-21 07:41:44.480 UTC [1] LOG:  listening on IPv4 address "0.0.0.0",port 5432
postgres    | 2020-08-21 07:41:44.480 UTC [1] LOG:  listening on IPv6 address "::",port 5432
postgres    | 2020-08-21 07:41:44.504 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres    | 2020-08-21 07:41:44.765 UTC [28] LOG:  database system was shut down at 2020-08-21 07:40:56 UTC
postgres    | 2020-08-21 07:41:44.867 UTC [1] LOG:  database system is ready to accept connections
myproject_nginx_1 exited with code 1
myproject_nginx_1 exited with code 1
nginx_1     | 2020/08/21 07:41:47 [emerg] 1#1: host not found in upstream "frontend" in /etc/nginx/conf.d/nginx.conf:6
nginx_1     | nginx: [emerg] host not found in upstream "frontend" in /etc/nginx/conf.d/nginx.conf:6
myproject_nginx_1 exited with code 1
myproject_nginx_1 exited with code 1
nginx_1     | 2020/08/21 07:41:51 [emerg] 1#1: host not found in upstream "frontend" in /etc/nginx/conf.d/nginx.conf:6
nginx_1     | nginx: [emerg] host not found in upstream "frontend" in /etc/nginx/conf.d/nginx.conf:6
myproject_nginx_1 exited with code 1
nginx_1     | 2020/08/21 07:41:55 [emerg] 1#1: host not found in upstream "frontend" in /etc/nginx/conf.d/nginx.conf:6
nginx_1     | nginx: [emerg] host not found in upstream "frontend" in /etc/nginx/conf.d/nginx.conf:6
myproject_nginx_1 exited with code 1
^CGracefully stopping... (press Ctrl+C again to force)
Stopping myproject_nginx_1    ... done
Stopping postgres      ... done

docker-compose up --build 输出

docker-compose up --build
WARNING: Found orphan containers (yaichi) for this project. If you removed or renamed this service in your compose file,you can run this command with the --remove-orphans flag to clean it up.
Building backend
Step 1/12 : FROM ruby:2.7.1
 ---> 958d3491c09a
Step 2/12 : RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
 ---> Using cache
 ---> b8ae75fa12b3
Step 3/12 : RUN mkdir -p /m3/backend
 ---> Using cache
 ---> cb3b556f8ce3
Step 4/12 : WORKDIR /m3/backend
 ---> Using cache
 ---> 764b5236b91f
Step 5/12 : COPY Gemfile /m3/backend/Gemfile
 ---> Using cache
 ---> d40100111945
Step 6/12 : COPY Gemfile.lock /m3/backend/Gemfile.lock
 ---> Using cache
 ---> 0682d5bf02f0
Step 7/12 : RUN bundle install
 ---> Using cache
 ---> f9615e684ebd
Step 8/12 : COPY . /m3/backend
 ---> 12ea56e4eeff
Step 9/12 : RUN bundle exec rails db:create
 ---> Running in 4e394b5ba38f
could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Couldn't create 'm3_development' database. Please check your configuration.
rails aborted!
ActiveRecord::NoDatabaseError: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/postgresql_adapter.rb:50:in `rescue in postgresql_connection'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/postgresql_adapter.rb:33:in `postgresql_connection'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:887:in `new_connection'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `checkout_new_connection'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:910:in `try_to_checkout_new_connection'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:871:in `acquire_connection'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:593:in `checkout'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:437:in `connection'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:1119:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/connection_handling.rb:221:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/connection_handling.rb:189:in `connection'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/tasks/postgresql_database_tasks.rb:12:in `connection'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/tasks/postgresql_database_tasks.rb:21:in `create'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/tasks/database_tasks.rb:126:in `create'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/tasks/database_tasks.rb:185:in `block in create_current'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/tasks/database_tasks.rb:479:in `block (2 levels) in each_current_configuration'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/tasks/database_tasks.rb:476:in `each'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/tasks/database_tasks.rb:476:in `block in each_current_configuration'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/tasks/database_tasks.rb:475:in `each'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/tasks/database_tasks.rb:475:in `each_current_configuration'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/tasks/database_tasks.rb:184:in `create_current'
/usr/local/bundle/gems/activerecord-6.0.3.1/lib/active_record/railties/databases.rake:39:in `block (2 levels) in <main>'
/usr/local/bundle/gems/railties-6.0.3.1/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/usr/local/bundle/gems/railties-6.0.3.1/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/usr/local/bundle/gems/railties-6.0.3.1/lib/rails/command.rb:48:in `invoke'
/usr/local/bundle/gems/railties-6.0.3.1/lib/rails/commands.rb:18:in `<main>'
/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
/usr/local/bundle/gems/activesupport-6.0.3.1/lib/active_support/dependencies.rb:324:in `block in require'
/usr/local/bundle/gems/activesupport-6.0.3.1/lib/active_support/dependencies.rb:291:in `load_dependency'
/usr/local/bundle/gems/activesupport-6.0.3.1/lib/active_support/dependencies.rb:324:in `require'
bin/rails:4:in `<main>'

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-