Commands needed when using docker pull

Hey all!
I’m trying to write a bash file that will build a docker and then install it on the ECR of Amazon.
What are the commands in the case of:

  1. First check whether there’s already a base image.
  2. Secondly, if there is, download it.
  3. Otherwise, install this new base image.

I was thinking of using only docker pull, which doesn’t reinstall the base image if it’s already the most updated to.

Thanks!

You mean you “build an image, and then you push it to ECR”.

There is no such thing as “a docker”, and images are not installed in a registry.

You do not install images. You can pull them from a remote registry into your local image cache. Also, when you start a container based on an image, you still don’t install anything.

If you pull an image using its tag, it will internally resolve to which image digest the tag currently points and will pull that image by its digest. It will not download existing image layers.

Why don’t you just use docker build --pull ..., which is explicitly designed to pull the most recent image for the specified base image tag before the image is built?

1 Like

You mean you “build an image, and then you push it to ECR”.

You’re right. Apologies, as I’m really learning this at the moment. Btw, many thanks for your quick response.

There is no such thing as “a docker”, and images are not installed in a registry.

Aren’t the images installed at the ECR? Otherwise, do you mean that the ECR only stores the specifications of the image, which would be later on installed on the local host?

Based on this:

You do not install images. You can pull them from a remote registry into your local image cache. Also, when you start a container based on an image, you still don’t install anything.

If you pull an image using its tag, it will internally resolve to which image digest the tag currently points and will pull that image by its digest. It will not download existing image layers.

When I start a container based on an image, isn’t it what is to follow in order to install it on the local image cache?

Why don’t you just use docker build --pull ... , which is explicitly designed to pull the most recent image for the specified base image tag before the image is built?

Great idea! I’ll look further into it.

Images are used for packaging. The only point in time where something is actually installed is during image built → as in “something is installed in the image you create”. An image can be pushed to a registry, to store it in the registry, and can be bulled from the registry to create a container based on the image.

Can you rephrase this part and add the exact commands you use to start a container based on an image, so I can be sure we are speaking about the same thing.

Can you rephrase this part and add the exact commands you use to start a container based on an image, so I can be sure we are speaking about the same thing.

Of course. I think that the first explanation pretty much made things a bit more clarified. In other words, is the “image” kind of a “bootable-usb” (in analogy, “image”) that tells the PC (“container”) what to install?
Could I say that the bootable-usb could first check the internet (in analogy, the “registry”), whether there is a new version of the software to be installed?

As per the code commands in the bash file:

  1. Setting the variables such as: repo_name, region, account-id, base_image, tag etc.
  2. docker_file=dockerfile
  3. sudo docker build – build-arg VERSION … --build-arg CONF_FILE= … --tag

And somewhere here I’d like to check first whether a more recent base-image is available in the registry.

LATEST_IMAGE=$(aws ecr describe-images --repository-name $REPO_NAME --region $REGION --query ‘sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]’ --output text)

if [[ “$LATEST_IMAGE” == “$DOCKER_TAG” ]]; then
echo “The latest Docker image ($LATEST_IMAGE) is already in the ECR repository. No need to build a new one.”
else
sudo build docker …

You are completely on the wrong track. A container does not boot, it just executes an isolated process on the host kernel with isolated storage and network (+other sorts of isolation).

Please google for the differences of containers and vms - there are hundreds of blog posts that discuss the differences.

Furthermore, I can highly recommend this free self-paced training: Introduction to Containers. It should provide a solid foundation about Docker concepts and how things are done in Docker. Working with Docker without understanding the concepts can be very time-consuming and frustrating.

Your command indicates it’s about building images. I already addressed the correct solution in the last paragraph of my first post.

To achieve your goal of building a Docker image, checking if a base image exists, and then pushing it to Amazon Elastic Container Registry (ECR), you can use a bash script. The script will use the docker pull command to check for the existence of the base image and download it if necessary. Then, it will build your new image and push it to ECR. Here’s a sample bash script to do this:

#!/bin/bash

# Replace these variables with your own values
DOCKER_USERNAME="your_docker_username"
DOCKER_REPO="your_docker_repository"
BASE_IMAGE="base_image_name:tag"
NEW_IMAGE="new_image_name:tag"
AWS_REGION="your_aws_region"

# Check if the base image exists
if docker image inspect "$BASE_IMAGE" &> /dev/null; then
  echo "Base image $BASE_IMAGE exists. Pulling it..."
  docker pull "$BASE_IMAGE"
else
  echo "Base image $BASE_IMAGE not found. Installing a new base image..."
  # Install your new base image here
  # Example: docker pull your_base_image:tag
  docker pull your_base_image:tag
fi

# Build your new Docker image (using the base image you just pulled or installed)
docker build -t "$NEW_IMAGE" .

# Login to ECR (ensure you have the AWS CLI configured and valid credentials)
aws ecr get-login-password --region "$AWS_REGION" | docker login --username AWS --password-stdin "$DOCKER_USERNAME.dkr.ecr.$AWS_REGION.amazonaws.com"

# Tag the new image for ECR
docker tag "$NEW_IMAGE" "$DOCKER_USERNAME.dkr.ecr.$AWS_REGION.amazonaws.com/$DOCKER_REPO:$NEW_IMAGE"

# Push the new image to ECR
docker push "$DOCKER_USERNAME.dkr.ecr.$AWS_REGION.amazonaws.com/$DOCKER_REPO:$NEW_IMAGE"

echo "Image $NEW_IMAGE pushed to ECR successfully!"

1 Like

Thanks for the explanation.

I did some homework and understood better. I think that my mistake originated from the wrong description of the task.

My task now is more clear this way:
There are two bash files: run.sh and build.sh.

run.sh is used by the customer.
build.sh is used by the developer.

The developer pushes new builds into ECR.
The customer pulls new builds from the ECR.

The linking word is “latest” on the ECR.

So what I’m trying to do now is as following:
Build.sh file would build the new image and push it.
Run.sh file would pull the latest image.

Seems like, according to this:

Why don’t you just use docker build --pull ... , which is explicitly designed to pull the most recent image for the specified base image tag before the image is built?

I should just use this “–pull” in addition in the run.sh file.

I thought that the customer’s run.sh would need to pull that “latest”, store it on “LATEST_IMAGE” and compare it with the current one he has. Then add a condition that if he already has the same condition, it wouldn’t pull the image. But now it seems like it could be better for him to “–pull”, so it will anyway pull the latest image whatsoever.

Is there a wiser approach to it?

Almost. The argument --pull is for docker build, when creating the container with docker run, you will need to use --pull=always.

See:
https://docs.docker.com/engine/reference/commandline/run/#pull

2 Likes