Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 24, 2021 12:20 pm GMT

Image Creation, Management, and Registry(Part 2)

Tagging Docker Images

Docker tags convey useful information about a specific image version/variant.

They are aliases to the ID of your image which often look like this: 8f5487c8b942

Assigning tag while building image

docker build -t demo:v1 .

tagwhilebuilding

Assigning tag if no tag default

Lets build a image without tag.

docker build .

imagewithouttag

Now lets assign tag to the existing image without tag.

docker tag adc07a47930e demo:v2

tagtonontagimage

tag for existing tag of the image

This will create another image with the same IMAGE ID.

docker tag demo:v2 demo2:v3

existingtagofimage

Docker Commit

Whenever you make changes inside the container, it can be useful to commit a containers file changes or settings into a new image.

By default, the container being committed and its processes will be paused while the image is committed.

Syntax:

docker container commit CONTAINER-ID myimage01

Created a container containing context01.txt file in root directory.
Then committed the container to the images and then creating another container from the same image, where we can see the same file/changes made present.

imagecomitting

We can also define the Commands while committing the images from the containers.

The --change option will apply Dockerfile instructions to the image that is created.

Supported Dockerfile instructions:

  • CMD | ENTRYPOINT | ENV | EXPOSE
  • LABEL | ONBUILD | USER | VOLUME | WORKDIR

command

docker container commit --change='CMD ["ash"]' modified-container

commandcommit

Docker Image Layers

A Docker image is built up from a series of layers.
Each layer represents an instruction in the images Dockerfile.

Here is the best resource I found for the presentation.

Docker Layer

As, the container have the R/W layer a top layer which is connected with the base image.
OR we can say that one base image is connected to different containers by Writable layer.

Lets, see some scenarios. Will build a docker image using dockerfile and will check the layers.

Dockerfile:

FROM ubuntuRUN dd if=/dev/zero of=/root/file1.txt bs=1M count=100RUN dd if=/dev/zero of=/root/file2.txt bs=1M count=100

Command to check the layers of the image, where layerdemo01 is the image name.

docker image history layerdemo1

Below Screenshot clearly shows how 2 layers are of same volume, as they are not adding the volume of previous layer. Every layer has its separate size according to the command or the changes.

layerdemo1

Now, lets try to remove these files and I found that 2 layers were created with 0B but the image still contains the same 282MB.

So, Basically here we have 5 layers.
Layer1 -> FROM Ubuntu
Layer2 -> RUN dd if=/dev/zero of=/root/file1.txt bs=1M count=100
Layer3 -> RUN dd if=/dev/zero of=/root/file2.txt bs=1M count=100
Layer4 -> RUN rm -f /root/file1.txt
Layer5 -> RUN rm -f /root/file2.txt

Here Only Layer4 and Layer5 is dealing with the removing of those files, but the file are still there in Layer3 and Layer2.
That is the reason image still have the same volume.

Dockerfile:

FROM ubuntuRUN dd if=/dev/zero of=/root/file1.txt bs=1M count=100RUN dd if=/dev/zero of=/root/file2.txt bs=1M count=100RUN rm -f /root/file1.txtRUN rm -f /root/file2.txt

layerdemo2

For the requirement where we need to remove the files after creation, we can use && to run both the command at one-go.
This will conserve the volume of the image

FROM ubuntuRUN dd if=/dev/zero of=/root/file1.txt bs=1M count=100 && rm -f /root/file1.txtRUN dd if=/dev/zero of=/root/file2.txt bs=1M count=100 && rm -f /root/file2.txt

Managing Images using CLI

So basically the best practice of using the command for images is

docker image <command>
  • docker pull ubuntu == docker image pull ubuntu
  • docker images == docker image ls
  • docker image build
  • docker image history
  • docker image import
  • docker image inspect
  • docker image load
  • docker image prune
  • docker image push

dockerImageCLI

Inspecting Docker Images

A Docker Image contains lots of information, some of these include:

  • Creation Date
  • Command
  • Environment Variables
  • Architecture
  • OS
  • Size

docker image inspect command allows us to see all the information associated with a docker image.

Suppose we need to get the particular field from the inspect data.
e.g Hostname

we can use

docker image inspect ubuntu | grep 'Hostname'docker image inspect ubuntu --format='{{.Id}}'

There are certain things that have parent child details. Like, ContainerConfig have Hostname, Domainname etc.
But, this will only gives the values not the key.

