Skip to main content

在 Docker 容器中运行 Strapi

¥Running Strapi in a Docker container

提醒

Strapi 不构建任何官方容器镜像。以下说明出于对社区的礼貌而提供。如果你有任何疑问,请联系 Discord

¥Strapi does not build any official container images. The following instructions are provided as a courtesy to the community. If you have any questions please reach out on Discord.

危险

Strapi 应用并不意味着连接到预先存在的数据库、不是由 Strapi 应用创建的数据库,也不是连接到 Strapi v3 数据库。Strapi 团队不会支持此类尝试。尝试连接到不受支持的数据库可能并且很可能会导致数据丢失,例如删除表。

¥Strapi applications are not meant to be connected to a pre-existing database, not created by a Strapi application, nor connected to a Strapi v3 database. The Strapi team will not support such attempts. Attempting to connect to an unsupported database may, and most likely will, result in lost data such as dropped tables.

以下文档将指导你使用现有 Strapi 项目构建自定义 Docker 容器。

¥The following documentation will guide you through building a custom Docker container with an existing Strapi project.

Docker 是一个开放平台,允许使用容器(即包含应用运行所需的所有部分的包,例如库和依赖)来开发、运输和运行应用。容器之间相互隔离,并打包自己的软件、库和配置文件;他们可以通过明确的渠道相互沟通。

¥Docker is an open platform that allows developing, shipping, and running applications by using containers (i.e. packages containing all the parts an application needs to function, such as libraries and dependencies). Containers are isolated from each other and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels.

Prerequisites

开发和/或暂存环境

¥Development and/or Staging environments

要在主机上本地使用 Strapi,你可以使用 Dockerfile,如果需要,还可以使用 docker-compose.yml 来启动数据库容器。

¥For working with Strapi locally on your host machine you can use the Dockerfile, and if needed the docker-compose.yml can also be used to start up a database container.

两种方法都需要现有的 Strapi 项目或创建新的项目(请参阅 快速入门指南)。

¥Both methods require an existing Strapi project or a new one created (see Quick Start guide).

开发 Dockerfile

¥Development Dockerfile

以下 Dockerfile 可用于为 Strapi 项目构建非生产 Docker 映像。

¥The following Dockerfile can be used to build a non-production Docker image for a Strapi project.

注意

如果你使用的是 docker-compose,则可以跳过手动设置环境变量,因为它们将在 docker-compose.yml 文件或 .env 文件中设置。

¥If you are using docker-compose, you can skip setting the environment variables manually, as they will be set in the docker-compose.yml file or a .env file.

为了在 Docker 容器中运行 Strapi,需要以下环境变量:

¥The following environment variables are required in order to run Strapi in a Docker container:

变量名描述
NODE_ENV应用运行的环境。
DATABASE_CLIENT要使用的数据库客户端。
DATABASE_HOST数据库主机。
DATABASE_PORT数据库端口。
DATABASE_NAME数据库名称。
DATABASE_USERNAME数据库用户名。
DATABASE_PASSWORD数据库密码。
JWT_SECRET用于为用户权限插件签署 JWT 的密钥。
ADMIN_JWT_SECRET用于为管理面板签署 JWT 的密钥。
APP_KEYS用于签署会话 cookie 的密钥。

你还可以设置一些 可选环境变量

¥You can also set some optional environment variables.

有关 Dockerfile 及其命令的更多信息,请参阅 Docker 官方文档

¥For more information on the Dockerfile and its commands, please refer to the official Docker documentation.

样品 Dockerfile

¥Sample Dockerfile:

./Dockerfile
FROM node:18-alpine3.18
# Installing libvips-dev for sharp Compatibility
RUN apk update && apk add --no-cache build-base gcc autoconf automake zlib-dev libpng-dev nasm bash vips-dev git
ARG NODE_ENV=development
ENV NODE_ENV=${NODE_ENV}

WORKDIR /opt/
COPY package.json yarn.lock ./
RUN yarn global add node-gyp
RUN yarn config set network-timeout 600000 -g && yarn install
ENV PATH=/opt/node_modules/.bin:$PATH

WORKDIR /opt/app
COPY . .
RUN chown -R node:node /opt/app
USER node
RUN ["yarn", "build"]
EXPOSE 1337
CMD ["yarn", "develop"]

(可选)Docker 组合

¥(Optional) Docker Compose

以下 docker-compose.yml 可用于启动数据库容器和 Strapi 容器以及用于两者之间通信的共享网络。

¥The following docker-compose.yml can be used to start up a database container and a Strapi container along with a shared network for communication between the two.

注意

有关运行 Docker compose 及其命令的更多信息,请参阅 Docker Compose 文档

¥For more information about running Docker compose and its commands, please refer to the Docker Compose documentation.

