我刚刚开始使用 docker,我正在尝试弄清楚如何部署我们的 web 应用程序。
我们有:
您可以使用 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)(数据库)。 # 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 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/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |