代码之家  ›  专栏  ›  技术社区  ›  user2105483

Docker compose没有更新源代码

  •  0
  • user2105483  · 技术社区  · 1 年前

    我在更新&&重新构建我的代码库

    我使用这个命令,但它不会更新我的代码库 docker compose up --build -d --force-recreate

    我在合成之前尝试过使用此命令清除数据 docker compose down -v --rmi all --remove-orphans 而且效果很好。如何在每次构建(docker nextjs容器)之前使用docker compose-up而不删除所有数据等?

    Docker版本24.0.5,内部版本为ced0996

    Dockerfile

    FROM node:18-alpine AS base
    
    # Install dependencies only when needed
    FROM base AS deps
    # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
    RUN apk add --no-cache libc6-compat
    WORKDIR /app
    
    # Install dependencies based on the preferred package manager
    COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
    RUN \
      if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
      elif [ -f package-lock.json ]; then npm ci; \
      elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
      else echo "Lockfile not found." && exit 1; \
      fi
    
    
    # Rebuild the source code only when needed
    FROM base AS builder
    WORKDIR /app
    COPY --from=deps /app/node_modules ./node_modules
    COPY . .
    
    # Next.js collects completely anonymous telemetry data about general usage.
    # Learn more here: https://nextjs.org/telemetry
    # Uncomment the following line in case you want to disable telemetry during the build.
    # ENV NEXT_TELEMETRY_DISABLED 1
    
    RUN npm run build
    
    # Production image, copy all the files and run next
    FROM base AS runner
    WORKDIR /app
    
    ENV NODE_ENV production
    # Uncomment the following line in case you want to disable telemetry during runtime.
    # ENV NEXT_TELEMETRY_DISABLED 1
    
    RUN addgroup --system --gid 1001 nodejs
    RUN adduser --system --uid 1001 nextjs
    
    COPY --from=builder /app/public ./public
    COPY --from=builder /app/package.json ./package.json
    
    # Set the correct permission for prerender cache
    RUN mkdir .next
    RUN chown nextjs:nodejs .next
    
    # Automatically leverage output traces to reduce image size
    # https://nextjs.org/docs/advanced-features/output-file-tracing
    COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
    COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
    
    USER nextjs
    
    EXPOSE 3000
    
    ENV PORT 3000
    # set hostname to localhost
    ENV HOSTNAME "0.0.0.0"
    
    CMD ["node", "server.js"]
    

    docker-compose.yml

    version: '3'
    
    services:
      nextjs-app:
        build:
          context: .
          dockerfile: Dockerfile
        container_name: docker-nextjs
        restart: always
        environment:
          NODE_ENV: production
        volumes:
          - nextjs-app-build:/app # Named volume for nextjs-app build folder
        ports:
          - '3000:3000'
        networks:
          - nginx_network
      nginx:
        image: csrinu236/medium-nginx-app # Placeholder for pushing image to Dockerhub
        build:
          context: ./nginx-docker
          dockerfile: Dockerfile
        volumes:
          - nextjs-app-build:/app # Mount the named volume to Nginx's /app/build
        depends_on:
          - nextjs-app
        restart: always
        ports:
          - '80:80'
          - '443:443'
        command: ['sh', '/etc/nginx/convert-nginx.sh']
        networks:
          - nginx_network
    
    volumes:
      nextjs-app-build: # Define the named volume
    
    networks:
      nginx_network:
        external: true
    
    1 回复  |  直到 1 年前
        1
  •  1
  •   David Maze    1 年前

    删除所有 volumes: 文件中的块。更具体地说,删除 卷: 安装在图像上的 /app 目录

    这里发生的情况是,中有一个旧的应用程序副本 nextjs-app-build 命名卷。您成功地重建了映像,但随后将卷装载到其内容上,命名卷的内容完全替换了重建映像中的内容。

    看起来您正试图利用特定Docker命名卷的特定行为,将文件从一个容器共享到另一个容器。当您将Docker命名的卷装载到容器(以及 只有 Docker命名的卷,而不是绑定装载或Kubernetes PersistentVolumeClaim或其他任何东西),第一次使用该卷时(以及 只有 如果卷完全为空),则将图像中的内容复制到卷中。这将导致第一个图像的内容被复制到卷中,然后将卷装入第二个容器中。但是,如果你更新了图像内容,卷就不会更新(Docker对其内容一无所知,并假设它有某种用户数据),你就会得到这种行为。

    这就引出了一个问题:你的静态资产是如何进入反向代理的?

    最简单的解决方案就是不去麻烦。在最现代的应用程序框架中,您可以在应用程序本身中设置一个静态文件服务器。这时,您可以将请求转发到后端,并让它处理自己的文件。

    # in your nginx config
    location / {
      proxy_pass http://nextjs-app:3000/;
      # with no try_files
    }
    

    另一种选择是 COPY 将文件转换为Nginx代理映像。您可以在同一个Dockerfile中执行此操作,作为结尾的附加阶段

    FROM nginx AS proxy
    COPY --from=builder /app /app # (...or to /var/www/html?)
    COPY ./nginx-docker/convert-nginx.sh /etc/nginx
    CMD ["/etc/nginx/convert-nginx.sh"]
    

    然后在Compose文件中,您需要指定要使用Dockerfile的哪一部分。

    version: '3.8'
      services:
        build:
          context: .
          target: runner # <-- add
        restart: always
        ports:
          - '3000:3000'
      nginx:
        image: csrinu236/medium-nginx-app # Placeholder for pushing image to Dockerhub
        build:
          context: .     # <-- change to top-level directory (same as app)
          target: proxy  # <-- add
        depends_on:
          - nextjs-app
        restart: always
        ports:
          - '80:80'
          - '443:443'
    
    # also note no volumes:, and using the default Compose-provided networks:
    

    你也可以 COPY --from 一个图像名称,它将允许您保留两个独立的Dockerfile(假设您正在分配 image: 两者的名称)。其中的一个技巧是Compose无法对图像构建进行排序,因此您必须先手动构建应用程序,然后再手动构建代理。

    推荐文章