Skip to content

Instantly share code, notes, and snippets.

@wwerner
Last active December 22, 2023 10:48
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save wwerner/05a8e627e8f3ba18300db745511d3bcb to your computer and use it in GitHub Desktop.
Save wwerner/05a8e627e8f3ba18300db745511d3bcb to your computer and use it in GitHub Desktop.
Enable heroku ps:exec in docker containers
FROM openjdk:8-jdk-alpine
VOLUME /tmp
# see https://devcenter.heroku.com/articles/exec#enabling-docker-support
RUN apk add --no-cache curl bash openssh python
ADD src/main/docker/heroku-exec.sh /app/.profile.d/heroku-exec.sh
RUN chmod a+x /app/.profile.d/heroku-exec.sh
ADD src/main/docker/sh-wrapper.sh /bin/sh-wrapper.sh
RUN chmod a+x /bin/sh-wrapper.sh
RUN rm /bin/sh && ln -s /bin/sh-wrapper.sh /bin/sh
#!/usr/bin/env bash
# see https://devcenter.heroku.com/articles/exec#enabling-docker-support
[ -z "$SSH_CLIENT" ] && source <(curl --fail --retry 3 -sSL "$HEROKU_EXEC_URL")
#! /bin/bash
/bin/bash "$@"
@wwerner
Copy link
Author

wwerner commented Mar 3, 2018

sh-wrapper.sh could be replaced by setting set +o posix in heroku-exec.sh.

@nicholasjhenry
Copy link

Thank you for this!

@MrFishFinger
Copy link

MrFishFinger commented Oct 11, 2020

i found i had to add a python symlink to my dockerfile, to get this working with alpine in October 2020:

ln -s /usr/bin/python3 /usr/bin/python

here is a complete alpine dockerfile (for a basic nginx app), that works with heroku-exec:

FROM alpine:latest

# install required packages
RUN apk add --no-cache --update python3 bash curl openssh nginx

# simplfy nginx config to enable ENV variable substitution
RUN echo 'server { listen PORT_NUMBER; }' > /etc/nginx/conf.d/default.conf \
 && mkdir /run/nginx

# add config required for HEROKU_EXEC
# ENV HEROKU_EXEC_DEBUG=1
RUN rm /bin/sh \
 && ln -s /bin/bash /bin/sh \
 && mkdir -p /app/.profile.d/ \
 && printf '#!/usr/bin/env bash\n\nset +o posix\n\n[ -z "$SSH_CLIENT" ] && source <(curl --fail --retry 7 -sSL "$HEROKU_EXEC_URL")\n' > /app/.profile.d/heroku-exec.sh \
 && chmod +x /app/.profile.d/heroku-exec.sh \
 && ln -s /usr/bin/python3 /usr/bin/python

# configure NGINX to listen on dynamic $PORT env variable supplied by Heroku.
CMD sed -i 's/PORT_NUMBER/'"$PORT"'/g' /etc/nginx/conf.d/default.conf; nginx -g 'daemon off;'

@wizonesolutions
Copy link

I still can't seem to get this working. I've tried their longer, non-Ubuntu script as well, I have heroku-exec.sh in both possible locations, Python symlinked, set +o posix. I may have to try sh-wrapper.sh yet.

@jimkring
Copy link

jimkring commented Mar 2, 2022

Thanks everyone! @MrFishFinger's solution for alpine worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment