user3142695
user3142695

Reputation: 17322

Running commands for docker container

This is how I'm running a command in a docker container:

$ docker run -it --rm --name myapp myimage:latest
$ node --version

Is it possible to to run this as one command? Can I pass a command to the docker run-command?

Something like

$ docker run -it --rm --name myapp myimage:latest "node --version"

Of course this is just a simple example. Later I will execute some more complex commands...

Upvotes: 0

Views: 1942

Answers (3)

Ashu Joshi
Ashu Joshi

Reputation: 560

The "docker run" command essentially will make the container run, and execute the "CMD" or "ENTRYPOINT" in the Dockerfile. Unless - the command in your dockerfile does not run a command prompt - "running" the container may not get you the prompt.

For example if you want that everytime you run the container - it gets you the command prompt then have the line below in your Dockerfile.:

CMD ["bash"]

If you want to run the same commands everytime you run the command - then you could create a script file with your commands, copy them to the container, and execute the script file as a CMD directive.

Upvotes: 1

Ahmad Abdelghany
Ahmad Abdelghany

Reputation: 13198

The general form of the command is actually:

docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

See documentation for more details.

The answer to the minimal example in your question is simply:

docker run -it --rm --name myapp myimage:latest node --version

If you want to run multiple commands in sequence, you can:

  1. Run container
  2. Execute your commands against the running container using docker exec
  3. Remove it

Upvotes: 0

eran meiri
eran meiri

Reputation: 1508

I am having some trouble understanding what you are trying to do.

you can just : docker run -d --name myapp myimage:latest -f /dev/null and then your container is up and you can run any command in it

you can pass a command to the docker run but once the command ends the container will exit

Upvotes: 0

Related Questions