We will always want to use docker compose when we running services.
There is many reasons why, but on this page we focusing on the principles of good docker compose file with the company’s specific conventions.
Let's look on an example of docker compose file of jenkins:
version: '3.8'
services:
jenkins:
image: jenkins/jenkins:latest-jdk21
container_name: jenkins
restart: always
privileged: true
user: root
ports:
- 9080:8080
- 50000:50000
volumes:
- /srv/jenkins/jenkins_home:/var/jenkins_home
- /var/run/docker.sock:/var/run/docker.sock
labels:
- "traefik.enable=true"
- "traefik.http.routers.jenkins.rule=Host(`jenkins.verisoft.io`)"
- "traefik.http.routers.jenkins.entrypoints=websecure"
- "traefik.http.routers.jenkins.tls.certresolver=myresolver"
- "traefik.http.services.jenkins.loadbalancer.server.port=8080"
networks:
- generalWeb
networks:
generalWeb:
external: true
Key Points
-
Where to start?
- Go to the official service GitHub/Site and take the official docker compose yaml from there
-
Unique Service Name:
- Each service (e.g.,
jenkins) must have a unique name. - Note: Some services (like PostgreSQL) might appear in multiple configurations, so we will use something like
jenkins-PostgreSQL.
- Each service (e.g.,
-
Volumes:
- All volumes follow the pattern:
/srv/service-name/store-name - Example in the file:
/srv/jenkins/jenkins_home.
- All volumes follow the pattern:
-
Docker Socket Volume:
- The
/var/run/docker.sockvolume is mounted to allow the container to interact with the host's Docker daemon. - Why keep it? It enables the container to control Docker commands on the host (useful for scenarios like dynamic container creation or integration with orchestration tools).
- The
-
Traefik Labels (5 Required):
traefik.enable=true– Always set.traefik.http.routers.{service-name}.rule=Host('{service-name}.verisoft.io')traefik.http.routers.{service-name}.entrypoints=websecuretraefik.http.routers.{service-name}.tls.certresolver=myresolvertraefik.http.services.{service-name}.loadbalancer.server.port={service-port}
ℹ️ Info: Replace all
{service-name}and{service-port}with the actual service name and port
-
Network:
- All services must connect to the external network
generalWeb. - This ensures communication with Traefik and proper routing of user requests.
- All services must connect to the external network
-
Running:
- After navigating to the docker compose folder, just use:
docker compose up -d - It's recommended to open the docker compose logs to see that everything is started as expected!
- After navigating to the docker compose folder, just use:
ℹ️ Info:
-d or --detach- Detached mode: Run containers in the background