Understanding Docker: A Beginner's Guide To Containerization😌

Understanding Docker: A Beginner's Guide To Containerization😌

·

6 min read

Introduction:

Here’s a beginner-friendly article on Docker that introduces its core concepts and practical applications.

If you work in IT or any technology-related field and are not yet familiar with containerization, this article will help you understand the concept and why it is important.

Prerequisites:

  • Familiarity with command-line interfaces (CLI): You should be familiar with using a terminal or command-line interface (CLI) to run Docker commands.

  • Networking Fundamentals: A basic understanding of networking concepts, such as ports and protocols, will help in working with Docker containers and their connectivity.

  • Version Control Systems: Familiarity with tools like Git can be helpful when managing codebases alongside Docker workflows.

  • Willingness to learn and understand all the concepts in this article.

Why Docker?

Amongst all other containerization platforms, docker is the most widely used platform with a range of over 318 billion users worldwide. Big companies like Netflix, Spotify, Paypal, Airbnb etc all use docker to run their application systems.

Docker is a software platform that helps developers build, test, and run applications quickly. It uses containers and achieves this through containerization, a method of packaging applications and their dependencies together into lightweight, portable units called containers.

Why Use Docker?

Consistency Across Environments: Docker containers makes sure your application acts the same way regardless of where it's running—on your local machine, a staging environment, or a production server.

Resource Efficiency: Unlike virtual machines, containers share the host operating system's kernel, which makes them lightweight and fast to start.

Portability: Containers can run on any system that has Docker installed, making them highly portable across different operating systems and cloud providers.

Simplified Deployment: Docker makes it easy to deploy applications by bundling all dependencies into one single image.

Key Docker Components

When starting out with docker, you cannot do without knowing these terms, they are essential knowledge in docker:

1. Docker Images

A Docker image is a read-only template that contains the application code, libraries, and other dependencies. Think of it as a snapshot of an environment. Images are used to create containers.

2. Docker Containers

A container is a running instance of a Docker image. It is isolated, lightweight, and includes everything needed to execute the application.

3. Dockerfile

A Dockerfile is a text file with instructions to build a Docker image. It defines the base image, application code, and any required configurations or dependencies.

4. Docker Hub

Docker Hub is a cloud-based registry where you can find and share Docker images. It’s like an app store for containers. After creating a docker container, it is pushed to a place called Docker Hub and other people can access your container remotely.

Setting Up Docker

To get started with docker, follow these steps:

Step 1: Install Docker

Download and install Docker Desktop for your operating system (Windows, macOS, or Linux) from the official Docker website.

Step 2: Verify Installation
Run the following command in your terminal to check if Docker is installed correctly:

docker --version

You should see the Docker version installed on your system.

Getting started with docker

Let’s walk through a simple example to run a web server using Docker.

Step 1: Pull an Image

Pull a prebuilt image from Docker Hub. For example, the official Nginx image:

docker pull nginx

Step 2: Run a Container

Use the pulled image to start a container:

docker run -d -p 8080:80 nginx

This command runs the Nginx container in detached mode (-d) and maps port 8080 on your machine to port 80 in the container.

Step 3: Access the Application

Open a web browser and go to http://localhost:8080. You should see the Nginx welcome page.

Managing Containers

List Running Containers

docker ps

This displays all active containers.

Stop a Container

docker stop [CONTAINER_ID]

Replace [CONTAINER_ID] with the container’s ID or name.

Remove a Container

docker rm [CONTAINER_ID]

Key Docker Commands

  • docker build: This command is used to build a Docker image from a Dockerfile.

  • docker images: This command is used to list all available images on your system.

  • docker rm: This command is used to remove or delete a Docker image.

  • docker logs: This command is used to view logs of a running container.

  • docker run: This command is used to create and start a new container from a docker image

  • docker pull: This command is used to download a Docker image from a Docker registry, such as Docker Hub, to the local Docker host.

  • docker push: This command is used to push a Docker image to a Docker registry, making it available for others to download and run.

  • docker volume: This command is used to manage volumes, which are used for persisting data generated by and used by Docker containers.

  • docker commit: This command is used to create a new docker image from a running or stopped container.

  • docker network: This command is used to manage networking for Docker containers, allowing you to create custom networks, inspect existing ones, and more.

  • docker ps: This command is used to lists running containers. Use docker ps -a to list all containers, including stopped ones.

  • docker logs: This command is used to retrieve logs from a container. This is helpful for debugging and monitoring application output.

  • docker cp: This command is used to copy files or directories between the host and a container.

  • docker prune: This command is used to clean up unused Docker objects, such as stopped containers, dangling images, unused networks, and volumes

  • docker stats: This command is used to display real-time resource usage statistics (CPU, memory, etc.) for running containers.

  • docker import/export: docker export: Exports the filesystem of a container as a tar archive. docker import: Imports a tarball to create a new image.

Resources

If you're willing to get more resources that will help you learn docker more intensively, check out;
✨Udemy: There are a lot of courses on docker that can be really helpful
✨Youtube: An official docker YouTube channel provides comprehensive tutorials and demos. There are also a lot of tutorial videos you can learn from
✨Docker documentation: The official Docker documentation
✨Docker Hub: Docker Hub also provides documentation, tutorials, and examples for using Docker images in different applications.
✨Docker blogs and tutorials: Just like this one, there are other good articles or blogs on Docker that'll help you gain insights on the topic.

Next Steps

Now that you’ve grasped the basics of Docker, you can explore more advanced concepts like:

  • Building custom Docker images using a Dockerfile.

  • Managing multi-container applications with Docker Compose.

  • Scaling applications using Docker Swarm or Kubernetes.

Conclusion

In this article, you have learned about Docker: what it is and why it is widely used. You have explored the key components of Docker, which is essential knowledge for anyone starting out. Additionally, you have learned how to set up your Docker environment and the basic commands you'll need. Finally, you've discovered resources and tutorials that will support you on your journey.

Docker is a powerful tool that simplifies the complexity of modern software development and deployment. With practice, you’ll find it indispensable in your DevOps toolkit!

Â