How to get back to shell in nodejs:latest docker image? - node.js

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

Related

Run docker command into node container

I have a nodejs application inside a docker containter, and I'm trying to run another docker image from the container.
I connected the docker socket to the container, ran the machine, and I went into the containter.
docker run -it -v /var/run/docker.sock:/var/run/docker.sock -w /root node bash
When I write in the terminal docker I get an error:
bash: docker: command not found.
It happens precisely in the specific image of NodeJS, if for example I run such a test
docker run -v /var/run/docker.sock:/var/run/docker.sock \
-ti docker
It works great.
Why can't I run docker in the node image?
This not work because to mount sockets nodejs container must include a docker instance inside it.
Just try another general image other than docker. It also will not work. Search for nodejs images it self include docker. Use that then it will work.
If such image not exist you have to create new image from both docker and nodejs images and add command to start it.

How to know which node:alpine image version is in running container

I have one running container which uses node:alpine as base image.I want to know version of this image.
You can check the Dockerfile of the container if handy.
You can check the first line with FROM node:<version>-alpine
For example :
FROM node:12.18.1-alpine
ENV NODE_ENV=production
WORKDIR /app
You can also use the command docker image inspect
https://docs.docker.com/engine/reference/commandline/image_inspect/
You can also exec into the container to check the version of Node
Command to check node version:
docker exec -ti <Container name> sh -c "node --version"
Try running the command FROM node:<version>-alpine
You can also try running the command alpine -v or alpine -version .You can also start Alpine and press? on the main menu to open the main Help page, which will also tell you the version.
Refer to the link for more information.

setting up a docker container with a waiting bash to install npm modules

I'm trying to do something pretty trivial. For my dev environment, I wish to be able to have a shell in my container so I can run commands like npm install or npm run xxx.
(I do not want to install my npm modules during build, since I want to map them to the host so that my editor is able to find them on the host. I do not want to execute npm install on the host, since I don't want the host to have to install npm).
So even though in a production container I would instruct my container to just run node, in my developer container I want to have an always waiting bash.
If I set entrypoint to /bin/bash, the container immediately exits. This means that I can't attach to it anymore (since it stopped) and starting it will just immediately exit it again.
I tried writing a small .sh to just loop and start /bin/bash again, but using that in my ENTRYPOINT yields an error that it can't find the .sh file, even though I know it is in the container.
Any ideas?
You can use docker exec to run commands in a given container.
# Open an interactive bash shell in my_container
docker exec -it my_container bash
Alternatively, you can use docker run to create a new container to run a given command.
# Create a container with an interactive bash shell
# Delete the container after exiting
docker run -it --rm my_image bash
Also, from the question I get the sense you are still in the process of figuring out how Docker works and how to use it. I recommend using the info from this question to determine why your container is exiting when you set the entrypoint to /bin/bash. Finding out why it's not behaving as you expect will help you to understand Docker better.
I'm not sure what command you are trying to run, but here's my guess:
Bash requires a tty, so if you try to run it in the background without allocating one for it to attach to, it will kill it self.
If you're wanting to run bash in the background, make sure to allocate a tty for it to wait on.
As an example, docker run -d -it ubuntu will start a bash terminal in the background that you can docker attach to in the future.

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...
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

Create docker container with Node.js/NPM preinstalled but no package.json

I am looking for a Docker image that is just some *nix flavor with NPM and Node.js installed.
This image
https://hub.docker.com/_/node/
requires that a package.json file is present, and the Docker build uses COPY to copy the package.json file over, and it also looks for a Node.js script to start when the build is run.
...I just need a container to run a shell script using this technique:
docker exec mycontainer /path/to/test.sh
Which I discovered via:
Running a script inside a docker container using shell script
I don't need a package.json file or a Node.js start script, all I want is
a container image
Node.js and NPM installed
Does anyone know if there is an a Docker image for Node.js / NPM that does not require a package.json file? Perhaps I should just use a plain old container image and just add the code to install Node myself?
Alright, I tried to make this a simple question, unfortunately nobody could provide a simple answer...until now!
Instead of using this base image:
FROM node:5-onbuild
We use this instead:
FROM node:5
I read about onbuild and could not figure out what it's about, but it adds more than I needed for my use case.
the below code is in our Dockerfile
# 1. start with this image as a base
FROM node:5
# 2. copy the script from real-life into the container (magic)
COPY script.sh /usr/src/app/
# 3. define container entry point which will run our script
ENTRYPOINT ["/bin/bash", "/usr/src/app/script.sh"]
you build the docker image like so:
docker build -t foo .
then you run the image like so, which will "run the entrypoint":
docker run -it --rm foo
The container stdout should stream to the terminal where you ran docker run which is good (am I asking too much?).

Resources