Search code examples
amazon-web-servicesseleniumdockerdockerfileamazon-ecs

Dynamically set Environment Variable for Selenium Chrome Hub in ECS Docker


I have a cluster in AWS-ECS where I have custom image in ECR pulled which uses selenium/node-chrome from docker hub as a base image. This is the service I want to use as selenium node. I am passing 3 environment variable in it where one of the variable is REMOTE_HOST. I want to set it dynamically while docker image runs in ECS.

Following is the things I tried.

DockerFile

FROM selenium/node-chrome:3.10.0 COPY init.sh /etc/profile.d/ RUN sudo chmod +x /etc/profile.d/init.sh

init.sh

export EC2_HOST=$(wget -O - http://169.254.169.254/latest/meta-data/local-ipv4 2> /dev/null)
export REMOTE_HOST="http://$EC2_HOST:5555"

I was expecting that while docker is initiated, REMOTE_HOST would be available for the base docker image. But I do not see the environment variable being taken by the image nor I can see the variable value while I echo after connecting to the container (docker exec -it <Container Id> bash) in the EC2 instance.

Can anybody help me how do I set the environment variable dynamically?

I have also tried to use entrypoint as follow with no luck

FROM selenium/node-chrome:3.10.0 COPY init.sh /etc/profile.d/ RUN sudo chmod +x /etc/profile.d/init.sh ENTRYPOINT ["init.sh"]


Solution

  • I can't speak to why /etc/profile.d/init.sh does not get executed on container spinup, maybe that depends on what the linux base image is for selenium/node-chrome.

    However, ENTRYPOINT ["init.sh"] will definitely not work because you're replacing the entry point script that selenium/node-chrome sets up. Instead you should try to add to it. I would edit your dockerfile to be:

    FROM selenium/node-chrome:3.10.0
    COPY init.sh /etc/profile.d/
    RUN sudo chmod +x /etc/profile.d/init.sh
    USER root
    RUN sed -i '1 asource /etc/profile.d/init.sh' /opt/bin/entry_point.sh
    USER seluser
    

    The sed line adds the init.sh line to existing entry_point.sh on the second line, now when the container starts, you will see:

    REMOTE_HOST variable is set, appending -remoteHost
    

    Note though that when you start a new shell with docker exec -it <id> /bin/bash it doesn't necessarily inherit the same environment as the shell that selenium process is running in, echo may not work.