Docker is synonymous with DevOps. For me, Docker is one of the top 5 technology innovations of the last decade.
What is Docker? How can you learn Docker? Let’s find out.
Table of Contents
- 1: Why do we need Docker?
- 2: How does Docker help?
- 3: What are Containers?
- 4: What is OCI?
- 5: What are some of the important terminology used with Docker?
- 6: How can I learn Docker?
- 6.1: Install Docker on Your Local Machine
- 6.2: Run Pre-Built Docker Images
- Running a Python application
- Running a Java application
- Running a Node application
- Summary
- 6.3: Create and Run Your Own Container Image
- 6.4: Push Your Container Image to Docker Hub
- 6.5: Understand Cloud Services for Running Containers
- 7: Next Steps
1: Why do we need Docker?
One of the popular architectural trends of the last decade is microservices.
In a monolith applications, you have one large deployable unit serving most features of your applications.
In a microservices architecture, you are dividing your applications into smaller, independently deployable building blocks.
Microservices architecture provides you with a flexibility to innovate and build applications in different programming languages (Go, Java, Python, JavaScript, etc).
However, flexibility comes at a cost. Deployments and operations become more complex.
How can we deploy applications and services built in different languages the same way?
How can we have single way to implement observability (logs, metrics, traces, ..) for different services and applications?
Enter Docker and containers!
2: How does Docker help?
When you are using Docker, you build a separate Docker image for each microservice.
Each Docker image contains everything that you need to run an instance of a microservice:
- OS
- Application Runtime (JDK or Python or NodeJS)
- Application Code
- Application Dependencies
You can deploy and run this Docker image the same way (using the same process) on any infrastructure - Your local machine, a corporate data center and in the cloud (AWS, Azure or Google Cloud).
Docker simplifies development:
- You can run your Docker image anywhere - on your local machine, or on your testers machine
- You can run your database and queues (MySql, Kafka,.. etc) as a docker containers without installing them on your local machine
Docker simplifies operations:
- You deploy all microservices the same way
- You can implement observability (logs, metrics, traces, ..) for all microservices the same way
3: What are Containers?
At the start, Docker answered two important questions:
- How can you build a Docker Image?
- For example: We can specify instructions in
Dockerfile
and create a Docker image.
- For example: We can specify instructions in
- How can you run a Docker Image?
- Docker provides a Docker Runtime - install it once and you can run any Docker image.
A running instance of a Docker image is called a container.
You might have multiple instances of a microservice running. Each instance is a container.
4: What is OCI?
The goal of Open Container Initiative (OCI) is to create open industry standards around container formats (for example, Docker Image) and runtimes (for example, Docker Runtime).
The easiest way to think about it now is:
- OCI provides the interface
- Docker is an implementation
5: What are some of the important terminology used with Docker?
- Docker Image: A package representing specific version of your application (or software)
- Contains everything your app needs (OS, software, code, dependencies)
- Docker Container: Runtime instance of a docker image
- Docker Registry: A place to store your docker images
- Docker Hub: A registry to host Docker images
- Docker Repository: Set of Docker images for a specific application (tags are used to differentiate different images)
- Dockerfile: Text file with instructions to create a Docker image
6: How can I learn Docker?
6.1: Install Docker on Your Local Machine
You can find instructions for your specific OS here - https://docs.docker.com/engine/install/.
6.2: Run Pre-Built Docker Images
Running a Python application
Use this command to launch the application.
docker container run -d -p 5000:5000 in28min/hello-world-python:0.0.1.RELEASE
You can access the running application at http://localhost:5000.
You can kill the running container using this sequence of commands.
docker container list
docker container stop <<ID_FROM_PREV_COMMAND_OUTPUT>>
Running a Java application
Use this command to launch the application.
docker container run -d -p 5000:5000 in28min/hello-world-java:0.0.1.RELEASE
You can access the running application at http://localhost:5000.
Running a Node application
Use this command to launch the application.
docker container run -d -p 5000:5000 in28min/hello-world-nodejs:0.0.1.RELEASE
You can access the running application at http://localhost:5000.
Summary
You can run containers for different microservices built in different languages the same way.
6.3: Create and Run Your Own Container Image
The following folder contains the source code of a nodejs application
- https://github.com/in28minutes/devops-master-class/tree/master/projects/hello-world/hello-world-nodejs
Here are the important files:
Dockerfile
- Instruction to create Docker image.index.js
- Code for REST APIpackage.json
- Your Dependencies
Let’s look at the Dockerfile in a little bit more details:
FROM node:8.16.1-alpine
WORKDIR /app
COPY . /app
RUN npm install
EXPOSE 5000
CMD node index.js
Dockerfile contains instruction to create Docker images
- FROM - Sets a base image
- WORKDIR - sets the working directory
- RUN - execute a command
- EXPOSE - Informs Docker about the port that the container listens on at runtime
- COPY - Copies new files or directories into image
- CMD - Default command for an executing container
Following set of instructions help you to create a Docker image.
cd into the folder containing Dockerfile
cd ../hello-world-nodejs/
Build a Docker image. in28min/hello-world-nodejs:0.0.2.RELEASE
is the complete reference to a docker image. in28min/hello-world-nodejs
is called the repository name. 0.0.2.RELEASE
is called the tag.
docker build -t in28min/hello-world-nodejs:0.0.2.RELEASE .
Once a Docker image is built, you can run it on your local machine.
docker container run -d -p 5000:5000 in28min/hello-world-nodejs:0.0.2.RELEASE
Congratulation on successfully building and running your first Docker image.
6.4: Push Your Container Image to Docker Hub
If you want to use your Docker image in other machines or you want to deploy it to cloud, you need to push it to a Docker registry.
One of the popular Docker registries is Docker Hub.
You can create an account at https://hub.docker.com/ and login to it using
docker login
Once you are connected to Docker Hub, you are ready to push your docker image. (Replace «YOUR_DOCKER_HUB_ID» with your Docker Hub id).
docker build -t <<YOUR_DOCKER_HUB_ID>>/hello-world-nodejs:0.0.2.RELEASE .
docker push <<YOUR_DOCKER_HUB_ID>>/hello-world-nodejs:0.0.2.RELEASE
You can now go to your friend’s laptop and run your Docker Image
docker container run -d -p 5000:5000 <<YOUR_DOCKER_HUB_ID>>/hello-world-nodejs:0.0.2.RELEASE
6.5: Understand Cloud Services for Running Containers
You have variety of options in each cloud to run your Docker Image.
Here’s a quick summary:
- AWS: AWS Elastic Beanstalk
- GCP: Google App Engine and Google Cloud Run
- Azure: Azure App Service
In addition, you can use container orchestration solutions. For now, let’s not worry about them!
7: Next Steps
Here are some of the recommended next steps to continue your journey:
- Securing your Docker Image
- Run Microservices using Docker
- Understand Docker Compose
- Understand Kubernetes