# Learning Docker, Part5 ###### tags: `Docker` ## Dockerizing Mutiple Services ### APP組織架構    ``` // Dockerfile.dev in client FROM node:20-alpine WORKDIR '/app' COPY ./package.json ./ RUN npm install COPY . . CMD [ "npm", "run", "start" ] ``` ``` // Dockerfile.dev in server & worker FROM node:20-alpine WORKDIR '/app' COPY ./package.json ./ RUN npm install COPY . . CMD [ "npm", "run", "dev" ] ``` --- ### Compose設置  ``` // docker-compose.yml version: '3' services: postgres: image: 'postgres:latest' environment: - POSTGRES_PASSWORD=postgres_password redis: image: 'redis:latest' server: build: dockerfile: Dockerfile.dev context: ./server volumes: - /app/node_modules - ./server:/app environment: - REDIS_HOST=redis - REDIS_PORT=6379 - PGUSER=postgres - PGHOST=postgres - PGDATABASE=postgres - PSPASSWORD=postgres_password - PGPORT=5432 client: build: dockerfile: Dockerfile.dev context: ./client volumes: - /app/node_modules - ./client:/app worker: build: dockerfile: Dockerfile.dev context: ./worker volumes: - /app/node_modules - ./worker:/app environment: - REDIS_HOST=redis - REDIS_PORT=6379 ``` --- ### Nginx  新增一個資料夾nginx,並在內新增一個檔案default.conf  ``` // default.conf upstream client { server client: 3000; } upstream api { server api: 5000; } server { listen 80; location / { proxy_pass http://client; } location /ws { proxy_pass http://client; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; } location /api { rewrite /api/(.*) /$1 break; proxy_pass http://api; } } ``` 新增一個檔案Dockerfile.dev ``` // Dockerfile.dev FROM nginx COPY ./default.conf /etc/nginx/conf.d/default.conf ``` 更改docker-compose.yml ``` // docker-compose.yml version: "3" services: postgres: image: "postgres:latest" environment: - POSTGRES_PASSWORD=postgres_password redis: image: "redis:latest" nginx: depends_on: - api - client restart: always build: dockerfile: Dockerfile.dev context: ./nginx ports: - "3050:80" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ api: build: dockerfile: Dockerfile.dev context: ./server volumes: - /app/node_modules - ./server:/app environment: - REDIS_HOST=redis - REDIS_PORT=6379 - PGUSER=postgres - PGHOST=postgres - PGDATABASE=postgres - PGPASSWORD=postgres_password - PGPORT=5432 client: build: dockerfile: Dockerfile.dev context: ./client volumes: - /app/node_modules - ./client:/app environment: - WDS_SOCKET_PORT=0 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ worker: build: dockerfile: Dockerfile.dev context: ./worker volumes: - /app/node_modules - ./worker:/app environment: - REDIS_HOST=redis - REDIS_PORT=6379 ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up