Rootless-ly Running Docker Daemon inside another Docker container

2 min read 06-10-2024
Rootless-ly Running Docker Daemon inside another Docker container


Running Docker Daemon Rootlessly Inside a Docker Container: A Guide

The Problem: Running Docker Within Docker

Imagine you need to run a Docker daemon inside another Docker container. It's a common scenario for developers wanting to test applications in isolated environments or for building CI/CD pipelines that require containerized Docker instances. However, directly running a Docker daemon inside a container can lead to security risks and complexities due to the traditional requirement of running the daemon with root privileges.

The Solution: Rootless Docker

Fortunately, the "rootless" Docker mode provides a secure and efficient way to run Docker daemons without root privileges. This approach allows you to run Docker within Docker safely and effectively.

Understanding Rootless Docker

Rootless Docker utilizes a special user namespace and cgroups configuration to isolate Docker operations without requiring root access. This means your containerized Docker daemon can manage containers within its own isolated environment, without affecting the host system.

Practical Example

Let's illustrate how to run a Docker daemon rootlessly inside a container using a simple example:

FROM ubuntu:latest

# Install Docker and required dependencies
RUN apt-get update && \
    apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release software-properties-common && \
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - && \
    add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" && \
    apt-get update && \
    apt-get install -y docker-ce docker-ce-cli containerd.io

# Create a Docker group and add the user
RUN groupadd -g 1000 docker && usermod -aG docker $USER

# Configure rootless Docker
ENV DOCKER_HOST=unix:///var/run/docker.sock
ENV DOCKER_TLS_VERIFY=0

# Start Docker daemon in the background
CMD ["dockerd", "-d", "--host=unix:///var/run/docker.sock"]

This Dockerfile builds an image that installs Docker, configures rootless mode, and starts the Docker daemon.

Advantages of Rootless Docker:

  • Enhanced Security: Eliminates the need for root access, minimizing potential security risks.
  • Simplified Management: Offers a streamlined approach to containerizing Docker daemons.
  • Resource Isolation: Enforces resource limits for the Docker daemon, preventing resource contention.

Key Considerations:

  • System Requirements: Ensure your host system meets the prerequisites for rootless Docker, including kernel version compatibility.
  • Network Configuration: Depending on your setup, you might need to configure network access between the outer and inner containers.
  • Permissions: Ensure the user running the outer container has the necessary permissions to access the Docker socket.

Conclusion:

Running a Docker daemon rootlessly inside a container is a powerful technique for achieving greater flexibility and security. By utilizing rootless Docker, you can leverage its benefits while maintaining a secure and controlled environment. As you explore further, remember to consult official documentation and community resources for more advanced use cases and configurations.

References: