dockersshamazon-lightsailgitlab-ci.yml

Docker Image Pull Succeeds in GitLab Pipeline but Image Not Found on AWS Lightsail


I’m running a GitLab CI/CD pipeline to deploy a Docker image from GitLab’s Container Registry to an AWS Lightsail instance. The pipeline logs show the image is pulled successfully and the Docker commands run without errors. However, when I log into the Lightsail instance, the pulled image is not found.

Here’s the relevant part of my GitLab CD job:

ssh -o StrictHostKeyChecking=no user@<Lightsail-IP> << "EOF" 
echo "$DOCKER_ACCESS_KEY" | docker login -u "$DOCKER_USERNAME" --password-stdin https://registry.gitlab.com/ &&
docker pull registry.gitlab.com/repo/backend-app:develop &&
docker tag registry.gitlab.com/repo/backend-app:develop backend-app:develop &&
docker run --name backend-app -p 80:8080 -d registry.gitlab.com/repo/backend-app:develop
EOF

The pipeline logs show the image is pulled successfully. Docker tags the image without errors. No containers are running, and the image doesn’t seem to be available in Lightsail when I check using docker images and docker ps -a inside the instance.

Here’s a snippet of the pipeline log showing the image pull process:

a2318d6c47ec: Pull complete
99a31bbbcd61: Pull complete
24da4c394c8b: Pull complete
fe7649cc1e2b: Pull complete
b16c49eb2a3e: Pull complete
f69474871ffa: Pull complete
18447de3a64d: Pull complete
c996151d738b: Pull complete
Digest: sha256:83d9c29bd5546a099153a81d52f7051b14b0db2792ad7908ed70b8be3971e142
Status: Downloaded newer image for registry.gitlab.com/repo/backend-app:develop
registry.gitlab.com/repo/backend-app:develop
b93fac7f2b1576d4885debd7bb9d9e779ae4731cd07fe254e32e183436a0a012

Logs indicate that the image is successfully pulled and tagged, but once I SSH into the Lightsail instance, the image doesn’t show up when running:

docker images
docker ps -a

Has anyone encountered this issue or have any idea why the image seems to disappear on the Lightsail instance?

Any help would be appreciated!


Solution

  • I found the problem.

    Seems like when running multiple commands over SSH can lead to issues with session state variable scope or command dependencies.

    To resolve this, you can run each command in a separate SSH session like that:

    ssh -o StrictHostKeyChecking=no user@<Lightsail-IP> "echo \"$DOCKER_ACCESS_KEY\" | docker login -u \"$DOCKER_USERNAME\" --password-stdin https://registry.gitlab.com/"
    ssh -o StrictHostKeyChecking=no user@<Lightsail-IP> "docker pull registry.gitlab.com/repo/backend-app:develop"
    ssh -o StrictHostKeyChecking=no user@<Lightsail-IP> "docker tag registry.gitlab.com/repo/backend-app:develop backend-app:develop"
    ssh -o StrictHostKeyChecking=no user@<Lightsail-IP> "docker run --name backend-app -p 80:8080 -d registry.gitlab.com/repo/backend-app:develop"