To use an environment variable in the ENTRYPOINT
array of a Docker container, you need to use the JSON array format for the ENTRYPOINT
and CMD
instructions. Here’s how you can achieve this:
- Define the environment variable in your Dockerfile:
dockerfileENV ADDRESSEE=world
- Use the environment variable in your
ENTRYPOINT
array:
dockerfileENTRYPOINT ["./greeting", "--message", "Hello, ${ADDRESSEE}!"]
By using ${ADDRESSEE}
instead of $ADDRESSEE
, Docker will interpret the environment variable correctly. When you run a container from this image, the ADDRESSEE
environment variable will be replaced with its value, resulting in the correct message being passed to your greeting
script.
Here’s the complete Dockerfile:
dockerfileFROM your-base-image ENV ADDRESSEE=world COPY greeting /usr/local/bin/greeting RUN chmod +x /usr/local/bin/greeting ENTRYPOINT ["./greeting", "--message", "Hello, ${ADDRESSEE}!"]
Make sure to replace your-base-image
with the actual base image you are using for your Docker container, and adjust the COPY
and RUN
commands as needed for your greeting
script.
المزيد من المعلومات
في Docker، يمكنك استخدام المتغيرات البيئية في تعليمات ENTRYPOINT
و CMD
باستخدام تنسيق الصفيف JSON. هذا يسمح لك بتعيين قيم ديناميكية للتعليمات البرمجية التي تشغل الحاوية.
عند تعيين المتغير البيئي في تعليمات ENTRYPOINT
و CMD
، استخدم ${VAR}
بدلاً من $VAR
لضمان تفسير المتغير البيئي بشكل صحيح.
يمكنك أيضًا تعيين قيم افتراضية للمتغيرات البيئية في Dockerfile باستخدام التعليمة ENV
، كما في المثال التالي:
dockerfileFROM your-base-image # تعيين قيمة افتراضية للمتغير البيئي ENV ADDRESSEE=world # تعريف تعليمات الدخول ENTRYPOINT ["./greeting", "--message", "Hello, ${ADDRESSEE}!"]
هذا النهج يجعل تطبيقات Docker الخاصة بك أكثر قابلية للتكوين والتشغيل في بيئات مختلفة دون الحاجة إلى تعديل الكود أو إعادة بناء الصورة.