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...
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.
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:
Run container
Execute your commands against the running container using docker exec
Remove it
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
Related
I'm trying to run the npm init command to spit out a package.json file on my local machines current working directory, by running a Node image on docker.
I have attempted to do it like so but haven't had the desired results described above.
docker run -d -v $pwd:~/tmp node:18-alpine3.14 "cd ~/tmp && npm init"
The command I'm passing above at the end gets passed to the Node application rather than the container it is held inside. I know this because the container exits with Error: Cannot find module '/cd ~/tmp && npm init'.
How can i execute commands for the container to receive rather than Node inside it in this example?
You cloud use sh -c "some command" as command, but I think it's cleaner, like below.
Using the workdir flag and also using your local user and group id so that you don't have to fix the permissions later on.
docker run --rm \
--user "$(id -u):$(id -g)" \
--workdir /temp \
--volume "$PWD:/tmp" \
--tty
--interactive
node:18-alpine3.14 npm init
I am also using tty and interactive so that you can answer the questions from npm init. If you dont want questions, use npm init -y.
When right-clicking on an image in Microsoft's Docker plugin and selecting run it executes the following command:
docker run --rm -d -p 3000:3000/tcp falaen:latest
I would like it to run the following command instead:
docker run --restart=always -v /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock -p 3000:3000 -d falaen
I have tried to modify the run command in the Dockerfile but without success.
Of course, I can run the command manually in the terminal each time but it would be nicer to simply right click on the image and then run it.
You could try adding this line to your VSCode's settings.json, to override the default configuration of the command.
"docker.commands.run": "docker run --restart=always -v /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock -p 3000:3000 -d ${tag}"
This will run the command using any image you select.
I'm newbie to docker, I tried this command
docker run -it node:latest
then, I was in the node REPL,
Welcome to Node.js v16.3.0.
Type ".help" for more information.
>
I tried control+c ,but this quit the image,
Is there any way to go to the shell in this image?
In order to overwrite the entry point of the docker image you're using, you will need to use the --entrypoint flag in the run command.
docker run -it --entrypoint bash node:latest
For better understanding on how to work with already running docker container you can refer to the following question
Is there a way to install node packages in a running docker environment without restarting?
I have running a few containers via docker-compose and need to use npm i <packagename> while the containers are running.
So far, I have found no consistent answer to google.
docker exec -it [container-id] /bin/bash
cd to the workspace and npm install packagename . Should install the package you want and also add it in package.json
Use docker exec + npm install <package-name>.
https://docs.docker.com/engine/reference/commandline/exec/
You can run any command in an active container using docker exec. In your case it will be:
// Replace <your-container-id> and <your-package-name>
docker exec -it <your-container-id> "npm install <your-package-name>"
or if you want to use a container name instead of a container id you can use:
// Replace <your-container-id> and <your-package-name>
docker exec -it $(docker ps | grep <your-container-name> | awk '{ print $1 }') "npm install <your-package-name>"
Here you have more informations about the docker exec docker exec command. (Docker Docs)
I'm trying to run a Spark instance using Docker (on Windows) following this explanation: https://github.com/sequenceiq/docker-spark
I was able to:
Pull the image
Build the image
I had to download the Github repository with the Dockerfile though and specify that in the build command. So instead of docker build --rm -t sequenceiq/spark:1.6.0 . I had to run docker build --rm -t sequenceiq/spark:1.6.0 /path/to/dockerfile
However when I try to run the following command to run the container:
docker run -it -p 8088:8088 -p 8042:8042 -p 4040:4040 -h san
dbox sequenceiq/spark:1.6.0
I get the error:
Error response from daemon: Container command '/etc/bootstrap.sh' not found or does not exist.
I tried copying the bootstrap.sh file from the Github repository to the /etc directory on the VM but that didn't help.
I'm not sure what went wrong, any advice would be more than welcome!
It is probably an issue with the build context because you changed the path to the Dockerfile in your build command.
Instead of changing the path to the Dockerfile in the build command, try cd'ing into that directory first and then running the command. Like so:
cd /path/to/dockerfile
docker build --rm -t sequenceiq/spark:1.6.0 .