Docker enables developers to package applications and their dependencies into lightweight, portable containers, ensuring consistency across diverse computing environments.
Why was it developed ?
Docker was developed to solve the problem of software inconsistency and deployment issues. It provides a standardized containerization solution, allowing developers to package applications and dependencies into portable containers. This ensures consistent and reliable performance across different environments, addressing the "it works on my machine" problem and streamlining the development and deployment processes.
In short, the frequent complains of developers that a particular app runs on his/her own local system but crashes on others is gone.
Deploying a Containerized Application
Workflow :
writing the source code for the application
build it's image
push it into a registry (usually into dockerHub)
run it as a container image
We will be covering each step listed here. You will get a complete overview and unleash the first steps into the world on containerization and docker.
OCI (Open Container Initiative)
OCI (Open Container Initiative) is an open-source project defining standards for container formats and runtimes, ensuring compatibility and interoperability across different container platforms. It takes care of different specs including :
Image spec
Runtime spec
Distribution spec (registries)
Just a jargon (buzz word) for container image
Source Code :
For this tutorial, we will refer to the repository "gsd" of Nigel Paulton. Link: https://github.com/nigelpoulton/gsd . You need to clone this locally on your system. Also install docker on your system and make an account on DockerHub. However, you can follow along and still get the concepts cleared and follow this later.
Lets Containerize an App
Docker File is nothing but a set of build instructions for converting src code into image. Take a note that it doesn't have any extension with it. Below is a sample docker file of a simple node application.
# Use the official Node.js image with the 'current-alpine' tag as the base image
FROM node:current-alpine
# Create a directory for the application code
RUN mkdir -p /usr/src/app
# Set the working directory to the newly created directory
WORKDIR /usr/src/app
# Copy the current directory (containing your application code) into the container at /usr/src/app
COPY . /usr/src/app
# Install dependencies using npm (replace with your package manager if different)
RUN npm install
# Specify the command to run when the container starts
CMD ["npm", "start"]
Now, change directory to "first container" and you can follow along.
Code Commands :
Make sure you have docker installed, once done go with the following command to build your first docker image.
docker image build -t <dockerhub_username>/<repo>:<image_name> .
Hosting on a Repository
Now we have our image of the application ready in the local directory, let's host it in the dockerHub. If you don't have an account, you need to create one. Then, we need to login in cli via :
docker login
<username>
<password>
docker image push <dockerhub_username>/<repo>:<image_name>
Note : docker buildx build command let's us build image of different architectures.
List images
docker image ls
We have a local image and that can be used to run a container. But we will run it from docker desktop.
Analogy : Image is a stopped container and container is running image.
Remove the local image
docker image rm <dockerhub_username>/<repo>:<image_name>
Run the container. We have imperatively defined this in the command line itself and this looks cool for now, later we will do this through version control system ( git ).
docker container run -d --name web -p 8000:8080 \<dockerhub_username>/<repo>:<image_name>
-d : flag means run image in background detach from curr terminal.
-- name web : calling the service web
-p 8000:8080 : we hit 8000 on localhost, app runs on 8080 in background
First it will look for the local copy of image in current dir then go to dockerHub.
List Container
docker container ls
Congratulations, you got the container running.
Containers are like fast light-weight virtual machine.
Stop & Delete Container
docker container stop <container_name>
docker container rm <container_name>
To exit from Shell of virtual machine don't exit just type ctrl+p+q
Bravo, a decade ago virtualizing hardware was cool. Now, we are virtualizing OS.
Tools to manage Container and Infrastructure: Docker Swarm, K8s to be covered in next blog. Basically we don't directly interact with the hosted application container but rather deploy it on nodes that work as infrastructure.
Stay Curious and Don't forget to Share your Feedback.