简体   繁体   中英

Running a linux command in Dockerfile throwing error

This is my Dockerfile content and it keeps throwing me error in running this statement. Any idea what am I doing wrong. I just want to dump ENV VARS into a file for React build on the container.

FROM node:12
WORKDIR /usr/src/app
COPY . .
RUN printenv | grep REACT_APP_ > client/.env
# RUN ["printenv", "|", "grep", "REACT_APP_", ">", "client/.env"]
RUN npm run setup
RUN npm run build:all

EXPOSE 3005
CMD [ "npm", "run", "start:prod" ]

Error: ERROR: Service '<name>' failed to build: The command 'printenv | grep REACT_APP_ > client/.env' returned a non-zero code: 1 ERROR: Service '<name>' failed to build: The command 'printenv | grep REACT_APP_ > client/.env' returned a non-zero code: 1

I have spent many hours with no luck. Help appreciated.

So there's a couple things to consider:

  1. Make sure the client dir exists. It doesn't come in the node:12 image by itself
  2. Make sure the env has a variable that will allow the grep REACT_APP_ to find something. If it does not, grep will return 1 and the docker image build will halt. 1 is normal return code when grep finds nothing, so it's not an error (and thus nothing more is printed). But the docker build treats it as an error since it's nonzero, and thus the build stops.

Also to read on EXIT CODES on man grep page,

EXIT STATUS Normally the exit status is 0 if a line is selected, 1 if no lines were selected, and 2 if an error occurred.

Here's a test I did that passes (slightly modified, just based on the considerations above):

FROM node:12
WORKDIR /usr/src/app
COPY . .
RUN mkdir client
ENV REACT_APP_1 1
RUN printenv | grep REACT_APP_ > client/.env
RUN cat client/.env

The output I get is this:

$ docker build -t test .
Sending build context to Docker daemon  2.048kB
Step 1/7 : FROM node:12
 ---> 28faf336034d
Step 2/7 : WORKDIR /usr/src/app
 ---> Running in 753293fa6257
Removing intermediate container 753293fa6257
 ---> 3a04798b1b9f
Step 3/7 : COPY . .
 ---> 3dd0d465a6e2
Step 4/7 : RUN mkdir client
 ---> Running in d513df2a0a34
Removing intermediate container d513df2a0a34
 ---> d46aa5200ae7
Step 5/7 : ENV REACT_APP_1 1
 ---> Running in af81940a90fb
Removing intermediate container af81940a90fb
 ---> 6169ad694a4d
Step 6/7 : RUN printenv | grep REACT_APP_ > client/.env
 ---> Running in 365020eeb2e5
Removing intermediate container 365020eeb2e5
 ---> b6ef574c48c8
Step 7/7 : RUN cat client/.env
 ---> Running in a6a69d6ba6c2
REACT_APP_1=1
Removing intermediate container a6a69d6ba6c2
 ---> 0814306133f0
Successfully built 0814306133f0
Successfully tagged test:latest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM