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" ]
Just use the WORKDIR command to change the directory you want to. Any other commands you use beyond this command will be executed in the directory you have set. It is also a better practice to make use of WORKDIR in docker.
The CMD command specifies the instruction that is to be executed when a Docker container starts.
An essential feature of a CMD command is its ability to be overridden. This allows users to execute commands through the CLI to override CMD instructions within a Dockerfile. A Docker CMD instruction can be written in both Shell and Exec forms as: Exec form: CMD [“executable”, “parameter1”, “parameter2”]
To run a command in a certain directory of your container, use the --workdir flag to specify the directory: docker exec --workdir /tmp container-name pwd This example command sets the /tmp directory as the working directory, then runs the pwd command, which prints out the present working directory:
Follow the steps below to find out how to change directories in CMD: Go to the search bar on the far-left side of your taskbar. Enter “CMD” or “Command Prompt” in the search bar. In the Command Prompt window, type in “ cd .” Press the “Space” key on your keyboard.
In this example, the container ID and name are highlighted. You may use either to tell docker exec which container to use. If you’d like to rename your container, use the docker rename command: Next, we’ll run through several examples of using docker exec to execute commands in a running Docker container.
To exit back out of the container, type exit then press ENTER: If your container image includes a more advanced shell such as bash, you could replace sh with bash above. If you need to run a command inside a running Docker container, but don’t need any interactivity, use the docker exec command without any flags:
If you have the dockerfile under control, simply add a line BEFORE CMD:
WORKDIR '/functions'
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" ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With