docker image inspect ubuntu --format='{{.ContainerConfig}}'

If you want the key and value both.

docker image inspect ubuntu --format='{{json .ContainerConfig}}'

If you just want the hostname value you can use below caommand to filter out the information from the inspect data.

docker image inspect ubuntu --format='{{.ContainerConfig.Hostname}}'

imageInspect

Docker Image prune

Docker image prune command allows us to clean up unused images.
By default, the below command will only clean up dangling images.

Dangling Images = Image without Tags and Image not referenced by any container

To prune all the images that has no container refrenced, we can use below commands.

docker image prune -a

If you want to remove all the images only which don't have tag associated, you can use below commands.

docker image prune

Before Prune we had these many images.

before prune

After running prune command.

after prune

Those images got removed which were not referenced to the container.

Here is the below command the image without the tag ( ) tag which is a Dangling image, but it can't be prune because it has containers associated.

imageprune

Flattening Docker Images

Modifying Image in a single Layer or specific Layer.

As we know ubuntu has many layers. So, to merge all layers to the single layer. There is one approach that to,
Import and Export to a container

Commands:

docker export myubuntu > myubuntudemo.tarcat myubuntudemo.tar | docker import - myubuntu:latest

layerscompress

Building Docker Registry

A Registry a stateless, highly scalable server-side application that stores and lets you distribute Docker images.

Docker Hub is the simplest example that all of us must have used.

There are various types of registry available, which includes:

Docker Registry
Docker Trusted Registry
Private Repository (AWS ECR)
Docker Hub

To push the image to a central registry like DockerHub, there are three steps:

  1. Authenticate your Docker client to the Docker Registry Refrence for setting up Docker Registry https://hub.docker.com/_/registry/
docker run -d -p 5000:5000 --restart always --name registry registry:2

setupdockerregistry

  1. Tag Docker Image with Registry Repository and optional image tag.
docker tag myubuntu:latest localhost:5000/myubuntu
  1. Push Image using docker push commandTo push the docker image to the AWS ECR.Refrence: https://docs.aws.amazon.com/AmazonECR/latest/userguide/docker-push-ecr-image.html
 docker push localhost:5000/myubuntu

dockerpush

Now, lets pull the image from the registry. For that first we need to untag the registy located image and then delete the image.

docker pull localhost:5000/myubuntu

dockerpull

Pushing Docker Image to Docker Hub

I have my account on Docker Hub and created had 1 repository.

dockerhubrepo

So, first we will login to the docker hub by CLI and then create the tag and push it. Then before pulling the image I have removed the tag to the image and removed all the containers associated to that container and then pull the image from the Docker Hub Repository.

docker logindocker tag busybox deepakporwal95/mydemo:v1docker push deepakporwal95/mydemo:v1docker pull deepakporwal95/mydemo:v1

Pushing out custom image to docker hub.

pushingtodockerhub

Pulling the image from Docker Hub.

PullingdockerImage

Searcing and Filtering Images from Docker Hub

DescriptionCommand
Search for Busybox imagedocker search busybox
Search for Busybox image with Max Result of 5docker search busybox --limit 5
Filter only official imagesdocker search --filter is-official=true nginx

On searching nginx images.

searchnginx

We will limit the number of results.

limit search

On searching images from Docker Hub we get many results. We can filter those results by three filter supporters.

  1. stars
  2. is-automated
  3. is-officialThis will bring to us specific required result only.

niginx official

Moving Images Across Hosts

Suppose we want to send docker image to other hosts or instances from admin box or master server. In this case we save the image as a zip and then transfer that image to host and at last will load that image in the host.

movingimageaccroos

Cache in Docker

While building a container or image it uses the cache of each layer which has been already been there.

Here is the Dockerfile and requirements.txt details that we will be using for this usecase.

Dockerfile

FROM python:3.7-slim-busterCOPY . .RUN pip install --quiet -r requirements.txtENTRYPOINT ["python", "server.py"]

requirements.txt

certifi==2018.8.24chardet==3.0.4Click==7.0cycler==0.10.0decorator==4.3.0defusedxml==0.5.0

Requirements

Here are the commands that will be using to build the images.

docker build -t without-cache .docker build -t with-cache .

without cache

withoutcache

with cache

with cache

References:
Official Docker
Udemy Course

Credit:
Zeal Vora

Prev: Image Creation, Management, and Registry(Part 1)


Original Link: https://dev.to/dporwal/image-creation-management-and-registry-part-2-514n

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To