Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker change directory in CMD command

Tags:

node.js

docker

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" ]
like image 622
its_tayo Avatar asked Jan 14 '20 08:01

its_tayo


People also ask

How do I change the directory in Dockerfile?

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.

What is CMD command in docker?

The CMD command​ specifies the instruction that is to be executed when a Docker container starts.

Can we override CMD in Dockerfile?

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”]

How do I run a command in a specific directory in Docker?

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:

How to change directories in CMD?

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.

How do I rename a docker container?

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.

How do I exit a docker container in Linux?

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:


Video Answer


2 Answers

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

WORKDIR '/functions'
like image 78
Christian Sauer Avatar answered Oct 08 '22 20:10

Christian Sauer


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" ]
like image 7
LinPy Avatar answered Oct 08 '22 19:10

LinPy