Service docker:dind working in some jobs but not others

I have two pipeline job configurations using docker:dind service. I am receiving Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? in one but not the other.

Naturally I did a search but all results point to using service dind or a custom runner. I would like to use the shared runner as it does work correctly in other pipelines.

Can anyone advise on where my mistake is?

This config functions correctly building and pushing the image.

docker-build:
 stage: image-build
 services:
   - docker:dind
 variables:
     AWS_REGISTRY: $REGISTRY
     DOCKER_HOST: tcp://docker:2375
     DOCKER_TLS_CERTDIR: ""
     build_arg: ""
 script:
     - aws ecr --region us-east-1 get-login-password | docker login --username AWS --password-stdin $AWS_REGISTRY
     - aws s3 cp s3://ml-ops/cicd/stage/configs/npmrc .npmrc
     - npm install
     - npm run build
     - image_id=`docker build --no-cache -f Dockerfile --build-arg NPM_TOKEN=${NPM_TOKEN} $build_arg ./ |tail -n 1|awk '{print $3}'`
     - if [ $CI_COMMIT_SHORT_SHA > 7 ]; then image_tag=$( echo $CI_COMMIT_SHORT_SHA | cut -c-7 ); fi
     - docker tag $image_id $AWS_REGISTRY/$CI_PROJECT_NAME:$image_tag
     - branch_tag=`if [ $CI_COMMIT_REF_SLUG == 'master' ]; then echo "prod"; elif [ $CI_COMMIT_REF_SLUG == 'qa' ]; then echo "qa"; elif [ $CI_COMMIT_REF_SLUG == 'stage' ]; then echo "staging"; fi`
     - docker tag $image_id $AWS_REGISTRY/$CI_PROJECT_NAME:$branch_tag
     - AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY aws ecr --region us-east-1 get-login-password | docker login --username AWS --password-stdin $AWS_REGISTRY
     - docker push $AWS_REGISTRY/$CI_PROJECT_NAME:$image_tag
     - docker push $AWS_REGISTRY/$CI_PROJECT_NAME:$branch_tag
 rules:
   - if: $CI_COMMIT_BRANCH == 'stage' || $CI_COMMIT_BRANCH == 'qa' || $CI_COMMIT_BRANCH == 'master'

This config does displays the error ’ Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?’

.deploy_template: &deploy_config
    services:
        - docker:dind
    variables:
        AWS_ECR_REGISTRY: $REGISTRY
        BUILD_ARG: ""
        DEPLOY_SCRIPT_ENV: "production"
        DOCKER_HOST: tcp://docker:2375
        DOCKER_TLS_CERTDIR: ""
    script:
        - aws s3 cp --recursive s3://ml-ops/cicd/$DEPLOY_SCRIPT_ENV/scripts/ .
        - aws ecr --region us-east-1 get-login-password | docker login --username AWS --password-stdin $AWS_ECR_REGISTRY
        - image_id=`docker build --no-cache -f Dockerfile --build-arg NPM_TOKEN=${NPM_TOKEN} $build_arg ./ |tail -n 1|awk '{print $3}'`
        - if [ $CI_COMMIT_SHORT_SHA > 7 ]; then image_tag=$( echo $CI_COMMIT_SHORT_SHA | cut -c-7 ); fi
        - docker tag $image_id $AWS_REGISTRY/$CI_PROJECT_NAME:$TAG_NAME
        - docker push $AWS_REGISTRY/$CI_PROJECT_NAME:$TAG_NAME


Can you show how you are using the second approach in gitlab-ci.yml?