Docker: Not Just for Building Containers – Unlocking Advanced Features for Modern Applications

Docker: Not Just for Building Containers – Unlocking Advanced Features for Modern Applications

Nowadays, when I see someone has learned Docker, whether through paid courses or free open-source platforms, I often find that most of these courses cover only the basics and bit intermediate level. As a result, many people miss out on utilising Docker's full potential. But Docker offers so much more than simple containerisation. In this blog, we’ll dive into those advanced Docker options that can take your skills to the next level, empowering you to optimize, secure, and scale applications like never before.

Docker has evolved into a platform that supports complex workflows and high-performance applications. From secure secrets management to GPU integration and automated scaling, Docker’s advanced capabilities have become indispensable. Let’s explore these features in depth, covering detailed demos, real-world use cases, and essential commands for each.


Let’s start with a useful tip! I’ve noticed that many people are still writing Dockerfiles manually from scratch. There could be a couple of reasons for this: maybe they don’t know about the docker init command or perhaps they’ve just forgotten about it. Whatever the case, using this simple command can save a lot of time!

1. Docker Init: Kickstart Your Docker Projects

The docker init command is designed to make it easier to create Dockerfiles and docker-compose files. It helps you get started quickly by generating basic configurations without much effort. This is especially helpful for DevOps teams and developers who want to speed up their project setup.

Creating a Dockerfile with Docker Init

To create a starter Dockerfile, you just need to run this command:


After running docker init command you have to select your application type, version, port number and app run command etc as shown in below attached image.


When you run this command, it automatically generates not just a basic Dockerfile, but also a variety of helpful files, including:

  • .dockerignore
  • Dockerfile
  • docker-compose.yaml
  • README.Docker.md

Isn’t that amazing? With just one command, you save yourself a lot of work! After that, you can easily customize these files to add any necessary dependencies, set environment variables, or modify build contexts as needed.

2. Multi-Host Networking: Seamlessly Connecting Containers Across Hosts

Docker’s networking features allow for communication between containers on the same host, but when scaling to multiple hosts, the networking configuration becomes more complex. Docker provides several networking options tailored for multi-host communication, particularly through?Overlay Networks.

Example Scenario: Distributed Microservices Communication Across Multiple Hosts

In a multi-host microservices architecture, each service might be running on different physical or virtual servers. Overlay networking allows containers on different hosts to communicate securely, which is essential for creating large-scale distributed applications.

Creating an Overlay Network for Multi-Host Communication

# Initialize Docker Swarm (enables overlay networks across hosts)
docker swarm init

# Create an overlay network
docker network create -d overlay --attachable my-overlay-network        

By creating an attachable overlay network, containers can communicate seamlessly across hosts without requiring complex external configurations.


3. Enhanced GPU Support with NVIDIA Integration for AI Workloads

Docker’s integration with NVIDIA provides robust support for GPUs, making it an ideal platform for AI and machine learning (ML) workloads that demand significant computational power. With the growing complexity of AI models and the need for faster processing, leveraging GPUs can greatly enhance performance and efficiency.

Why Use NVIDIA GPUs with Docker?

NVIDIA GPUs are specifically designed for high-performance computing tasks, such as training deep learning models and performing large-scale data analysis. By integrating NVIDIA’s technology with Docker, you can easily manage and deploy GPU-accelerated applications in a containerized environment. This means that you can take advantage of the powerful parallel processing capabilities of NVIDIA GPUs without the hassle of manual configuration or setup.

Using Docker with NVIDIA GPUs

To utilize NVIDIA GPUs in your Docker containers, you need to ensure that you have the NVIDIA Container Toolkit installed. Once set up, running GPU-enabled applications becomes straightforward. For example, you can use the following command to run a Docker container that leverages all available NVIDIA GPUs:

docker run --gpus all nvidia/cuda:11.0-base nvidia-smi        

With this command, you can leverage NVIDIA GPUs in Docker, allowing for accelerated computing power for tasks like training machine learning models.


4. Squashing Image Layers: Reducing Docker Image Size

When you build a Docker image, each command you run in your Dockerfile creates a new layer. While this is useful for keeping track of changes, it can also lead to larger image sizes. Squashing layers is a technique that combines multiple layers into one, helping to reduce the overall size of the Docker image. This makes your images easier to manage and faster to transfer.

Why is Layer Size Important?

Docker images consist of layers that stack on top of each other. Each layer adds to the total size of the image, which can lead to slow downloads and increased storage costs. A smaller image size is beneficial for several reasons:

  • Faster Deployment: Smaller images can be pulled from a registry more quickly, speeding up the deployment process.
  • Efficient Use of Resources: Reducing image size helps save storage space and bandwidth, which is especially important in environments where resources are limited.
  • Improved CI/CD Performance: Continuous Integration/Continuous Deployment (CI/CD) pipelines run faster when they work with smaller images.

