البرمجة

استخدام المتغيرات البيئية في Docker ENTRYPOINT

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:

  1. Define the environment variable in your Dockerfile:
dockerfile
ENV ADDRESSEE=world
  1. Use the environment variable in your ENTRYPOINT array:
dockerfile
ENTRYPOINT ["./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:

dockerfile
FROM 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، كما في المثال التالي:

dockerfile
FROM your-base-image # تعيين قيمة افتراضية للمتغير البيئي ENV ADDRESSEE=world # تعريف تعليمات الدخول ENTRYPOINT ["./greeting", "--message", "Hello, ${ADDRESSEE}!"]

هذا النهج يجعل تطبيقات Docker الخاصة بك أكثر قابلية للتكوين والتشغيل في بيئات مختلفة دون الحاجة إلى تعديل الكود أو إعادة بناء الصورة.

شاهد أيضاً
إغلاق
زر الذهاب إلى الأعلى