Shell into a Docker container running on a Heroku

2019-02-17 07:49发布

Given a dyno in which a container is running, what's the Heroku equivalent of docker exec -it blarg /bin/bash? That is, how can one open a shell into the already-running container?

Example Dockerfile:

FROM heroku/heroku:16
CMD while true; do sleep 1; done

Example run:

$ heroku container:push my_app
<wait a minute>
$ heroku ps
=== my_app (Free): /bin/sh -c while\ true\;\ do\ sleep\ 1\;\ done (1) 
my_app.1: up 2017/10/09 12:13:07 -0600 (~ 4m ago)

So far so good.

But now...

$ heroku ps:exec --dyno=my_app.1
Establishing credentials... error
 ▸    Could not connect to dyno!
 ▸    Check if the dyno is running with `heroku ps'

For good measure I check heroku ps at this point and it shows that the dyno is still running.

Yes, I've done all the things Heroku suggests to enable Docker support. Per the documentation, I have tried using a base image of my choice while ensuring that bash, curl, openssh, and python are present. I have also tried using the Heroku-16 base image, as shown in the above example.

(The linked documentation also references steps required for Private Spaces. Since I'm not using Private Spaces I have not applied those steps.)

标签: docker heroku
3条回答
2楼-- · 2019-02-17 08:26

If bash is installed, run heroku run bash. This will get you into the shell from your command line.

You can also use the GUI and go to "More" -> "Run Console" on your heroku app, and input "bash" to bring it up there.

查看更多
该账号已被封号
3楼-- · 2019-02-17 08:32

TL;DR Ensure that bash is installed in the image and add this to your Dockerfile:

RUN rm /bin/sh && ln -s /bin/bash /bin/sh

Explanation

Contrary to what the documentation would lead one to believe, Heroku does not, out of the box, support heroku ps:exec into a Docker container already running in a dyno.

Quoting from responses I have received from the Heroku team:

Our ps:exec feature ... works ... by injecting a bash file into dynos, opening an additional port in the background, and allowing you to connect to it.

[T]he default shell used by Docker is /bin/sh, which is not compatible with the Heroku Exec script (it's requires /bin/bash).

There is a workaround you can use though. Put the following in your Dockerfile:

RUN rm /bin/sh && ln -s /bin/bash /bin/sh

This is definitely a gap in our product, and we'll work to make this better.

查看更多
Root(大扎)
4楼-- · 2019-02-17 08:33

Edited: In order to run heroku ps:exec on apps with Docker and deployed via the Container Registry you have to enable runtime-heroku-exec. You can do heroku features:enable runtime-heroku-exec to enable it

Here you can see the documentation of exec with the instructions to enable docker support

查看更多
登录 后发表回答