How to Squash Layers During Build

To enable layer squashing, you can use the --squash option when building your Docker image. Here’s how you do it:

docker build --squash -t devops-docker-demo-optimized-image .        
By squashing layers, you’re effectively merging all the commands that were run in your Dockerfile into a single layer. This leads to a smaller image size, which can improve performance in various scenarios, particularly in CI/CD pipelines and during deployments.

5. Leveraging .dockerignore for Optimized Builds

When you build a Docker image, Docker sends all the files from your project directory to the Docker daemon. However, not all these files are needed for your image. This is where the .dockerignore file comes in handy. It helps you exclude unnecessary files and directories from the build context, which can lead to faster builds and smaller images.

Example of a .dockerignore File

Here’s a simple example of what a .dockerignore file might look like, if you had used docker init command then .dockerignore file should already present in your working directory:


An example of .dockerignore file

When you run the docker build command, Docker will automatically read this file and exclude any specified items from the build context.


6. Docker Compose File Watch: Automate Reloads During Development

What is Docker Compose File Watch?

Docker Compose File Watch is a feature that helps you work faster while developing your applications. It automatically reloads your containers whenever you update your source code files. This means you don’t have to manually restart your containers every time you make a change, which saves you a lot of time and effort.

When you’re developing an application, you often make changes to your code and want to see how those changes affect your app right away. Manually restarting your containers each time can be tedious and slow down your workflow. With File Watch, your containers update instantly as soon as you save your changes, making the development process smoother and more efficient.

How to Enable File Watch in Docker Compose

To enable the file watch feature in Docker Compose, you can use the following command:

docker-compose --watch up        

This command tells Docker Compose to start your services and watch for any changes in your source code files. When you modify a file, Docker Compose will automatically reload the affected containers. This simple feature can significantly speed up your development workflow and improve your overall experience when working with Docker.


7. Docker BuildKit Secrets Management: Securing Sensitive Information During Builds

Docker BuildKit is a powerful tool that helps you manage the building of Docker images more effectively. One of its important features is the ability to handle sensitive information, such as API keys or database passwords, securely during the build process. This means you can keep your secrets safe without exposing them in your Dockerfiles.

When you build a Docker image, you might need to use sensitive information. If you include this information directly in your Dockerfile, it can be easily accessed by anyone who has access to the image. This poses a security risk. With BuildKit, you can inject secrets during the build process without leaving traces in the final image.

How to Use BuildKit to Inject Secrets

Here’s how you can use Docker BuildKit to manage secrets securely in your Docker builds:

  1. Creating a Secret: First, you need to create a secret file that contains your sensitive information, for example, my_secret.txt.
  2. Dockerfile Example: In your Dockerfile, you can use the following command to securely access your secret:

RUN --mount=type=secret,id=my_secret \
    some-command-to-use-my_secret        

In this line, some-command-to-use-my_secret represents any command that needs to use the secret during the build.

Building with the Secret: To build your Docker image with the secret, use this command:

DOCKER_BUILDKIT=1 docker build --secret id=my_secret,src=my_secret.txt .        

This command enables BuildKit and tells Docker to inject the secret from my_secret.txt into the build process.

BuildKit ensures that the secret is not included in the final image. This means that your sensitive information is used only during the build and is kept secure from prying eyes. Once the build is complete, the secret does not remain in the image, reducing the risk of exposure.

8. Advanced Health Checks: Ensuring Application Stability

Advanced health checks in Docker help you keep your applications running smoothly. They allow Docker to monitor the health of your containers actively. If a container becomes unresponsive or fails, Docker can automatically restart it or send alerts, ensuring your application stays stable and available.

When you run applications in containers, it's crucial to know if they're functioning correctly. A container could stop responding due to various issues, like a bug in the application or a problem with external services. Advanced health checks help detect these problems early, so you can take action before they affect your users.

How to Set Up an Advanced Health Check

You can set up an advanced health check in your Dockerfile using the following command:

HEALTHCHECK --interval=30s --timeout=10s \
    CMD curl -f https://localhost/health || exit 1        

With this setup, Docker will regularly check the health of your application. If the health check fails, Docker can automatically restart the container to recover from the failure. This process helps maintain high availability for your applications, allowing them to recover quickly without requiring manual intervention.


9. VirtioFS: Enhanced File Sharing Performance in Docker Desktop for Mac

