Источник:
1 |
https://community.vscale.io/hc/ru/community/posts/211638529-Docker-%D1%81%D0%BE%D0%B7%D0%B4%D0%B0%D1%91%D0%BC-%D0%B8-%D0%B7%D0%B0%D0%BF%D1%83%D1%81%D0%BA%D0%B0%D0%B5%D0%BC-%D0%B2%D0%B5%D0%B1-%D0%BF%D1%80%D0%B8%D0%BB%D0%BE%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5 |
структура каталогов:
1 2 3 4 |
project_folder/ nginx/ web/ api/ |
каталог nginx
1 2 3 4 |
nginx/ Dockerfile nginx.conf .. |
каталог nginx \ файл Dockerfile
1 2 3 4 5 6 7 8 9 10 11 |
FROM nginx MAINTAINER your_email_address - Укажем почту разработчика, занимающегося поддержкой создаваемого образа в строчке MAINTAINER. COPY nginx.conf /etc/nginx/nginx.conf - файл конфигурации для nginx WORKDIR /etc/nginx EXPOSE 80 CMD ["nginx", "-g", "daemon off;"] |
nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
worker_processes auto; events { worker_connections 8096; multi_accept on; use epoll; } http { fastcgi_read_timeout 60; sendfile off; keepalive_timeout 10; keepalive_requests 1024; client_header_timeout 10; client_body_timeout 10; send_timeout 10; gzip on; gzip_vary on; gzip_comp_level 2; gzip_buffers 4 8k; gzip_proxied expired no-cache no-store private auth; gzip_min_length 1000; gzip_disable "MSIE [1-6]\."; gzip_types text/plain text/xml text/css text/comma-separated-values text/javascript application/x-javascript application/atom+xml; # WEB CONTAINER LINK upstream web_servers { server web:8080; } # API CONTAINER LINK upstream api_servers { server api:8080; } server { listen 80; location / { proxy_pass http://web_servers; proxy_redirect off; 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_connect_timeout 30s; proxy_send_timeout 30s; proxy_read_timeout 30s; proxy_pass http://api_servers; proxy_redirect off; 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; } } } |
Каталог web
1 2 3 4 5 6 |
web/ Dockerfile app.js package.json public/index.html .. |
каталог web и файл Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
FROM node:latest MAINTAINER your_email_address # 1. WORKDIR /tmp ADD package.json /tmp/package.json RUN npm config set registry http://registry.npmjs.org/ RUN npm install RUN mkdir -p /usr/src/app RUN cp -a /tmp/node_modules /usr/src/app WORKDIR /usr/src/app # 2. ADD . /usr/src/app EXPOSE 8080 CMD [ "npm", "run", "prod" ] |
каталог web и файл index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
"use strict"; const express = require('express'); const http = require('http'); const app = express(); const server = http.Server(app); const path = require('path'); app.use(express.static(path.join(__dirname, '/public'))); app.get('*', (request, response) => { response.sendFile(path.resolve(__dirname, 'public', 'index.html')) }); server.listen(8080); |
каталог api
1 2 3 4 5 6 |
api/ Dockerfile app.js package.json data/.. .. |
файлы api ???
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
В нашем случае образы серверов статического и динамического контента не отличаются. Что касается самого сервера, то код его следующий: "use strict"; const express = require('express'); const bodyParser = require('body-parser'); const http = require('http'); const app = express(); const server = http.Server(app); const path = require('path'); const AppRouter = require('./Routers/App.router'); app.use('/api'.concat('/demo'), AppRouter); server.listen(8080); Простой маршрутизатор (Router), может выглядеть следующим образом: const express = require('express'); const router = express.Router(); router.get('/demo', function (req, res, next) { res.status(200).json({ success: true, process: 'started', }); }) module.exports = router; |
взаимодействие контейнеров
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
project_folder/ docker-compose.yml nginx/ web/ api/ docker-compose.yml ------------------ nginx: build: ./nginx links: - web:web - api:api ports: - "80:80" web: build: ./web ports: - "8080" api: build: ./api ports: - "8080" ------------------ |
запуск
1 |
docker-compose up |