Sunday, 26 May 2019

Docker: Working with tags


Docker tags are used to differentiate the image with different versions. You will get a better idea with an example.

Let me pull the image ‘hello-world’ from docker hub without specifying the tag (Execute the command ‘docker pull hello-world’).

Since I do not specify the tag, while pulling the image, it downloads the latest one (image with the latest tag).
$docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:92695bc579f31df7a63da6922075d0666e565ceccad16b59c3374d2cf4e8e50e
Status: Downloaded newer image for hello-world:latest


Let’s print all the images and see how it looks like. As you see image ‘hello-world’ with tag ‘latest’ is downloaded.

$docker image ls -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        3 months ago        1.84kB

Download image with specific tag
You can go to below location and see all the tags for the image hello-world.
https://hub.docker.com/_/hello-world?tab=tags

Use below syntax to pull the image with specific tag.
docker image pull ImageName:Tag

Example

docker image pull hello-world:linux
$docker image pull hello-world:linux
linux: Pulling from library/hello-world
Digest: sha256:1a67c1115b199aa9d964d5da5646917cbac2d5450c71a1deed7b1bfb79c2c82d
Status: Downloaded newer image for hello-world:linux


Let’s print all the images again.
$docker image ls -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        3 months ago        1.84kB
hello-world         linux               fce289e99eb9        3 months ago        1.84kB

Now you can see, there are two ‘hello-world’ images with tags ‘latest’ and ‘linux’.

How to run the image with tag?
Syntax 1
docker run imagename

If you run the image without specifying tag, then latest image will run by default.

Syntax 2
docker run imagename:tag
If you want to run the image with specific tag, use this syntax.

Example
docker run hello-world
docker run hello-world:linux

Building an image without tag
If you build an image without tag, then it is always build with ‘latest’ tag.

Step 1: Create a directory 'tagsDemo'.

Step 2: Navigate to 'tagsDemo' and create a 'Dockerfile'

Dockerfile
FROM ubuntu

CMD echo "Image without tag"


Build the image by executing the command 'docker image build .'.
$docker image build .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM ubuntu
latest: Pulling from library/ubuntu
898c46f3b1a1: Already exists 
63366dfa0a50: Already exists 
041d4cd74a92: Already exists 
6e1bee0f8701: Already exists 
Digest: sha256:017eef0b616011647b269b5c65826e2e2ebddbe5d1f8c1e56b3599fb14fabec8
Status: Downloaded newer image for ubuntu:latest
 ---> 94e814e2efa8
Step 2/2 : CMD echo "Image without tag"
 ---> Running in bbaf5b5f2a7a
Removing intermediate container bbaf5b5f2a7a
 ---> a1868b148d8d
Successfully built a1868b148d8d


Execute the command 'docker image ls', to see list of all the available repositories.
$docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
<none>              <none>              a1868b148d8d        About a minute ago   88.9MB
ubuntu              latest              94e814e2efa8        6 weeks ago          88.9MB

Surprisingly REPOSITORY and TAG has value <none>, it is because we are not mentioned any name to the repository.

Let me rebuild the image by specifying image name.
docker image build -t tagsapp .


I specified image name as ‘tagsapp’.
$docker image build -t tagsapp .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM ubuntu
 ---> 94e814e2efa8
Step 2/2 : CMD echo "Image without tag"
 ---> Using cache
 ---> a1868b148d8d
Successfully built a1868b148d8d
Successfully tagged tagsapp:latest

$docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tagsapp             latest              a1868b148d8d        4 minutes ago       88.9MB
ubuntu              latest              94e814e2efa8        6 weeks ago         88.9MB

You can see that there is an image with name tagsapp and tag latest (Since I do not specified any tag, it takes latest by default).


Run the image tagsapp.
$docker run tagsapp
Image without tag

Building an image with tags
You can tag the image while building itself. Let me explain with an example.

Update Dockerfile like below.


Dockerfile
FROM ubuntu

CMD echo "Application version 1"


Execute the command ‘docker image build -t tagsapp:1 .’
$docker image build -t tagsapp:1 .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM ubuntu
 ---> 94e814e2efa8
Step 2/2 : CMD echo "Application version 1"
 ---> Running in 359a5675daea
Removing intermediate container 359a5675daea
 ---> 17ff919cfdbd
Successfully built 17ff919cfdbd
Successfully tagged tagsapp:1


Execute the command ‘docker image ls’ to see all the available images.
$docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tagsapp             1                   17ff919cfdbd        46 seconds ago      88.9MB
tagsapp             latest              a1868b148d8d        9 minutes ago       88.9MB

How to run the image with specific tag?
Syntax

docker run imageName:tagName

$docker run tagsapp:1
Application version 1
$
$docker run tagsapp:latest
Image without tag
$
$docker run tagsapp
Image without tag


Previous                                                 Next                                                 Home

No comments:

Post a Comment