样品 docker-compose.yml

¥Sample docker-compose.yml:

./docker-compose.yml
version: "3"
services:
strapi:
container_name: strapi
build: .
image: strapi:latest
restart: unless-stopped
env_file: .env
environment:
DATABASE_CLIENT: ${DATABASE_CLIENT}
DATABASE_HOST: strapiDB
DATABASE_PORT: ${DATABASE_PORT}
DATABASE_NAME: ${DATABASE_NAME}
DATABASE_USERNAME: ${DATABASE_USERNAME}
DATABASE_PASSWORD: ${DATABASE_PASSWORD}
JWT_SECRET: ${JWT_SECRET}
ADMIN_JWT_SECRET: ${ADMIN_JWT_SECRET}
APP_KEYS: ${APP_KEYS}
NODE_ENV: ${NODE_ENV}
volumes:
- ./config:/opt/app/config
- ./src:/opt/app/src
- ./package.json:/opt/package.json
- ./yarn.lock:/opt/yarn.lock
- ./.env:/opt/app/.env
- ./public/uploads:/opt/app/public/uploads
ports:
- "1337:1337"
networks:
- strapi
depends_on:
- strapiDB

strapiDB:
container_name: strapiDB
platform: linux/amd64 #for platform error on Apple M1 chips
restart: unless-stopped
env_file: .env
image: mysql:5.7
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_USER: ${DATABASE_USERNAME}
MYSQL_ROOT_PASSWORD: ${DATABASE_PASSWORD}
MYSQL_PASSWORD: ${DATABASE_PASSWORD}
MYSQL_DATABASE: ${DATABASE_NAME}
volumes:
- strapi-data:/var/lib/mysql
#- ./data:/var/lib/mysql # if you want to use a bind folder
ports:
- "3306:3306"
networks:
- strapi

volumes:
strapi-data:

networks:
strapi:
name: Strapi
driver: bridge

生产环境

¥Production Environments

生产中的 Docker 映像与开发/暂存环境中使用的映像不同,因为除了用于运行应用的命令之外,管理构建过程也存在差异。典型的生产环境将使用反向代理来为应用和管理面板提供服务。Docker 映像是使用管理面板的生产版本构建的,用于运行应用的命令是 strapi start

¥The Docker image in production is different from the one used in development/staging environments because of the differences in the admin build process in addition to the command used to run the application. Typical production environments will use a reverse proxy to serve the application and the admin panel. The Docker image is built with the production build of the admin panel and the command used to run the application is strapi start.

创建 Dockerfile 后,即可构建 生产容器。或者,可以将容器发布到 registry,以供社区使用。社区工具 可以帮助你构建生产 Docker 映像并将其部署到生产环境。

¥Once the Dockerfile is created, the production container can be built. Optionally, the container can be published to a registry to make it available to the community. Community tools can help you in the process of building a production Docker image and deploying it to a production environment.

生产 Dockerfile

¥Production Dockerfile

以下 Dockerfile 可用于为 Strapi 项目构建生产 Docker 映像。

¥The following Dockerfile can be used to build a production Docker image for a Strapi project.

./Dockerfile.prod
# Creating multi-stage build for production
FROM node:18-alpine as build
RUN apk update && apk add --no-cache build-base gcc autoconf automake zlib-dev libpng-dev vips-dev git > /dev/null 2>&1
ENV NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}

WORKDIR /opt/
COPY package.json yarn.lock ./
RUN yarn global add node-gyp
RUN yarn config set network-timeout 600000 -g && yarn install --production
ENV PATH=/opt/node_modules/.bin:$PATH
WORKDIR /opt/app
COPY . .
RUN yarn build

# Creating final production image
FROM node:18-alpine
RUN apk add --no-cache vips-dev
ENV NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
WORKDIR /opt/
COPY --from=build /opt/node_modules ./node_modules
WORKDIR /opt/app
COPY --from=build /opt/app ./
ENV PATH=/opt/node_modules/.bin:$PATH

RUN chown -R node:node /opt/app
USER node
EXPOSE 1337
CMD ["yarn", "start"]

构建生产容器

¥Building the production container

构建生产 Docker 镜像可以有多种选择。以下示例使用 docker build 命令为 Strapi 项目构建生产 Docker 映像。但是,建议你查看 Docker 文档 以获取有关使用更高级选项构建 Docker 映像的更多信息。

¥Building production Docker images can have several options. The following example uses the docker build command to build a production Docker image for a Strapi project. However, it is recommended you review the Docker documentation for more information on building Docker images with more advanced options.

要为 Strapi 项目构建生产 Docker 映像,请运行以下命令:

¥To build a production Docker image for a Strapi project, run the following command:

