Problems faced while creating CI/CD of Java students Management project(Infra setup on a single node using containers)

Problem1:

Issue Description

Problem: Users encounter the error "Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?" when trying to execute Docker commands within a Docker-in-Docker (DinD) setup. This issue typically arises due to permission problems accessing the Docker socket (/var/run/docker.sock), which is essential for communicating with the Docker daemon.

Context:

  • Environment: Docker-in-Docker (DinD) setup.

  • User Scenario: Jenkins running builds inside a Docker container, attempting to execute Docker commands.

  • Host Setup: Docker group ID on the host is 982, which does not exist inside the container.

  • Container Setup: Jenkins user with user and group ID 1000.

Solution

Step 1: Align Group IDs

  • Objective: Create a group inside the container with the same group ID as the Docker group on the host (982) and add the Jenkins user to this group.

  • Commands:

      DockerfileCopy code# In Dockerfile for the Docker-in-Docker slave
      RUN addgroup -g 982 dockergroup
      RUN adduser jenkins dockergroup
    

Step 2: Ensure Correct Docker Socket Mounting

  • Objective: Mount the Docker socket from the host into the container.

  • docker-compose.yml Update:

      yamlCopy codevolumes:
        - /var/run/docker.sock:/var/run/docker.sock
    

Step 3: Restart Docker Daemon

  • Objective: Apply changes and restart the Docker service.

  • Commands:

      bashCopy codesudo systemctl daemon-reload  # Reloads systemd manager configuration
      sudo systemctl restart docker  # Restarts Docker service
    

Additional Notes

  • Restarting Docker: Be aware that restarting the Docker service will stop all running containers. Make sure this won't disrupt any important services or tasks.

  • Permissions: Commands require superuser privileges (sudo).

  • Configuration: daemon-reload is only necessary if changes have been made to the Docker service configuration files.

  • Security: Adjustments to Docker and system configurations, especially in a DinD context, should be carefully considered for their security implications.

Conclusion

Aligning group IDs and ensuring proper Docker socket mounting are crucial steps in resolving permission issues when accessing the Docker daemon in a Docker-in-Docker setup. Restarting the Docker daemon applies changes and refreshes the service state. This documentation outlines the steps to diagnose and solve the encountered issue, emphasizing necessary precautions and system considerations.