使用 Docker 构建你的 Astro 站点
Docker 是一个使用容器构建、部署和运行应用的工具。
¥Docker is a tool to build, deploy, and run applications using containers.
Docker 镜像和容器可以部署到许多不同的平台,例如 AWS、Azure 和 谷歌云。本教程不会介绍如何将站点部署到特定平台,但会向你展示如何为项目设置 Docker。
¥Docker images and containers can be deployed to many different platforms, like AWS, Azure, and Google Cloud. This recipe won’t cover how to deploy your site to a specific platform but will show you how to set up Docker for your project.
¥Prerequisites
-
Docker 安装在你的本地计算机上。你可以找到 此处适用于你的操作系统的安装说明。
-
你项目中的 Dockerfile。你可以 在此处了解有关 Dockerfile 的更多信息 并使用下一节中的 Dockerfile 作为起点。
创建 Dockerfile
Section titled 创建 Dockerfile¥Creating a Dockerfile
在项目的根目录中创建一个名为 Dockerfile
的文件。该文件包含构建站点的说明,该说明将根据你的需求而有所不同。本指南无法显示所有可能的选项,但将为你提供 SSR 和静态模式的起点。
¥Create a file called Dockerfile
in your project’s root directory. This file contains the instructions to build your site, which will differ depending on your needs. This guide can’t show all possible options but will give you starting points for SSR and static mode.
如果你使用 npm 之外的其他包管理器,则需要相应地调整命令。
¥If you’re using another package manager than npm, you’ll need to adjust the commands accordingly.
此 Dockerfile 将构建你的站点并在端口 4321
上使用 Node.js 为其提供服务,因此需要在你的 Astro 项目中安装 节点适配器。
¥This Dockerfile will build your site and serve it using Node.js on port 4321
and therefore requires the Node adapter installed in your Astro project.
添加 .dockerignore
Section titled 添加 .dockerignore¥Adding a .dockerignore
将 .dockerignore
文件添加到你的项目中是最佳实践。该文件描述了 Docker COPY
或 ADD
命令中应忽略哪些文件或文件夹,与 .gitignore
的工作方式非常相似。这加快了构建过程并减小了最终图片的大小。
¥Adding a .dockerignore
file to your project is best practice. This file describes which files or folders should be ignored in the Docker COPY
or ADD
commands, very similar to how .gitignore
works. This speeds up the build process and reduces the size of the final image.
该文件应与 Dockerfile
本身位于同一目录中。阅读 .dockerignore
文档以获取更多信息
¥This file should go in the same directory as the Dockerfile
itself. Read the .dockerignore
documentation for extra info
¥Static
Apache (httpd)
Section titled Apache (httpd)以下 Dockerfile 将构建你的站点并使用默认配置在端口 80
上使用 Apache httpd 为其提供服务。
¥The following Dockerfile will build your site and serve it using Apache httpd on port 80
with the default configuration.
NGINX
Section titled NGINX为了构建上面的 Dockerfile,你还需要为 NGINX 创建一个配置文件。在项目的根目录中创建一个名为 nginx
的文件夹,并在其中创建一个名为 nginx.conf
的文件。
¥In order to build the Dockerfile above, you’ll also need to create a configuration file for NGINX. Create a folder called nginx
in your project’s root directory and create a file called nginx.conf
inside.
多阶段构建(使用 SSR)
Section titled 多阶段构建(使用 SSR)¥Multi-stage build (using SSR)
下面是一个更高级的 Dockerfile 示例,借助 Docker 的 多阶段构建,它可以在仅源代码发生更改时不重新安装 npm 依赖,从而优化站点的构建过程。这甚至可以将构建时间缩短几分钟,具体取决于依赖的大小。
¥Here’s an example of a more advanced Dockerfile that, thanks to Docker’s multi-stage builds, optimizes the build process for your site by not reinstalling the npm dependencies when only the source code changes. This can reduce the build time even by minutes, depending on the size of your dependencies.
¥Recipe
-
Build your container by running the following command in your project’s root directory. Use any name for
<your-astro-image-name>
:This will output an image, which you can run locally or deploy to a platform of your choice.
-
To run your image as a local container, use the following command.
Replace
<local-port>
with an open port on your machine. Replace<container-port>
with the port exposed by your Docker container (4321
,80
, or8080
in the above examples.)You should be able to access your site at
http://localhost:<local-port>
. -
Now that your website is successfully built and packaged in a container, you can deploy it to a cloud provider. See the Google Cloud deployment guide for one example, and the Deploy your app page in the Docker docs.