docker build \
--build-arg NODE_ENV=production \
# --build-arg STRAPI_URL=https://api.example.com \ # Uncomment to set the Strapi Server URL
-t mystrapiapp:latest \ # Replace with your image name
-f Dockerfile.prod .

(可选)将容器发布到注册表

¥(Optional) Publishing the container to a registry

为 Strapi 项目构建生产 Docker 映像后,你可以将该映像发布到 Docker 注册表。理想情况下,对于生产用途,这应该是私有注册表,因为你的 Docker 映像将包含敏感信息。

¥After you have built a production Docker image for a Strapi project, you can publish the image to a Docker registry. Ideally for production usage this should be a private registry as your Docker image will contain sensitive information.

根据你的托管提供者,你可能需要使用不同的命令来发布图片。建议你查看 Docker 文档,了解有关使用更高级选项发布 Docker 映像的更多信息。

¥Depending on your hosting provider you may need to use a different command to publish your image. It is recommended you review the Docker documentation for more information on publishing Docker images with more advanced options.

一些流行的托管提供者是:

¥Some popular hosting providers are:

社区工具

¥Community tools

有多种社区工具可帮助你将 Strapi 部署到各种云提供者并在开发或生产环境中设置 Docker。

¥Several community tools are available to assist you in deploying Strapi to various cloud providers and setting up Docker in a development or production environment.

我们强烈支持我们的社区努力,并鼓励你查看以下工具,请通过为它们的发展做出贡献来帮助支持它们。

¥We strongly support our community efforts and encourage you to check out the following tools, please help support them by contributing to their development.

如果你想将你的工具添加到此列表中,请在 Strapi 文档存储库 上打开拉取请求。

¥If you would like to add your tool to this list, please open a pull request on the Strapi documentation repository.

@strapi-community/dockerize

@strapi-community/dockerize 包是一个 CLI 工具,可用于为 Strapi 项目生成 Dockerfiledocker-compose.yml 文件。

¥The @strapi-community/dockerize package is a CLI tool that can be used to generate a Dockerfile and docker-compose.yml file for a Strapi project.

首先,在现有 Strapi 项目文件夹中运行 npx @strapi-community/dockerize@latest 并按照 CLI 提示操作。

¥To get started run npx @strapi-community/dockerize@latest within an existing Strapi project folder and follow the CLI prompts.

更多信息请查看官方 GitHub 存储库npm 包

¥For more information please see the official GitHub repository or the npm package.

@strapi-community/deployify

@strapi-community/deployify 包是一个 CLI 工具,可用于将应用部署到各种云提供者和托管服务。其中一些还支持使用 Docker 容器部署 Strapi 项目,并且将调用 @strapi-community/dockerize 包来生成所需的文件(如果这些文件尚不存在)。

¥The @strapi-community/deployify package is a CLI tool that can be used to deploy your application to various cloud providers and hosting services. Several of these also support deploying a Strapi project with a Docker container and will call on the @strapi-community/dockerize package to generate the required files if they don't already exist.

首先,在现有 Strapi 项目文件夹中运行 npx @strapi-community/deployify@latest 并按照 CLI 提示操作。

¥To get started run npx @strapi-community/deployify@latest within an existing Strapi project folder and follow the CLI prompts.

更多信息请查看官方 GitHub 存储库npm 包

¥For more information please see the official GitHub repository or the npm package.

Docker 常见问题解答

¥Docker FAQ

为什么 Strapi 不提供官方 Docker 镜像?

¥Why doesn't Strapi provide official Docker images?

Strapi 是一个可用于构建许多不同类型的应用的框架。因此,不可能提供可用于所有用例的单个 Docker 映像。

¥Strapi is a framework that can be used to build many different types of applications. As such, it is not possible to provide a single Docker image that can be used for all use cases.

为什么我们有不同的 Dockerfile 用于开发和生产?

¥Why do we have different Dockerfiles for development and production?

出现各种 Docker 镜像的主要原因是我们的管理面板的构建方式。管理面板是使用 React 构建的,并在构建过程中打包到 Strapi 应用中。这意味着 Strapi 后端充当 Web 服务器来为管理面板提供服务,因此某些环境变量会静态编译到内置的管理面板中。

¥The primary reason for various Docker images is due to the way our Admin panel is built. The Admin panel is built using React and is bundled into the Strapi application during the build process. This means that the Strapi backend is acting as a web server to serve the Admin panel and thus certain environment variables are statically compiled into the built Admin panel.

通常认为使用 Strapi 为开发和生产环境构建不同的 Docker 镜像是最佳实践。这是因为开发环境未针对性能进行优化,并且不打算暴露在公共互联网上。

¥It is generally considered a best practice with Strapi to build different Docker images for development and production environments. This is because the development environment is not optimized for performance and is not intended to be exposed to the public internet.