What are the differences between a Docker image and a container?
A container is a collection of one or more processes, organized under a unique name and identifying identifier that are isolated from other processes running in an IT environment. This computing environment can be a physical computer or a virtual machine.
A container image is a template that defines how an image will be performed at run time.
While containers started out as a Linux technology, you can create containers in the Windows operating system as well.
The important thing to understand about Docker technology is that it has two main components: the client CLI tool and the container runtime environment. The CLI tool is used to run instructions to the Docker runtime environment on the command line. The job of the Docker runtime is to create containers and run them on the operating system.
Docker is a company that made containers popular. However, container technology existed before Docker. FreeBSD, LXC and Director are the first container technologies. Docker appeared in 2013 and has become very popular. However, new container runtime environments have appeared since the introduction of Docker; for example container and Podman.
Docker images and the Dockerfile
A container image is a template by which a container will be realized at run time. The artifact that defines a container image is a Dockerfile. A Dockerfile defines the base image that will serve as the basis of the container. In addition, the Docker file will contain commands to copy the files that the container will need at run time, as well as to download any other components required by the container. Additionally, the Dockerfile will contain instructions that are part of the container initialization process.
Figure 1 below shows a simple Dockerfile.
The following is an explanation of each line in the Dockerfile.
Containers, images and the Dockerfile
The Dockerfile, as shown above in Figure 1, declares node: 15.4.0-alpine3.10 as the base image.
The Dockerfile has instructions for declaring a working directory, /application
Docker copies all application files in the current directory of the developer’s file system to the working directory, /application.
The Dockerfile tells the Docker runtime to run npm install in order to download the dependency packages associated with the application.
One of the characteristics of a container is that it can expose port numbers specific to the container and bind code to the given port number. In this case, the Dockerfil tells the runtime to expose port 3000.
Finally, the Dockerfile tells the runtime to start the application by running the command, node index.js.
Creating Docker images
To create the Docker image, you run the following command:
docker build -t=an_image .
- docker is the CLI command
- to build is the subcommand used to create the image based on the Dockerfile
- -t = some_image is a parameter that indicates the image tag, in this case, a picture. You can think of a tag as a way to identify an image.
- . is a dot that indicates that the Dockefile on which to implement the build is in the current directory. If the Dockerfile is elsewhere, you provide the path to the Dockerfile instead of the period.
Once you’ve created a Docker image, you can use it to create a container on the local machine or you can store it in a container registry such as Docker Hub. When a container image is stored in a container registry, it is available to other users.
Docker container creation
Once a container image is created you use the subcommand, Course to realize the container in relation to the Dockerfile. Here is an example of using the subcommand.
docker run -d --name mycontainer -p 3000:3000 an_image
- docker is the CLI command
- Course is the subcommand used to create the container based on the container image
- -D is a parameter that tells the container runtime to run the container in the background so as not to block the terminal window in which the runtime is created
- -Name my container is a parameter that gives a name to the container, in this case, my container. In addition, the container will be assigned a unique identifier by the container runtime.
- -p 3000: 3000 is a parameter that tells the container runtime to bind port 3000 of the local machine to port 3000 of the running container
- a picture declares the Docker image to use. The Docker runtime will first search the local machine to locate the image. If the container image is not on the local machine, Docker will look for the default container registry for the image. The default container registry is usually DockerHub.
To verify that the container is running, enter the following command in a terminal window:
docker ps -a
You will get results similar to the following:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 16126511090f an_image "node server.js" 8 seconds ago Up 6 seconds 0.0.0.0:3000->3000/tcp mycontainer
Docker vs container image
The main difference between a Docker image and a container is that a Docker image is a template that defines how a container will be made. A Docker container is a runtime instance of a Docker image.
The purpose of this article is to answer the question, what is a Docker image versus a Docker container? This brief explanation only covers the tip of a very large topic. The Docker website has much more information on the peculiarities of Docker containers and images as well as other topics of general interest.