我刚刚开始使用 docker,我正在尝试弄清楚如何部署我们的 web 应用程序。 我们有:
ReactJS Web 应用程序和工作台前端(非同构,Apache) Java/Wildfly 服务器后端 PostgreSQL 数据库 用于推送通知的 NodeJS Websocket 服务器 我的第一种方法是将它全部放在一个 ubuntu 容器中,然后部署它。这导致了一个非常长的 dockerfile,所以我认为这不是正确的方法和 I googled best practices并且到了每个容器应该只有一个关注点的地步。
这是否意味着我应该为上述每个进程使用一个容器并将它们链接起来,这将导致 4 个正在运行的容器(数据库有一个卷)?
我现在看到的优点是: 可扩展性 可维护性 将应用程序的部分移动到不同的服务器/主机更容易 我能想到的唯一两个缺点是 将所有容器链接在一起可能会变得复杂 (小)由于运行 4 个容器而不是 1 个容器而导致的性能损失(以及更多用于排队、邮件服务器等)
Best Answer-推荐答案
您可以使用 4 个不同的容器!使用 Docker,您通常希望尝试使每个容器相对简单,但这并不意味着您必须将每个组件分离到不同的容器中,例如您的服务器 API 中的 Web 应用程序(我发现将它们组合在同一个容器中很有用,因为它使通信更容易)但是将数据库放在单独的容器中是一种很好的做法。 Docker 有一个名为 Docker Compose 的工具用于启动容器应用程序:
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration. - Overview of Docker Compose
为了使用此工具,您将: 为每个单独的容器创建一个包含文件夹的项目目录。 通过创建 Dockerfile 定义您的应用程序环境对于子文件夹中的每个容器(Web 应用程序/服务器 API)(数据库)。 用于服务器 API(和 Web 应用程序(角度))的 Dockerfile:
# Create image based on the official Node 6 image from dockerhub
FROM node:6
# Create a directory where our app will be placed
RUN mkdir -p /usr/src/app/
# Change directory so that our commands run inside this new directory
WORKDIR /usr/src/app
# Get all the code needed to run the app
COPY . /usr/src/app
# Move into AngularApp directory
WORKDIR /usr/src/app/public
# Install dependencies for AngularApp
RUN npm install
# Changed to updated angular dependency
RUN ./node_modules/@angular/cli/bin/ng build
# Move into server directory
WORKDIR /usr/src/app/server
# Install dependencies for server
RUN npm install
# Return to top level directory of project
WORKDIR /usr/src/app
# Add API environment variables
ENV USERNAME="username"
ENV PASSWORD="password"
# Expose the port the app runs in
EXPOSE 4200
EXPOSE 80
WORKDIR /usr/src/app
在项目文件夹中定义一个 docker-compose.yml 文件。此文件将定义您的应用程序中的服务,以便它们可以在隔离的环境中一起运行。 Compose file version 3 reference docker compose 文件遵循以下结构:
version: '3'
services:
web:
build: <directory with Dockerfile for service>
ports:
- "5000:5000"
volumes:
- .:/code
- logvolume01:/var/log
links:
- redis
database:
image: mongo
volumes:
logvolume01: {}
最后运行 docker-compose up Compose 将开始运行您的整个应用程序!
关于Docker:前端、后端、数据库、推送服务器的 4 个容器?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/45078663/
|