In the 4.6 release of Docker Desktop for Mac, a significant upgrade has been introduced for file sharing, particularly for macOS users. The new experimental feature called VirtioFS replaces the previous default method, gRPC-FUSE, leading to remarkable improvements in performance. During testing with the macOS community, it was found that filesystem operations could be completed up to 98% faster.

Why is this Important for Developers?

Developers often work by editing source code on their macOS host while running applications within Docker containers. For instance, a typical command to run a Symfony app might look like this:

docker run -v /Users/me:/code -p 8080:8080 my-symfony-app        

This command enables real-time editing of source code, allowing developers to save changes and see immediate results in their applications. Quick and reliable file syncing between the host and container is essential for maintaining productivity and a good user experience.

Performance Improvements

File sharing performance is crucial, especially when dealing with large projects. For example, in frameworks like Symfony, if a developer edits the code and reloads the page, the container’s web server must quickly access multiple files from the host. With modern projects often having tens of thousands of files, delays can significantly impact performance.

The introduction of VirtioFS has addressed these issues, leading to substantial performance gains:

  • 90% faster MySQL import (from 3m 16s to 18s for a 284MB file)
  • 87% faster composer install in large codebases (from 1m 27s to 11s)
  • 80% faster startup for monolithic TypeScript apps (from 1m 30s to 18s)

User feedback has been overwhelmingly positive, with many noting dramatic improvements in their development environments, such as instant migrations and overall faster setups.

How to Enable VirtioFS

To take advantage of VirtioFS, ensure your system meets the following requirements:

  • macOS 12.2 or above for Apple Silicon
  • macOS 12.3 or above for Intel

To enable VirtioFS in Docker Desktop:

  1. Make sure you have Docker Desktop version 4.6 or higher.
  2. Open Docker Desktop and go to Preferences (the gear icon).
  3. Navigate to Experimental Features.
  4. Check the boxes for Use the new Virtualization framework and Enable VirtioFS accelerated directory sharing.
  5. Click Apply & Restart.

With these simple steps, developers can unlock enhanced file sharing capabilities, leading to a more efficient and productive development experience.

Reference: https://www.docker.com/blog/speed-boost-achievement-unlocked-on-docker-desktop-4-6-for-mac/


10. Docker Scout: Enhancing Image Security and Best Practice Compliance

Docker Scout is an innovative tool designed to help developers enhance the security of their Docker images and ensure compliance with best practices. As cybersecurity threats continue to rise, having a tool like Docker Scout becomes increasingly important for maintaining safe and reliable containerized applications.

Docker Scout scans your Docker images to identify known vulnerabilities. It helps developers understand the security posture of their images and provides actionable recommendations to fix issues. This functionality is integrated directly into Docker Desktop, making it easy to access and use.

Using Docker Scout to Analyze Images

To analyze a specific image, you can use the following command:

An example of scanning a busybox docker image

  • Vulnerability Detection: Docker Scout scans the specified image (my-image:latest) for known vulnerabilities. This includes checking against a database of security advisories to see if any components in the image have reported security issues.
  • Best Practices Compliance: In addition to vulnerability detection, Docker Scout also checks if your image adheres to Docker's best practices. These practices might include optimizing image size, using minimal base images, and ensuring that sensitive information isn't embedded in the image.
  • Actionable Insights: After the analysis, Docker Scout provides you with a summary of the findings. This summary highlights any vulnerabilities detected and offers suggestions on how to remediate them. For example, it might suggest updating certain packages to their latest versions or changing configurations to enhance security.


That's it for today! I hope you enjoyed this post about Docker, Inc Advanced features. Let me know in the comments how many points you've covered so far! If you found this helpful, please like and share it—it really makes a difference. Creating content like this takes a lot of effort, and your support means the world to me.

Don't forget to follow me for the next newsletter, and guess what? I'll be starting an awesome playlist on YouTube channel very soon, so make sure to subscribe! Your encouragement keeps me motivated to bring you even more valuable insights. Thanks for being a part of this journey!



Mangesh Gajbhiye

9k+| Member of Global Remote Team| Building Tech & Product Team| AWS Cloud (Certified Architect)| DevSecOps| Kubernetes (CKA)| Terraform ( Certified)| Jenkins| Python| GO| Linux| Cloud Security| Docker| Azure| Ansible

1 个月

Very informative Shivam Agnihotri ??

回复
Ajeet Singh Raina

?? Follow me for Docker, Kubernetes, Cloud-Native, LLM and GenAI stuffs | Technology Influencer | ?? Developer Advocate at Docker | Author at Collabnix.com | Distinguished Arm Ambassador

1 个月

Fantastic write-up. I loved the overall structure of the blog and engaging content. Thank you for sharing your knowledge gained through the Meetup with the community.

DevOps Learner

DevOps Enthusiastic

1 个月

Very informative.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了