next/image is failing with Docker Compose #68774
-
SummaryIn Short: Note: The images this failed to fetch are available with the API URL ( 'use client'
import Image from "next/image";
type PostImgProps = {
imgUrl: string;
};
const PostImg = (p: PostImgProps) => {
// if I comment out the "Image" below there's no error remains
return (
<Image
className="object-cover"
src={p.imgUrl}
alt="post image"
width={1200}
height={1000}
/>
);
};
export default PostImg;I know that Next.js tries to render client components in the server too. So I've imported it like const PostImg = dynamic(
() => import('@/path/to/postImg'),
{
ssr: false, // completely disable server side rendering
loading: () => (
<div className="...">
<!-- loader code -->
</div>
)
}
);to disable server rendering. But I am still facing the error. As far as I can tell. As the component is still being rendered on the server. It fails to pull the image because the URL ( Thank you in advance for the help :) Additional informationNode: 20
Next: 14.2.5ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 4 replies
-
|
I've also created a question on StackOverflow |
Beta Was this translation helpful? Give feedback.
-
|
The issue is that, next/image works a bit differently than you might think. The image src, points to a So the whole dynamic thing, of disable server side rendering has no effect, for your issue. I guess you could do: And since that's all happening server side, you'd be fine. You probably also need to add these to the next.config.js file, either as remote patterns or domains, though the former is preferred. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks @icyJoseph, That feature of next/image is making the issue in the environment my app runs. API & the UI are in docker container (& under the same network). Next.js is allowed to call API using |
Beta Was this translation helpful? Give feedback.
-
|
Did you get a solution for this? |
Beta Was this translation helpful? Give feedback.
-
|
Context
Why Fix (minimum viable)
Notes
If this solves it, please Mark as answer so others can find it. |
Beta Was this translation helpful? Give feedback.
-
|
The issue occurs because next.config.ts (or next.config.js) is not being copied into the final Docker image. Make sure the config file is included in the runtime stage. Add one of the following lines to your Dockerfile: Once the config file is present in the final image, the issue is resolved. |
Beta Was this translation helpful? Give feedback.
-
|
This is a Docker networking issue. Inside your Solution1. Use Docker service names for internal URLsIn your Docker Compose, services communicate via their service names, not 2. Configure
|
Beta Was this translation helpful? Give feedback.
This is a Docker networking issue. Inside your
webcontainer,localhost:3000refers to the container itself, not yourapiservice.Solution
1. Use Docker service names for internal URLs
In your Docker Compose, services communicate via their service names, not
localhost. Sohttp://api:3000instead ofhttp://localhost:3000.2. Configure
next.config.jsfor Docker