In the 3rd edition of our series, Docker for Noobs, we will explore about Docker Images and Containers.
Docker for Noobs Series:
Part 2 – Docker Editions and Installation
Part 3 – Images and Containers
Part 4- Working with Images and Containers
What is a Docker Image?
Docker Image refers to a template structure that consists of binaries and libraries. A Docker Image is made up of several layers. These images are available in public or private repositories that can be re-used by others to serves as the basis for their development work.
An Image can be as simple as a Hello World text output or a full-fledged OS with Web Server within it. An Image in itself is a read only file that is immutable.
Images are BUILD using a Dockerfile which consists of a set of commands where each command creates a layer in the image.
What is a Docker Container?
Docker Containers are derived from a Docker Image. As Images are immutable, Containers are mutable and are something which is actually running the system here. A Docker Container in its running form is somewhat similar to Virtual Machines.
Containers are formed when they are RUN from an Image.
Difference between Docker Image and Container
The best way to explain difference between a Docker Image and Container is as follows:
The Designer Way- You have a WordPress website and need to create a new page. You chose a template based on which new page will be created. You can create several pages from a single template. Here, Templates can be referred to as Docker Images and Containers can be referred to as pages based on that Template.
The Developer Way- You have a class and instantiating that class creates an object of it. You can create several objects of a class. Here, Class is similar to Docker Image while Objects that get created from that class can be referred to as Containers.
Getting your first Docker Image
Docker Images, as mentioned earlier, can be present in public repositories or private repositories. For the sake of simplicity, we will use the docker public repository at- https://hub.docker.com/explore/ for seeing the list of available images and getting one on our local machine.
If you go to https://hub.docker.com/explore/, you will see several repositories. Let us take the Alpine for our example. Alpine is a minimal Docker image based on Alpine Linux. An Image can be pulled from here using the command:
docker pull alpine
When you type this command in your terminal window, you will see that it pulls the image from Docker Hub and downloads it to your local system. Once the image is downloaded, you can see the list of available images on your system by running the command:
The above command will list out all Docker Images available on your system.
Running your first Docker Container
Now that you have a Docker Image available, we can run it and get a Docker Container. In order to run a Docker Container, we type the command:
docker run alpine
This will run the container, but you would see nothing happened. This is because though we had run the container, we had nothing to run within the container, hence it stopped as soon as it was launched. You can see the list of all containers, including stopped ones using the command:
docker container ls -all
On running the command, you will see that it shows the Container ID which was assigned to it, the Image on which this container was based and the Status of Exit as it was closed on launch.
This brings end to our Part 3 of the series where we explained about Docker Images and Containers. In the next part, we would dive deeper into commands related to Docker Images and Containers.