The Next.js application, running within a Docker container, is experiencing difficulties loading static image files from the public directory

I am facing an issue while attempting to run a Next.js app locally inside a Docker container. Everything works fine when I run the container, except for my image files not rendering on the page. Upon inspecting with developer tools, it shows a failed network request for those images without any error code displayed (no 4XX code). The failed request appears like this:

https://i.stack.imgur.com/qSFde.png

Interestingly, when I build with npm run build and run the app locally using npm run start, the same request runs successfully. Similarly, in development mode using npm run dev, it also succeeds.

Below is the section of code utilizing the Next Image module:

import Image from "next/image";

<Image
    src="/images/computerStation.png"
    alt=""
    height={300}
    width={600}
/>

Here is the directory tree structure for my public folder:

root   
│
└───public
    │   
    └───images
        │
        └───computerStation.png

Based on the success in my local build and development environment, I suspect there might be an issue with my Docker file configuration. I mostly followed the guidelines from the Next.js documentation but adapted it to work with npm instead of yarn. You can view my Dockerfile below:

# Install dependencies only when needed
FROM node:alpine AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install --frozen-lockfile

# Rebuild the source code only when needed
FROM node:alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN npm run build

# Production image, copy all the files and run next
FROM node:alpine AS runner
WORKDIR /app

ENV NODE_ENV production

COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules

RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
RUN chown -R nextjs:nodejs /app/.next
USER nextjs

EXPOSE 3000

RUN npx next telemetry disable

CMD ["node_modules/.bin/next", "start"]

I would appreciate any assistance on resolving this issue. Thank you!

Answer №1

It is essential to verify the Node version in your docker setup, and it should ideally be:

FROM node:14-alpine AS deps

FROM node:14-alpine AS builder

FROM node:14-alpine AS runner

Keep in mind that Next.js may encounter issues when running on Node 16 (node:alpine).

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Next.js Link feature does not always guarantee that the component will render correctly, and the serverSideProps function may not always receive updated

Having an issue with next.js - when a user tries to navigate from one profile to another using the Link in the navbar: <li> <Link href={`/profile/${user.user.id}`}> <a className="flex flex-row items-center"> ...

Amplify encounters the "Failed to load resource: the server responded with a status of 400" issue

I encountered an error while using Amplify, although the build was completed successfully. Failed to load resource: the server responded with a status of 400 manifest.json:1 The system is functional in the local environment. Below is the Package.json scri ...

transferring user input to the server side

I've created a form in React using Material UI, and I need to send the form data to the backend. Currently, all fields are displaying the values except for the dateAndTime field, which is not connected to the rest of the fields. How can I ensure that ...

Optimizing the usage of override Button with Box component in Material-UI

Recently, I've been delving into the Box component in material-UI (https://material-ui.com/components/box/#box) and attempting to understand and write code for it. Specifically, I've been experimenting with overriding a Button component using th ...

Issue with handling a Promise Rejection error in a React/Next.js project while working with the openAI API

Currently, I am in the midst of working on a React/Next.js project where I have come across an error regarding Unhandled Promise Rejection within my code. Resolving this issue has proven to be quite challenging. Here are the specifics of the problem: Erro ...

Maintaining Aspect Ratio and Adding Letterboxes with Next.js Image

In my Next.js app, there is a section dedicated to displaying selected photos from a gallery. It's crucial for this area to maintain a fixed size of 566px*425px as users navigate through images or when a photo is loading. While the layout is responsiv ...

Having difficulty coming back from a promise catch block

I'm struggling to populate a menu list from my PouchDB database because I am unable to retrieve anything within the promise that is executed after calling get on the db. Below is the code in question: <MenuList> {this.populateSavedClues()} ...

Interactive Tab Navigation using React Swipe

As a new user of React, I am exploring ways to implement swipeable tabs for mobile browsers (similar to Instagram's profile tabs). After some research, I came across react-swipeable-views and Material UI, which I tried using. While it seems to be fu ...

Generating a 3D model and integrating it with a 3D terrain display on Mapbox

My goal is to integrate a 3D model as a custom layer on Mapbox using Three.js, alongside adding a 3D terrain on the map. I have followed the documented examples for incorporating a 3D model and 3D terrain from Mapbox. The issue I'm encountering is th ...

How can I convert base64 data to a specific file type using React Cropper JS?

I have successfully implemented the crop functionality using react cropper js. Now, I am faced with a challenge of sending the cropped image as a file type. Currently, I can obtain the base64 string for the cropped image. However, when I submit the cropped ...

Problem encountered when trying to download an npm package directly from Github

Working on a React common project that I wanted to include in another React project (the 'base' project). I named the test project as shown below in the package.json file and pushed it to Github: { "name": "NPM-From-Git-Test", "version": "5. ...

The MUI date picker does not display dates earlier than 1900

I am in need of a datepicker that allows users to select dates from the 1850s, however, the mui datepicker only starts from the 1900s. To demonstrate this issue, I have provided a sample code in this codesandbox I am utilizing mui in the remainder of my ...

Is it possible to utilize fetch from the local drive in conjunction with App Router in the latest version of Next JS?

I encountered a 404 error and realized that the expected response from an on message listener had gone out of scope. Initially, I was working on creating an API for user authentication and was advised to convert it to route.js. So, I made the necessary ch ...

It appears that the home page of next.js is not appearing properly in the Storybook

Currently, I am in the process of setting up my next home page in storybooks for the first time. Following a tutorial, I successfully created my next-app and initialized storybooks. Now, I am stuck at importing my homepage into storybooks. To achieve this, ...

Closing a Bootstrap 5 Dropdown in React without using toggle feature

Currently, I am using a search field in my application that displays data as dropdown items when the user is typing. The library being utilized is React Bootstrap (bootstrap 5) and it's functioning perfectly. However, an issue arises where the dropdow ...

Changing external variables within a React component

I've been working on creating a form using redux-form that includes a dynamic set of checkboxes. The issue I'm facing is that these checkboxes are being passed to the component through props, preventing me from utilizing them in the fields secti ...

Create two grid components for Material-UI and use Flexbox to ensure that both items have equal height

I'm currently working on ensuring that two grid items have the same height using flex in Material UI, but I've hit a roadblock. It seems like my approach with the Grid element might be incorrect. I've experimented with adjusting the height a ...

Tips for adjusting the color of child elements when hovering over a card in Material UI

Having trouble changing the color of typography by hovering on a card? I tried many things, but finally decided to seek help here. When I remove the default color from typography and hover over the card, the text color changes as desired. However, I want t ...

Incorporate the following font into an external HTML file and retrieve the path to the asset

The latest next/font API offers convenient methods for applying fonts using classes, styles, or even CSS Variables directly. But the question is, how can a reference to the font be embedded for external HTML inclusion? For example, if I have this code: // ...

Guide to creating adaptive content using React and Material UI

For my current project, I am working on designing a product detail page using React and Material UI. The challenge I am facing is ensuring that the page remains responsive across different screen sizes. Specifically, I am struggling with the description se ...