Getting started with Docker

If you are getting started or are a seasoned IT professional dealing with building or deploying application code, you may have heard of containers. This is my note, along with a few links as I wrapped my head around the concept of a docker image, an image repository, and an image container. By the end of this note, I hope to have inspired you to know more about containers.

What is Docker? At a high level, as any other DevOps took, it is a technology that enables an organization to deliver value to its customers efficiently. Hence, if an organization wants to realize the benefits of DevOps adoption, using a container-based approach with Docker makes it feasible.

The traditional development process has benefitted by using a version control system like Git, enabling project teams to classify code into different branches and map each branch to an environment/s. Isolating and managing development and production code was crucial to creating a manageable and scalable product development process. Project teams enhanced the process by using branching and merging strategies aligned with the product’s release cycle. However, considering the environment where project teams hosted the product, such improvements were yet to be made. Yes, managing environments in the form of infrastructure-as-code was a step in the right direction. But the benefits are higher in the case of cloud-native solutions. In the classic case of hosting an application on a virtual machine, managing infrastructure-as-code became challenging beyond a point. And that is the challenge Docker solved. Docker did not just solve it, but it improved it by using the concept of a version control system. I would highly recommend reading more about the underlying concept at JFrog-a-beginners-guide-to-docker.

I found the below three concepts necessary to understand as I was studying.
Docker image:
The concept is well explained in the following article  –docker-images.
Image repository:
As the name suggests, it’s a host to store docker images. Docker Hub and Amazon Elastic Container Registry (Amazon ECR) are examples of image repositories. Here’s a quick 6-minute introduction to Amazon ECR -getting-started.
Image container:
A container is a usable manifestation of a docker image. A container is created from a Docker image file. I found a good explanation at docker-what-are-containers.

Note: I used to get confused between registry and repository. After a bit of reading, I learned that a docker repository is a remote location that holds all the push-ed images of an application. A docker registry stores a collection of repositories.

I learn hands-on, so here is a fast workflow of using Docker. I used Nigel Poultin’s git repo (gsd-first-container) to create a docker image, push that image into a repository and then create a container from the image in the repository. I had Git and Docker installed on my computer before I started. I also had a Docker Hub account and an empty repository created.

Step 1: Create a docker image from a DockerFile
After cloning the repo, I navigated to the folder (first-container) where the Docerkfile was located via the command line. There I ran the following command:
docker image build -t kunduso/getting-started:v0.1 . where kunduso is my Docker Hub ID and getting-started is the repository where I wanted to store the image, and v0.1 is a tag that I associated with this particular image. The . at the end of the command tells Docker where to find the Dockerfile for this project. It is the same working directory in this case. After firing the command, Docker went through all the steps listed in the Dockerfile one by one. Below is a tiny snippet of the log file.

61.Image-2
Step 2: Upload the docker image to an image repository
After the previous step, an image was created on my computer. To check all the images, I ran the command docker image ls which listed all the images available on my local (computer). After creating the image, I wanted to push that to the Docker Hub repository. The command to push was: docker image push kunduso/getting-started:v0.1
However, it was not well received, as you can see from the log file.
61.Image-3
That is because I had not yet logged into my Docker Hub account. The command to login is docker login
61.Image-4
After a successful login, I executed the docker image push command once again, which produced the below log.
61.Image-5
I logged into my Docker Hub account and verified that the v0.1 version of the image was present.
Step 3: Create a container from the image in the image repository
At the end of step 2, I had a docker image available with all its dependencies tied in on my computer and also in the Docker Hub. To use an image, I need to convert that to a container. Since the image was already available on my computer, Docker will use that. And to prevent that and use the image from the Docker Hub repository, I deleted my local image. The command to delete an image from my computer was: docker image rm kunduso/getting-started:v0.1
61.Image-6
To confirm that the image did not exist on my computer, I ran docker image ls and did not find the image in the result. So the next command to run was docker container run -d -p 8000:8080 --name app kunduso/gettings-started:v0.1 As you can see from the log image below, the image was pulled from the remote repository.
61.Image-7
At that stage, I accessed http://localhost:8000/, and the container was up and running.
61.Image-8
The container also listed its id on the web page. And that was it. That was the use case I had set for myself -to create a Docker image, push the image to a remote repository, and create a container from the image in the remote repository.

With my use case completed, it was time to release the resources  -stop and delete the container. The command to stop the container is docker container stop $(ContainerName) where $(ContainerName) is app in my case.
The command to delete the container is docker container rm $(ContainerName) where $(ContainerName) is app in my case.

If you have followed me and want to start learning via tutorials, please check out this YouTube video from TechWorld with Nana Docker Tutorial for Beginners. Also, here is some more additional study material listed at Docker-Docs.

Reference:
Nigel’s Pluralsight course: Getting Started with Docker

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s