user8437892
user8437892

Reputation: 21

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?

Upvotes: 2

Views: 2104

Answers (2)

mhb
mhb

Reputation: 773

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.

Upvotes: 0

augurar
augurar

Reputation: 13046

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.

Upvotes: 1

Related Questions