How to override the CMD command in the docker run line
Asked Answered
T

4

31

How you can replace the cmd based on the docker documentation: https://docs.docker.com/engine/reference/builder/#cmd

You can override the CMD command

Dockerfile:

RUN chmod +x /srv/www/bin/* & chmod -R 755 /srv/www/app
RUN pip3 install -r /srv/www/app/pip-requirements.txt
EXPOSE 80
CMD ["/srv/www/bin/gunicorn.sh"]

the docker run command is:

docker run --name test test/test-backend

I tried

docker run --name test test --cmd ["/srv/www/bin/gunicorn.sh"]
docker run --name test test cmd ["/srv/www/bin/gunicorn.sh"]

But the console say this error:

System error: exec: "cmd": executable file not found in $PATH
Tristan answered 6/10, 2015 at 21:8 Comment(0)
T
29

The right way to do it is deleting cmd ["..."]

 docker run --name test test/test-backend /srv/www/bin/gunicorn.sh
Tristan answered 6/10, 2015 at 21:8 Comment(0)
P
14

The Dockerfile uses CMD instruction which allows defaults for the container being executed.

The below line will execute the script /srv/www/bin/gunicorn.sh as its already provide in CMD instruction in your Dockerfile as a default value, which internally gets executed as /bin/sh -c /srv/www/bin/gunicorn.sh during runtime.

 docker run --name test test/test-backend 

Now say if you want to run some thing else, just add that at a end of the docker run. Now below line should run bash instead.

docker run --name test test/test-backend /bin/bash

Ref: Dockerfile Best Practices

Platform answered 7/10, 2015 at 3:32 Comment(0)
C
4

For those using docker-compose:

docker-compose run [your-service-name-here-from-docker-compose.yml] /srv/www/bin/gunicorn.sh

In my case I'm reusing the same docker service to run the development and production builds of my react application:

docker-compose run my-react-app npm run build

Will run my webpack.config.production.js and build the application to the dist. But yes, to the original question, you can override the CMD in the CLI.

Corundum answered 10/3, 2017 at 17:59 Comment(0)
C
0

Try this in your Dockerfile

CMD ["sh" , "-c", "command && bash"]
Chief answered 30/12, 2021 at 22:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.