Authorize docker-machine to access AWS ECR

I’m running gitlab-runner with docker-machine using amazonec2 driver with autoscaling. Everything works fine, except giving the “new” machines access to the container registry in AWS (the ECR). To give that access I need to run a script like followed:

aws configure set region us-west-2
aws configure set aws_access_key_id $AWS_REGISTRY_ID
aws configure set aws_secret_access_key $AWS_REGISTRY_SECRET
aws ecr get-login-password | docker login --username AWS --password-stdin $ECR_URI_BASE

For this script to run, both AWS CLI and Docker need to be installed on the docker-machines which are booting up. I am trying anything and can’t make this work.

Is there a method to run a pre-script that can run on a docker-machine, after installing docker, and right before running the actual CI/CD job?

Solutions tried

I created an AMI machine image on AWS with AWS CLI already installed, and the docker authorization is setup as a startup script to run. This solution should have worked, except that when I pre-install docker on the machine, then, docker-machine fails to connect to the installed docker and the following CI/CD job never starts.
Note: If I remove the docker installation, the CI/CD job starts, docker-machine installs docker on the machine and starts the CI/CD job. Then, it falis when trying to access the ECR.