Developers need to quickly spin up any development environment without having to install all of the software and tools necessary on their local machine. This is accomplished using images and containers but what does that mean? Today, I’m going to explain the difference between a docker image vs container.
What is a Docker Image?
An image is a package containing the information needed to create a container. It includes all the dependencies and deployment configurations that are used at container runtime. Images can inherit from multiple base images using layering. We will discuss this topic in depth during this course and you will build base images and work with the builder pattern and multi-stage builds. layers stacked on top of each other to form the container's filesystem. An image does not have state and it never changes.
If you were working in your local development environment, to get started with any project your first step used to be installing everything you need for the project you’re working on such as your SQL server, IIS, search index provider like Solr, etc. This process is error prone and tedious.
Docker images include all of those tools which means that when you start on a new project, the developers on the team would simply pull the docker image! It’s honestly the best thing ever.
What is a Docker Container?
On the other hand, a container is a runtime instance of an image. It has an image, execution environment, and instructions for runtime such as doing a nuget restore and building the solution using MSBuild.
The idea behind Docker is that it defines a standard to ship software. Containers refer to Shipping Containers which define a standard to ship goods globally. Those goods arrive at a dock and a dock worker (also known as docker) is the person who loads and unloads goods from shipping containers. I was working with Docker for over a year before I heard this and I wanted to pass it on! Let’s take a look at this in practice!
How To Pull A Docker Image and Run a Docker Container
I’m going to use the static-site example. You can get this from the docker-curriculum repo using the run command shown here. I use the Docker extension in VSCode to work with Docker images and containers and I highly recommend it.
docker run -d -P --name static-site prakhar1989/static-site
How To Get The Port Number For a Running Docker Container
You will need to get the port for your static site using the docker port command and specifying the static-site container.
docker port static-site
Open a browser and navigate to your localhost and port number.
Using the Docker Extension in VSCode
I like to use VSCode to work with my Docker containers and if you install the extension, you can manage your containers, images, registries, networks, volumes, and contexts. You can expand the images and containers panels to see what you have created! The name of the image and container is the same: prakhar1989/static-site. However, the container can be referenced by the shorter name or tag “static-site”.
You can also view your containers using the command docker ps. This will show you valuable information about your container including the status and ports.
To view all images, use the command docker images.
In summary, a Docker image is a package that includes all the dependencies and deployment configurations that are used at container runtime. A container is the runtime instance of an image.