概述
Docker 镜像构建工作流是一个初级难度的工作流。从 Dockerfile 构建 Docker 镜像,打标签并验证运行正确后再推送到仓库。该工作流的目标是:Build and verify a Docker image locally before sharing it with the team or pushing to a registry.
环境准备
执行本工作流需要以下环境:Docker installed and running;A Dockerfile in the project root;A .dockerignore file for exclusions。请确保在开始前准备好所有必需的工具和资源,以避免中途因环境问题中断。
执行步骤
本工作流包含3个步骤。步骤1:Build the image——Run docker build -t myapp:latest . to build the image from the Dockerfile. 步骤2:Run the image——Run docker run -p 8080:8080 myapp:latest and verify the application responds. 步骤3:Check image size——Run docker images myapp:latest to check the image size against the team budget.
验证与清理
预期结果:A verified Docker image is available locally, ready to be pushed to a registry.。验证方法:Run the container and curl the health endpoint to confirm the application is responding correctly.。清理操作:Remove the image with docker rmi myapp:latest if not needed.。estimatedMinutes 为 20 分钟。
代码示例
docker build -t myapp:latest .
docker run -p 8080:8080 myapp:latest
curl localhost:8080/health