Skip to main content

在 Docker 容器中运行 Strapi

🌐 Running Strapi in a Docker container

Caution

Strapi 不会构建任何官方容器镜像。以下说明是作为对社区的友好提供。如果你有任何问题,请在 Discord联系我们。

Warning

Strapi 应用不适合连接到已有的数据库(即不是由 Strapi 应用创建的),也不适合连接到 Strapi v3 数据库。Strapi 团队将不支持此类尝试。尝试连接不受支持的数据库可能,并且很可能会导致数据丢失,例如表被删除。

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

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 来启动一个数据库容器。

这两种方法都需要一个已存在的 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.

Note

如果你正在使用 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 及其命令的更多信息,请参阅 official Docker documentation

示例 Dockerfile

🌐 Sample Dockerfile:

./Dockerfile
FROM node:22-alpine
# 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 Compose

🌐 (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.

Note

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

示例 docker-compose.yml

🌐 Sample docker-compose.yml:

./docker-compose.yml
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:8.0
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,就可以构建 生产容器。可选地,可以将容器发布到 注册表,以使社区可以使用它。社区工具 可以帮助你在构建生产 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:22-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
ARG 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:22-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 documentation 以获取有关使用更高级选项构建 Docker 镜像的更多信息。

要为 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 documentation ,以获取有关使用更高级选项发布 Docker 镜像的更多信息。

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

🌐 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 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,并按照命令行提示操作。

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

欲了解更多信息,请参阅官方 GitHub repositorynpm 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,并按照命令行提示操作。

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

欲了解更多信息,请参阅官方 GitHub repositorynpm 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.