skip to Main Content

I have the following simple Dockerfile

FROM node:16

COPY . .
RUN npm install

CMD ["npm", "run", "dev"]

It runs a NextJS application. I need to make two CURL Requests from the container but, I can’t do:

CMD ["npm", "run", "dev", "&&", "curl", "localhost:3000/.....

Because the following error appears:

error - Invalid project directory provided, no such directory: /&&

It seems that it’s trying to catch the curl command as a directory or a flag.

What can I do? Also, I need to kill the server when the two CURL Requests are done (kill the docker container).

2

Answers


  1. Chosen as BEST ANSWER

    I solved it with the following line:

    CMD (npm run dev &) && sleep 10 && curl -X PUT localhost:3000/api/1 && curl -X PUT localhost:3000/api/2
    

    It is not the best solution because I can't control the time when "npm run dev" is running. But in my case, it takes 3 seconds so it is safer for me, so it works.

    Anyways, a better solution for the "sleep 10" could be to check the server until this is ready, in a for loop.


  2. You’re using the ‘exec’ form of the CMD statement which doesn’t run a shell. That’s usually preferred because it takes up a bit less memory.

    But && is a shell operator, so you need a shell to be able to do that. So instead you can use the ‘shell’ form of the CMD statement and do

    CMD npm run dev && curl localhost:3000/.....
    

    More info here.

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