我有一个 Angular 17 项目,我想对其进行 dockerize。我已经构建了我的图像,当我尝试使用
docker run -dit --name <container name> <imagename>
运行容器时,但容器随后立即退出。当我运行 docker ps
时,我的容器没有出现。
Dockerfile:
# Use node image as builder
FROM node:18.14 AS builder
# Set working directory in the container
WORKDIR /app
# Copy package.json and package-lock.json to the container
COPY package.json package-lock.json ./
# Install dependencies
RUN npm install
# Copy the rest of the application code
COPY . .
# Build the application
RUN npm run build --prod
# Use nginx image for serving the application
FROM nginx:latest
# Copy built application files from builder stage to nginx directory
COPY --from=builder /app/dist/project /usr/share/nginx/html
# Expose port 80 for nginx
EXPOSE 80
# Start nginx in foreground
CMD ["nginx", "-g", "daemon off;"]
正如您在评论中提到的
exec /docker-entrypoint.sh: exec format error
是真正需要解决的问题。
你能想出一个最小的、可重现的例子吗?举个例子,这对我来说在本地工作
# Use node image as builder
FROM node:18.14 AS builder
# Set working directory in the container
WORKDIR /app
# (pretend to) Build the application
RUN echo "Hello world" > out.txt
# Use nginx image for serving the application
FROM nginx:latest
# Copy built application files from builder stage to nginx directory
COPY --from=builder /app/ /usr/share/nginx/html
这可能与处理器架构差异有关,请参阅在 M1 Mac 上构建时 Docker 失败(exec /usr/local/bin/docker-entrypoint.sh:exec 格式错误) 和 https://docs.docker.com/构建/指南/多平台/
如果您在本地构建图像并在其他地方运行它,这很可能是解决方案 - https://stackoverflow.com/a/69636473/131391
如果您在本地构建和运行,那么我首先确保您可以在编写 Dockerfile 之前简单地运行提到的基础映像。例如
docker run nginx:latest