2

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?

2 Answers2

1

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.

augurar
  • 12,081
  • 6
  • 50
  • 65
  • indeed - I am trying to figure out how best to use docker in a dev workflow :)run will create a new container, which is not what I want. Exec requires a started container, and Start allows me to start a container, but unless it is set to have a waiting process (like nodemon), it will exit. For dev-purposes, I would like to configure a container (I use compose) to just start up in bash directly. So in the exec example, yes, I could have a container start with nodemon (for instance) and then use exec to get into it, but I would rather have it start at bash. – user8437892 Aug 10 '17 at 05:34
  • @user8437892 I don't think what you're trying to do makes sense. If you aren't running a daemon process, why do you need a long-running container? Just use `docker run` when you need to run a command and use volumes for persistence if desired. – augurar Aug 10 '17 at 18:09
  • I see, I think the confusing part for me is that run starts a new container each time. Maybe a persistent volume is the key to what I want. – user8437892 Aug 11 '17 at 06:35
0

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.

mhb
  • 754
  • 8
  • 20
  • that makes sense. I'm trying to use docker-compose up though, so it is not very practical to use docker run manually on it. – user8437892 Aug 10 '17 at 05:40
  • I do not understand what you are doing. Any more details? – mhb Aug 10 '17 at 09:35
  • I want to configure the container to start in bash and remain there (waiting to be attached to), without having to do a run, which would start a new container. The bash is there for dev purposes (run npm install, or npm run start etc). I can call exec -it /bin/bash on the container, but it will have to be started, and so i'm looking for a way to configure the container to not exit immediately. Normally you would have node running, which would take care of that, but this is a dev container and that not practical. – user8437892 Aug 11 '17 at 06:39
  • Why does what I proposed not work? You can do a `docker create` and then separately a `docker start`, but all a `docker run` is, is `create + start`. – mhb Aug 11 '17 at 21:37