Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Access env variables inside docker entrypoint script

I want to pass environment variables in my docker run command and access them in my entrypoint shell script.

This is my super simple Dockerfile for test purposes:

FROM ubuntu:20.04

WORKDIR /

ADD entrypoint.sh .

RUN chmod 755 entrypoint.sh

ENTRYPOINT [ "/entrypoint.sh" ]

And this is the entrypoint.sh:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#!/bin/sh

printf "env var TEST = ${TEST} "

I just build the Dockerfile like this: docker build -t test:1.0 .

And then run it like this: docker run -i test:1.0 -e TEST='Hello world'

Unfortunately the output is not containing the env var.

>Solution :

Something like this will work:

entrypoint.sh

#!/bin/sh
echo $MYVAR

Dockerfile

FROM alpine:3.15.4
WORKDIR /home/app
COPY ./entrypoint.sh .
ENV MYVAR=""
ENTRYPOINT ["/bin/sh", "/home/app/entrypoint.sh"]

And then you can build & run the container while setting the environment variable with:

$ docker build -t test . && docker run -e MYVAR="hello world" test

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading