Skip to main content

Images

An image file contains application binaries, and dependencies. It will also have meta data about how to execute the image.

defination

An Image is an ordered collection of root filesystem changes and the corresponding execution parameters for use within a container runtime. It’s a complete OS, it might be as small as a single file or a full Os like Ubuntu.

There is a Linux distro called alpine which is very small (less than 5MB) in size as docker image. It can be installed and run through the command docker with docker container run -it alpine sh. (bash is not available buy default in this so have to connect sh)

A docker image will allow you to take one of the OS image as base image and add the required softwares on top of it and copy files that are related to you application and package all of them together as image.

The storage for docker images officially supported, it is called docker hub. You can find all most all softwares related docker images in docker hub, the url for it is https://hub.docker.com/. It used to be free for everyone in the beginning, but now it is free only for personal use. You can find pricing information in pricing section.

Official images

Anybody can create docker image, and give any available unique name to it, this may give spammers opportunity to create images with malwares and publish in docker hub. To avoid this docker hub cameup with tag called offical, if a docker image is tagged as official it is created by the owner of software.

For example I searched for node.js image with word node and found list of images here https://hub.docker.com/search?q=node The results shown as bellow docker image search node.js

Below are the explanation for the flags added in image

  1. This result shows that this is an official node.js image from node itself
  2. This is also an official image but it also include mongo and express, and it is not actually from node (the logo says it is from mongoose)
  3. There would be also images sponsored by dockers
  4. The list of trusted image types

All official images in general with single word, just releasing an image from a popular organization will not make it automatically official image, it should comply with the terms and conditions of docker and the organization should register the official image.

The official docker images will generally named with single word, like mysql, node, nginx etc. other images will have structure of organization-name/docker-image-name. Ex: my_company/mysql_server

version and tag

By default every image version will tagged with different version names. And all latest versions of images are generally tagged with latest. Technically it is passible to tag any image version with tag "latest" but at least in case of official or popular or trusted images we can trust this information. Every image can have multiple tags but with same image id ex: 1.10.5, 1.10, latest, mainline, 1 etc. when we pull same image with different tag the docker will recognize it and just pull it from cache. The docker will show multiple images in list with different tag names Every image will have multiple layers while building it (Every new commit / push will act as a new layer) it will have its own SHA, and docker will compare and pulls only missing layers if image already exist. Observe bellow image, pull the nginx with version mainline, and check existing images in the host machine. There are two images with exactly same image ids, but different tags. docker images with multiple tags

Dockerfile

To create own docker image, create a docker configuration file with name Dockerfile (D capital) and add required configurations into it. In general we choose base image and more steps to cusotomize it, and create our own version out of it. For example you can start a OS docker image as base and add required tools/softwares in it through multiple bash and docker commands.

Create docker image

Lets create a docker image for node.js, add a simple script that will start node.js Webserver on a particular port and when you open the url in browser will print a simple text. Lets divide the process into 3 simple steps.

Step - I

We create required files in this step, in your computer create an empty folder and inside the folder place below two files in it. The first one is a server.js file with some node.js code (code already given below), and the second one is Dockerfile with the required docker configuration (check below for the code).

The node.js webserver code is borrowed from official node.js website at https://nodejs.org/en/docs/guides/getting-started-guide/, did couple of changes to make it simple and compliant with dockers. As this is node.js stuff and I don't want to deviate the topic so may be you just copy paste the code.

server.js

// --> name the file as: server.js

const http = require('http');

const PORT = 3000;

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.end('<H2>Hello World</H2>');
});

server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}/`);
});

Then below file.

# --> name the file as: Dockerfile

FROM alpine:3.16
RUN apk add nodejs npm
WORKDIR /src
COPY ./server.js .
EXPOSE 3000
CMD node server.js

Step - II

In this step we execute the command that will generate docker image out of the given files, execute below given command to generate the docker image.

docker build -t hello-world-server . 
# note there is a . at end of line

This command will create a new docker image every line in your Dockerfile represents a separate step, and once everything go well you could see the create image with name hello-world-server by executing the command docker image ls

Step - III

In this step we run create and run the container from the docker image we created. Run below command and then open the link http://localhost:3333 to see the result

docker run -p 3333:3000 -it --name my-server hello-world-server 
Note

You may observed the message in your terminal / command prompt Server running at http://localhost:3000/, but please note that the server is running on port 3000 inside the docker container, and published to host machine with port 3333. So you can access it in your machine with http://localhost:3333 only.

We successfully created our own docker image and created and run the container out of it, there is no limitation on what you want to have in your docker image, you can for example create a docker image out of any linux distribution and install java, mysql, angular and copy your code and run it. Though it is not a recommended practice but nothing will stop you to experiment. All you have to know is what you have to write in Dockerfile for different activities and understand the docker build command.

docker build

As you already learned above, this command is used to build the docker image, and the command always need the required Dockerfile configuration, and the the target folder.

docker image build -t <tag name|image name> <folder>
docker image build -t hello-world-server . # the folder from where the command executed should have Dockerfile

docker image build -t -f SomeDockerfile hello-world-server . #In this command instaed of using default Dockerfile, we are considering different file with name SomeDockerfile

docker image history

docker image inspect

docker image tag

docker image prune

docker image rm