Hello. In this tutorial, we will talk about Docker and a brief introduction to Docker images.
1. What is Docker?
In the present world, Docker is an important term –
- Often used in CI/CD platform that packages and runs the application with its dependencies inside a container
- Is a standard for Linux Containers
- A Container is a runtime that runs under any Linux kernel and provides a private machine-like space under Linux
1.1 What is Docker used for?
It is used for –
- For environment replication, while the code runs locally on the machine
- For numerous deployment phases i.e. Dev/Test/QA
- For version control and distributing the application’s OS within a team
1.2 Basic Docker terminology?
- Image: Representation of Docker container i.e. a JAR or WAR file in Java
- Container: Runtime of Docker i.e. a deployed and running Docker image. For example, an executable Spring Boot jar
- Engine: The code that manages, creates, and runs the Docker containers
- Hub: A public developers registry to distribute their code
- Repository: A collection of Docker related images i.e. different versions of the same application
1.3 Setting up Docker
If someone needs to go through the Docker installation, please watch this video.
2. Introduction to Docker Image
A docker image is a read-only template containing the list of instructions for creating a container that runs on the containerized environment such as Docker. It provides a convenient way to bundle the application and server environments that can be used privately or share it publically with other Docker users. Docker image acts as a reusable asset that can be deployed on any host.
2.1 Docker container vs Docker image
The below table shows a quick difference between the docker container and docker image –
|Docker image||Docker container|
|It is a container blueprint||It is an image instance|
|It is non-writable||It is writable|
|Can exist without a container||Must run an image to exist|
|Does not need any computing resource to operate||Need computing resources to run i.e. they run as virtual machines|
|Can be shared via public or private repositories via the Dockerhub platform||They are not shareable|
|Can be recreated multiple times based on the requirement||Multiple containers can be created from the same image|
2.2 Image repositories
Docker hub repositories allow us to share the container images with the team or customer or general public at large. An image repository offers –
- Collaboration – Companies can use the private repositories to share the prepared images with their team or public repositories to share images with a larger group or the external community
- Convenience – A repo often makes it easy for the developers to access their images or images created by the larger group
- Integration – Provides easy integration with other platforms like Docker and Kubernetes
- Security – Docker provides a scanning feature to scan the images against the vulnerabilities or malware that might be lying inside the image and could exploit your code
2.3 How to create a Docker image
For creating a docker image one needs to understand the concept of the Dockerfile file. A Dockerfile is a simple text file responsible for creating the Docker image wherein a docker image is a read-only file with a bunch of instructions that when executed results in the creation of a Docker container. A Dockerfile usually consists of the below keyword –
FROM– Tells us what base image to be used for creating the image
MAINTAINER– Specifies the author of the image
RUN– Builds the container
VOLUME– Allows to externally mount a volume via the host itself or a Docker data container
ENV– Sets the environment variables that are used in the Dockerfile and any script that it calls
COPY– Copy a file (in the same directory as Dockerfile) to the container
CMD– Specifies a default command to run within a container
ENTRYPOINT– Specifies a specific executable to run within a container
2.3.1 How to create a Dockerfile?
Three areas are kept in mind while creating a docker image –
- Making the consistent design of your images as they are easier to maintain and will reduce maintenance when developing new images
- Reducing the build time by integrating it with continuous integration
- Reducing the image size to improve the security, performance, efficiency, and maintainability of the container
- Securing the containers to protect the application from external threats or attacks
Below is an example of Dockerfile with the important commands. The same Dockerfile can be modified based on the business needs or the application code.
FROM ubuntu LABEL maintainer="email@example.com" ADD appsetup / RUN /bin/bash -c 'source $HOME/.bashrc; echo $HOME' CMD ["echo", "Hi everybody!"]
2.3.2 Building a docker image from Dockerfile
Dockerfile is used to create a docker image. Let us see how to build an image with Dockerfile. The basic syntax to build an image is –
docker build location_of_your_dockerfile
If you’re already in the directory where Dockerfile is located, put a dot instead of location i.e.
docker build .
You can also add the
-t flag to tag the new image with a name. Once the image is built successfully you can verify whether it is created or not with the help of the below command –
2.4 Docker image commands
Several commands are associated with the docker image and let us understand them one by one –
docker image build– Build an image from the Dockerfile
docker image inspect– Display information about the docker image
docker image prune– Remove all images from the host system
docker image pull– Pulls an image from the repository
docker image push– Pushes an image or a repository to a registry
docker image tag– Create a tag that refers to a source image
That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share! Happy learning and do not forget to share!
In this tutorial, we learned about Docker and different variations in the docker image command. You can also download the commands used in this tutorial from the Downloads section.
This was a tutorial on learning Docker and playing around with the docker image command.
You can download the full source code of this example here: Introduction to Docker Image