A container is a collection of one or more processes, organized under a single name and identifying ID that is isolated from the other processes running within a computing environment. That computing environment can be a physical computer or a virtual machine.
A container image is a template that defines how an image will be realized at runtime.
While containers started out as a Linux technology, you can create containers within the Windows operating system too.
The important thing to understand about Docker technology is that it has two main components: the client CLI tool and the container runtime. The CLI tool is used to execute instructions to the Docker runtime at 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 Warden are early containers technology. Docker appeared in 2013 and has become quite popular. However, new container runtimes have appeared since Docker’s introduction; for example containerd and Podman.
Docker images and the Dockerfile
A container image is a template by which a container will be realized at runtime. The artifact that defines a container image is a Dockerfile. A Dockerfile defines the base image that will serve as the foundation of the container. Also, the Docker file will have commands for copying files that the container will need at runtime as well as downloading any other components upon which the container requires. In addition, the Dockerfile will have 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 to declare a working directory, /app
Docker copies all the files for the application in the current directory of the developer’s file system into the working directory, /app.
The Dockerfile tells the Docker runtime to execute npm install in order to download the dependency packages that are associated with the application.
One of the features of a container is that it can expose port numbers that are special 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 up the application by executing the command, node index.js.
Docker image creation
To create the Docker image, you execute the following command:
docker build -t=an_image .
- docker is the CLI command
- build is the subcommand used to create the image based on the Dockerfile
- -t=an_image is a parameter that indicates the tag of the image, in this case, an_image. You can think of a tag as a way to identify an image.
- . is a period that indicates that the Dockefile upon 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 create 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’s available to others to use.
Docker container creation
After a container image is created, you use the subcommand, run to realize the container against the Dockerfile. The following is an example of using the subcommand.
docker run -d --name mycontainer -p 3000:3000 an_image
- docker is the CLI command
- run 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 tie up the terminal window in which run is being created
- –name mycontainer is a parameter that gives the container a name, in this case, mycontainer. Also, 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 the port 3000 on the local machine to port 3000 from the running container
- an_image declares the Docker image to use. The Docker runtime will look on the local machine first to locate the image. If the container image is not on the local machine, Docker will look on the default container registry for the image. The default container registry is usually DockerHub.
To verify that the container is running, type the following command in a terminal window:
docker ps -a
You’ll 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 image vs container
The key difference between a Docker image vs a container is that a Docker image is a template that defines how a container will be realized. A Docker container is a runtime instance of a Docker image.
The purpose of this piece is to answer the question, what is a Docker image vs. a Docker container? This brief explanation covers just the tip of a very big topic. The Docker website has a lot more information about the particulars of Docker containers and images as well as other topics of general interest.