skip to Main Content

I try to containerize the react application, but this error persists.

Here’s the log:

[+] Building 0.0s (0/0)  docker:default

[+] Building 4.2s (9/16)                                                                                                                                                                         docker:default
 => [internal] load build definition from Dockerfile                                                                                                                                                       0.0s

 => => transferring dockerfile: 380B                                                                                                                                                                       0.0s 

 => [internal] load metadata for docker.io/library/nginx:stable-perl                                                                                                                                       4.0s 
 => [internal] load metadata for docker.io/library/node:lts-alpine                                                                                                                                         4.0s 
 => [auth] library/nginx:pull token for registry-1.docker.io                                                                                                                                               0.0s
 => [auth] library/node:pull token for registry-1.docker.io                                                                                                                                                0.0s
 => [internal] load .dockerignore                                                                                                                                                                          0.0s 
 => => transferring context: 52B                                                                                                                                                                           0.0s 
 => CANCELED [build 1/7] FROM docker.io/library/node:lts-alpine@sha256:a7b980c958bfe4d84ee9263badd95a40c8bb50ad5afdb87902c187fefaef0e24                                                                    0.1s 
 => => resolve docker.io/library/node:lts-alpine@sha256:a7b980c958bfe4d84ee9263badd95a40c8bb50ad5afdb87902c187fefaef0e24                                                                                   0.0s 
 => => sha256:a7b980c958bfe4d84ee9263badd95a40c8bb50ad5afdb87902c187fefaef0e24 1.43kB / 1.43kB                                                                                                             0.0s 
 => => sha256:db4b1d98b5a92263f2b9221c4d7930ce6b02f9834926c8b6174bfed2913cea16 1.16kB / 1.16kB                                                                                                             0.0s 
 => => sha256:5414852b0111feebb3686ec58f92f2edb534cef2826b575581cdcadd42a7f527 7.21kB / 7.21kB                                                                                                             0.0s 
 => ERROR [internal] load build context                                                                                                                                                                    0.1s 
 => => transferring context: 1.43MB                                                                                                                                                                        0.0s 

 => CANCELED [stage-1 1/2] FROM docker.io/library/nginx:stable-perl@sha256:3083ef7ca8bc4f7dc24596382092ae5e53d1169a6e706b83c2b448d044919ba3                                                                0.1s 
 => => resolve docker.io/library/nginx:stable-perl@sha256:3083ef7ca8bc4f7dc24596382092ae5e53d1169a6e706b83c2b448d044919ba3                                                                                 0.0s 
 => => sha256:3083ef7ca8bc4f7dc24596382092ae5e53d1169a6e706b83c2b448d044919ba3 10.27kB / 10.27kB                                                                                                           0.0s 
 => => sha256:15585364514a1e9df46ec8d6ddfd1a56ee316724f47cb7b375ce4d4df137c856 2.49kB / 2.49kB                                                                                                             0.0s 
 => => sha256:e790832271e7c18bce18208197ed61dde7dd4821af50789ceb4bc2c5d1b0b205 9.97kB / 9.97kB                                                                                                             0.0s 

------
 > [internal] load build context:
------
ERROR: failed to solve: archive/tar: unknown file mode ?rwxr-xr-x

I looked it up; it seems to have something to do with the file permissions. I tried changing the files in the directory via Windows Explorer, but nothing happens.

I have tried the chmod but still no luck.

Here is the Dockerfile

FROM node:lts-alpine AS build

WORKDIR /optimal/

COPY package*.json ./

RUN npm install

COPY src/ /optimal/src

COPY public/ /optimal/public

RUN chmod -R 755 /optimal/src /optimal/public

RUN npm run build

CMD [ "npm", "start" ]

FROM nginx:stable-perl

WORKDIR /usr/share/nginx/html

COPY –from=build /optimal/build /usr/share/nginx/html

EXPOSE 80

CMD [ "nginx", "-g", "daemon off;" ]

2

Answers


  1. this error is related to a file in your build context having an unusual or invalid file mode that Docker doesn’t recognize. This can often happen when the files are copied from a Windows filesystem, which uses different file permission schemes compared to Unix-based systems.

    Check the File Permissions and Attributes:

    Ensure that the files in your context directory (especially src and public) don’t have unusual attributes. You can use ls -l to check the file permissions on a Unix-like system.

    Ensure Correct Line Endings:

    Sometimes, files might have incorrect line endings if edited on Windows. Use tools like dos2unix to convert the files.
    find . -type f -exec dos2unix {} ;

    then try this dockerfile

    FROM node:lts-alpine AS build
    
    WORKDIR /optimal/
    
    # Copy package files and install dependencies
    COPY package*.json ./
    RUN npm install
    
    # Copy source files
    COPY src/ /optimal/src
    COPY public/ /optimal/public
    
    # Sanitize file permissions in the build context
    RUN find /optimal/src /optimal/public -type d -exec chmod 755 {} ;
    RUN find /optimal/src /optimal/public -type f -exec chmod 644 {} ;
    
    RUN npm run build
    
    FROM nginx:stable-perl
    
    WORKDIR /usr/share/nginx/html
    
    # Copy build artifacts
    COPY --from=build /optimal/build /usr/share/nginx/html
    
    EXPOSE 80
    
    CMD ["nginx", "-g", "daemon off;"]
    

    Ensure you have a .dockerignore file to exclude unnecessary files. Example:

    node_modules
    Dockerfile
    .dockerignore

    Rebuild Docker Image:

    Clean up Docker and rebuild the image:

    docker system prune --all --force
    docker build -t my-react-app .

    Login or Signup to reply.
  2. initially nothing worked for me too, but it finally worked when I just ran docker compose build using Git Bash. The one you get when you install git from https://git-scm.com/download/win

    Could you also try that and see if it works for you?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search