<p>I would like to run a shell script every time when a shell is started using <code>docker exec</code> or <code>kubectl exec</code>. I am looking for something similar to <code>.bashrc</code>/<code>.bash_profile</code>/<code>.profile</code>, but since this particular container is based on alpine linux, bash is not available.</p>
<p>How could I execute a shell script in the container?</p>
<p>I would like to achieve something like this:</p>
<pre class="lang-bash prettyprint-override"><code>&gt; docker exec -it my_alpine_based_container sh
Hi! Welcome to the container. This message comes from a shell script.
/usr/src/app $
</code></pre>
<p>And similarly with <code>kubectl</code>:</p>
<pre class="lang-bash prettyprint-override"><code>&gt; kubectl exec -it my_pod_running_the_container -- sh
Hi! Welcome to the container. This message comes from a shell script.
/usr/src/app $
</code></pre>

Attila
<p>When using the busybox ash variant, which is used by Alpine Linux as well, it is possible to set the ENV variable in the Dockerfile to point to a script that is executed on startup - even for not login shells.</p>
<p>For details see the busybox ash source code: <a href="https://git.busybox.net/busybox/tree/shell/ash.c?h=1_29_3#n14189" rel="nofollow noreferrer">ash.c, line 14189</a>.</p>
<p>Dockerfile:</p>
<pre><code>FROM alpine:latest
ENV ENV=/root/.ashrc
RUN echo &quot;echo 'Hello from .ashrc!'&quot; &gt;&gt; /root/.ashrc
CMD [&quot;/bin/sh&quot;]
</code></pre>
<p>Test:</p>
<pre class="lang-bash prettyprint-override"><code> $ docker exec -it c8688d3e9ce8 sh
Hello from .ashrc!
/ #
</code></pre>
