12

I'm trying to use docker for a firebase project, for firebase the package.json is in a sub folder called functions. I'm using the node:alpine image.

In my Dockerfile, I need to cd into the functions directory then run npm run start. How do I do this, please?

I have tried CMD [ "cd", "functions", ";", "npm", "run", "serve" ], I got this error /usr/local/bin/docker-entrypoint.sh: exec: line 8: cd: not found

FROM node:alpine

WORKDIR '/app'

RUN npm install -g firebase-tools

COPY functions/package*.json functions/

RUN cd functions && npm install && cd ..

COPY . .

CMD [ "cd", "functions", ";", "npm", "run", "serve" ]
1
  • That's because there is no external "cd" executable; it's a built-in shell command. When using [], the command is being executed directly, not via a shell. That also means that the ";" would be passed as a parameter and not be interpreted as a command separator. Basically, you tried the equivalent of /bin/cd function \; npm run serve Commented Feb 6, 2023 at 13:50

2 Answers 2

10

If you have the dockerfile under control, simply add a line BEFORE CMD:

WORKDIR '/functions'
Sign up to request clarification or add additional context in comments.

Comments

9

the correct way is to use workdir:

WORKDIR /functions
CMD [ "npm", "run", "serve" ]

or just use:

CMD [ "/bin/sh" , "-c" , "cd /functions && npm run serve" ]

3 Comments

I got this error while running with "/functions": can't cd to /functions: No such file or directory, but using "functions" works just fine.
Your data are in /app/functions. Take a look at the the workdir and copy commands in your dockerfile
CMD cd /functions && npm run serve is equivalent to the last line (Docker in fact expands it to an identical form).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.