Getting Started With Docker: 5 Easy Steps
Docker is synonymous with DevOps. What is Docker? How can you learn Docker? Let’s find out in this post.
Join the DZone community and get the full member experience.
Join For FreeDocker is synonymous with DevOps. For me, Docker is one of the top five technology innovations of the last decade.
What is Docker? How can you learn Docker? Let’s find out.
Table of Contents
- Why Do We Need Docker?
- How Does Docker Help?
- What Are Containers?
- What Is OCI?
- What Are Some of the Important Terms Used With Docker?
- 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 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 the 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 a single way to implement observability (logs, metrics, traces, etc.) 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 as follows:
- 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 Docker containers without installing them on your local machine.
Docker simplifies operations as below:
- 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 the Open Container Initiative (OCI) is to create open industry standards around container formats (i.e., Docker Image) and runtimes (i.e., 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 Terms Used With Docker?
- Docker Image: A package representing a 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?
Follow these five easy steps to get started with Docker.
6.1 Install Docker on Your Local Machine
You can find instructions to install Docker Engine for your specific OS in the official Docker documentation.
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
Refer to this source code of a Node.js application. Here are the important files:
Dockerfile
- Instruction to create Docker imageindex.js
- Code for REST APIpackage.json
- Your dependencies
Let’s look at the Dockerfile in a little bit more detail:
FROM node:8.16.1-alpine
WORKDIR /app
COPY . /app
RUN npm install
EXPOSE 5000
CMD node index.js
Dockerfile contains instructions to create Docker images.
FROM
- Sets a base imageWORKDIR
- Sets the working directoryRUN
- Execute a commandEXPOSE
- Informs Docker about the port that the container listens on at runtimeCOPY
- Copies new files or directories into an imageCMD
- Default command for an executing container
The following set of instructions help you to create a Docker image.
cd
into the folder containingDockerfile
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 on other machines or you want to deploy it to the cloud, you need to push it to a Docker registry. One of the popular Docker registries is Docker Hub.
You can create an account and log in to it using the following:
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 a 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
Published at DZone with permission of Ranga Karanam, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments