Compose

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

A sample docker-compose file

---
version: "3.1"
services:
  gitlab:
    container_name: gitlab
    image: "gitlab/gitlab-ce:14.6.3-ce.0"
    restart: always
    hostname: "gitlab.jeeva.us"
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        external_url 'https://gitlab.jeeva.us'
      # Add any other gitlab.rb configuration here, each on its own line
    ports:
      - "30080:80"
      - "30443:443"
      - "30022:22"
    volumes:
      - "/docker/gitlab/config:/etc/gitlab"
      - "/docker/gitlab/logs:/var/log/gitlab"
      - "/docker/gitlab/data:/var/opt/gitlab"

  docker:
    container_name: dind
    image: docker:dind
    privileged: true
    hostname: docker
    restart: always
    environment:
      - DOCKER_TLS_CERTDIR=/certs
    ports:
      - "2376:2376"
    command: --storage-driver=overlay2
    volumes:
      - "/etc/localtime:/etc/localtime:ro"
      - "/docker/dind/certs-ca:/certs/ca"
      - "/docker/dind/certs-client:/certs/client"

  jenkins:
    container_name: jenkins
    image: "jeeva420/jenkins:with-docker"
    restart: always
    hostname: "jenkins.jeeva.us"
    environment:
      - DOCKER_CERT_PATH=/certs/client
      - DOCKER_HOST=tcp://docker:2376
      - DOCKER_TLS_VERIFY=1
      - DOCKER_TLS_CERTDIR=/certs
    ports:
      - "8080:8080"
      - "5000:5000"
    volumes:
      - "/etc/localtime:/etc/localtime:ro"
      - "/docker/jenkins:/var/jenkins_home"
      - "/docker/dind/certs-client:/certs/client:ro"

To create and start the container:

To start all the services in daemon mode:

docker-compose up -d

To build and start a specific service

docker-compose up -d gitlab

To stop a specific service

docker-compose stop -t 1 gitlab

To remove stopeed container

docker-compose rm -s -v jenkins

To Stop and remove containers, networks, images, and volumes

docker-compose down plex

To run a specific command on a running service

$ docker-compose run jenkins date
Sun 23 Jan 2022 12:09:44 PM PST