DevOps Series: Docker Fundamentals for the beginners

Monowar Mukul
3 min readDec 30, 2023

--

GitHub link: https://github.com/asiandevs/DevOps-docker.git

Introduction

Welcome to the Docker Fundamentals and Best Practices Guide! This comprehensive guide is designed to help you understand the core concepts of Docker, a powerful containerization platform. Whether you are a developer, system administrator, or IT professional, this guide will equip you with the knowledge to leverage Docker effectively in your projects.

Docker vs. Virtual Machines

Before diving into Docker, let’s briefly compare it with traditional virtual machines. Unlike VMs that run entire operating systems, Docker containers encapsulate only the necessary dependencies for an application. This makes containers lightweight, faster to start, and more resource-efficient. We’ll explore these advantages and provide insights into when to choose Docker over VMs.

Docker Installation on EC2 Linux

Prerequisites

Before installing Docker on an EC2 Linux instance, ensure that you have:

- An active AWS account
- An EC2 instance running a supported Linux distribution

Installation Steps

Follow these steps to install Docker on your EC2 Linux instance:

1. Update the package index: `sudo yum update -y`
2. Install Docker: `sudo yum install docker -y`
3. Start the Docker service: `sudo service docker start`
4. Add your user to the `docker` group: `sudo usermod -aG docker $USER`
5. Log out and log back in to apply group changes.

Now, you’re ready to leverage Docker on your EC2 instance. We’ll explore common troubleshooting tips and security considerations to ensure a smooth installation.

Docker Lifecycle

Understanding the Docker lifecycle is crucial for managing containers effectively. The key stages include:

1. Create: Build containers using images.
2. Run: Start containers based on images.
3. Update: Modify existing containers or create new ones.
4. Pause: Temporarily halt container processes.
5. Stop: Gracefully stop container processes.
6. Start: Resume execution of a paused container.
7. Restart: Stop and start a container in one command.
8. Kill: Forcefully stop a container.
9. Remove: Delete stopped containers.
10. Prune: Remove unused resources.

We’ll delve into each stage, providing practical examples and tips to optimize your Docker workflow.

Docker Services Build, Push, and Pull Operations

Build

Build Docker images from Dockerfiles using the `docker build` command. This involves specifying a Dockerfile and context (build context). We’ll cover best practices for writing efficient Dockerfiles and explore advanced build options.

Example:

```bash
docker build -t my-image:latest .
```

Push

Push images to a container registry for sharing and distribution. Before pushing, tag the image appropriately. We’ll discuss container registries, image versioning, and securing your images.

Example:

```bash
docker tag my-image:latest registry.example.com/my-image:latest
docker push registry.example.com/my-image:latest
```

Pull

Pull images from a container registry to your local environment. We’ll explore strategies for efficient image management and version control.

Example:

```bash
docker pull registry.example.com/my-image:latest
```

Dockerfiles

Dockerfiles are configuration files that define how to build a Docker image. They contain instructions for installing dependencies, setting up the environment, and configuring the application. We’ll go beyond the basics, covering multi-stage builds, caching strategies, and best practices for optimizing image size.

Example Dockerfile:

```Dockerfile
# Use an official base image
FROM ubuntu:latest

# Set the working directory
WORKDIR /app

# Copy application code to the container
COPY . .

# Install dependencies
RUN apt-get update && apt-get install -y python3

# Define the command to run the application
CMD [“python3”, “app.py”]
```

Volumes

Docker volumes provide a way to persist data outside the container. They enable data sharing between the host and the container. We’ll explore various volume types, backup strategies, and considerations for managing persistent data in Dockerized applications.

Example:

```bash
docker run -v /path/on/host:/path/in/container my-image:latest
```

Explore more Docker volume options to suit your application’s data management needs.

Conclusion

Congratulations! You’ve now covered essential Docker fundamentals, installation on EC2 Linux, lifecycle management, service operations, Dockerfiles, and volumes. Experiment with these concepts to enhance your containerization skills and streamline your development workflows.

For advanced topics, consider exploring container orchestration with tools like Kubernetes and Docker Compose. This guide provides a solid foundation, but the world of Docker is vast and continually evolving.

Happy Dockering!

--

--

Monowar Mukul

Monowar Mukul is a Cloud Solution Architect Professional. /*The statements and opinions expressed here are my own & nothing with my present or past employer*/