我已经尝试了好几天,用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>'