To achieve this, you need a way to manage access to Docker containers such that each team can only manage their own containers. Since direct SSH access is not an option, and you're using GitLab runners, you can leverage Docker's API and/or use Docker Compose with proper network isolation and permissions.
Here's a step-by-step guide on how to set this up:
1. Define Docker Networks and Labels
First, define a Docker network for each team to isolate their containers. Use labels to group containers by team.
# Create networks for each team
docker network create teamA_network
docker network create teamB_network
# Repeat for other teams...
2. Use Labels to Tag Containers
In each team's docker-compose.yml, use labels to tag the containers with the appropriate team.
# Example for Team A
version: '3'
services:
app:
image: teamA/app:latest
networks:
- teamA_network
labels:
- "team=teamA"
networks:
teamA_network:
external: true
3. Docker Compose Configuration for Teams
Ensure each team has a dedicated GitLab runner with its own Docker Compose configuration.
# Example for Team B's `docker-compose.yml`
version: '3'
services:
app:
image: teamB/app:latest
networks:
- teamB_network
labels:
- "team=teamB"
networks:
teamB_network:
external: true
4. Configure GitLab Runners
In your GitLab CI/CD configuration (.gitlab-ci.yml), configure the runners to use Docker Compose and ensure they use the predefined labels and networks.
# Example for Team A's `.gitlab-ci.yml`
stages:
- build
- deploy
build:
stage: build
script:
- docker-compose -f docker-compose.yml build
deploy:
stage: deploy
script:
- docker-compose -f docker-compose.yml up -d
5. Implementing Docker API with Permissions
You can expose Docker's API socket and create a script to filter access based on labels.
Create a Docker Socket Proxy
- Create a Docker socket proxy using Traefik or Nginx Proxy Manager.
- Secure it with basic authentication or API tokens.
Example with Traefik
version: '3'
services:
proxy:
image: tecnativa/docker-socket-proxy
environment:
CONTAINERS: "1"
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
networks:
- proxy
traefik:
image: traefik:v2.3
command:
- --api.insecure=true
- --providers.docker
- --entrypoints.http.address=:80
- --entrypoints.traefik.address=:8080
- --log.level=DEBUG
ports:
- 80:80
- 8080:8080
networks:
- proxy
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
networks:
proxy:
external: true
6. Script to Filter Access
Create a script or small service to wrap Docker API calls, filtering actions based on container labels.
from flask import Flask, request
import docker
app = Flask(__name__)
client = docker.DockerClient(base_url='unix://var/run/docker.sock')
@app.route('/containers/json', methods=['GET'])
def list_containers():
team_label = request.headers.get('X-Team-Label')
containers = client.containers.list(all=True, filters={"label": f"team={team_label}"})
return {"containers": [container.attrs for container in containers]}
# Add similar routes for other Docker operations...
if __name__ == '__main__':
app.run(debug=True)
Deploy the script and expose it via your Docker socket proxy. Ensure the GitLab runners are configured to use this proxy endpoint, and provide the correct team labels via environment variables.
By implementing a combination of Docker Compose, network isolation, labeling, and a Docker socket proxy with a filtering script, you can effectively manage container access for multiple teams without needing SSH access. This ensures that each team can only interact with their own containers while using GitLab runners.