[Setting | Ubuntu] Docker 기본환경 구성 및 설치

도커를 활용하기 위한 기본 환경 구성 설정법과 docker compose를 활용한 서비스 실행법을 정리한다.


작성일 : 2022-06-08
OS : Ubuntu 22.04 LTS
Docker : Docker version 20.10.17

1> docker compose 설치

# 설치가능 버전을 확인
sudo apt-cache policy docker-compose
# --------------------------------------------

docker-compose:
  Installed: 1.29.2-1
  Candidate: 1.29.2-1
  Version table:
 *** 1.29.2-1 500
        500 http://archive.ubuntu.com/ubuntu jammy/universe amd64 Packages
        100 /var/lib/dpkg/status
# --------------------------------------------

# 특정버전의 docker-compose 설치
sudo apt install docker-compose=1.29.2-1 -y

2> data 폴더 생성

data 폴더를 만들고 docker-compose.yml 에서 컨테이너 볼륨을 생성

# root 권한으로 실행
sudo su
mkdir /data

3> .env

sudo vi /data/.env
## ----- FOR DEV_OPS
## gitlab
GITLAB_IP=172.20.0.101
GITLAB_PORT=9999
## jenkins
JENKINS_IP=172.20.0.102
JENKINS_PORT=8888
## sonarqube
SONARQUBE_IP=172.20.0.103
SONARQUBE_PORT=7777
## mediawiki
MEDIAWIKI_IP=172.20.0.104
MEDIAWIKI_PORT=6666
## redmine
REDMINE_IP=172.20.0.105
REDMINE_PORT=5555

## ----- FOR SERVICE
## postgresql
POSTGRESQL_VER=14.3
POSTGRESQL_ID=opendocs
POSTGRESQL_PW=qwer1234
POSTGRESQL_IP=172.20.0.201
POSTGRESQL_PORT=5432
## mariadb
MARIADB_VER=10.8.3
MARIADB_ROOT_PW=qwer1234
MARIADB_IP=172.20.0.202
MARIADB_PORT=3306

4> docker-compose.networks.yml

sudo vi /data/docker-compose.networks.yml
version: '3.8'

networks:
  opdnet:
    driver: bridge
    ipam:
      driver: default
      config:
      - subnet: 172.20.0.0/21
        gateway: 172.20.0.1

5> docker-compose.volumes.yml

sudo vi /data/docker-compose.volumes.yml
version: '3.8'

volumes:
## ----- FOR DEV_OPS
  gitlab_config:
    name: gitlab_config
  gitlab_data:
    name: gitlab_data
  gitlab_logs:
    name: gitlab_logs
  jenkins_home:
    name: jenkins_home
  sonarqube_data:
    name: sonarqube_data
  sonarqube_logs:
    name: sonarqube_logs
  sonarqube_extensions:
    name: sonarqube_extensions
  mediawiki_data:
    name: mediawiki_data
  redmine_data:
    name: redmine_data

## ----- FOR SERVICE
  postgresql_data:
    name: postgresql_data
  mariadb_data:
    name: mariadb_data

6> docker-compose.services.yml

sudo vi /data/docker-compose.services.yml
version: '3.8'

services:
## ----- FOR DEV_OPS
    gitlab:
        image: gitlab-opendocs
        build:
            context: .
            dockerfile: ./gitlab/Dockerfile
        hostname: gitlab
        container_name: gitlab
        networks:
            opdnet:
                ipv4_address: ${GITLAB_IP}
        ports:
        - ${GITLAB_PORT}:80
        - '10022:22'
        restart: always
        volumes:
        - gitlab_config:/etc/gitlab:rw
        - gitlab_logs:/var/log/gitlab:rw
        - gitlab_data:/var/opt/gitlab:rw

    jenkins:
        image: jenkins-opendocs
        build:
            context: .
            dockerfile: ./jenkins/Dockerfile
        user: root
        hostname: jenkins
        container_name: jenkins
        networks:
            opdnet:
                ipv4_address: ${JENKINS_IP}
        ports:
        - ${JENKINS_PORT}:8080
        - '50000:50000'
        environment:
        - JAVA_OPTS='-Dhudson.model.DownloadService.noSignatureCheck=true'
        volumes:
        - jenkins_home:/var/jenkins_home:rw

    sonarqube:
        image: sonarqube-opendocs
        build:
            context: .
            dockerfile: ./sonarqube/Dockerfile
        hostname: sonarqube
        container_name: sonarqube
        networks:
            opdnet:
                ipv4_address: ${SONARQUBE_IP}
        ports:
        - ${SONARQUBE_PORT}:9000
        volumes:
        - sonarqube_data:/opt/sonarqube/data:rw
        - sonarqube_logs:/opt/sonarqube/logs:rw
        - sonarqube_extensions:/opt/sonarqube/extensions:rw

    mediawiki:
        image: mediawiki-opendocs
        build:
            context: .
            dockerfile: ./mediawiki/Dockerfile
        hostname: mediawiki
        container_name: mediawiki
        networks:
            opdnet:
                ipv4_address: ${MEDIAWIKI_IP}
        ports:
        - ${MEDIAWIKI_PORT}:80
        - '8443:8443'
        environment:
        - MEDIAWIKI_HOST=wiki.opendocs.co.kr
        - ALLOW_EMPTY_PASSWORD=yes
        - MEDIAWIKI_DATABASE_HOST=${MARIADB_IP}
        - MEDIAWIKI_DATABASE_PORT_NUMBER=${MARIADB_PORT}
        - MEDIAWIKI_DATABASE_USER=mediawiki
        - MEDIAWIKI_DATABASE_PASSWORD=qwer1234
        - MEDIAWIKI_DATABASE_NAME=mediawiki
        volumes:
        - mediawiki_data:/bitnami/mediawiki:rw
        depends_on:
        - mariadb
        links:
        - mariadb

    redmine:
        image: redmine-opendocs
        build:
            context: .
            dockerfile: ./redmine/Dockerfile
        hostname: redmine
        container_name: redmine
        networks:
            opdnet:
                ipv4_address: ${REDMINE_IP}
        ports:
        - ${REDMINE_PORT}:3000
        - '8444:8443'
        environment:
        - ALLOW_EMPTY_PASSWORD=yes
        - REDMINE_DATABASE_HOST=${MARIADB_IP}
        - REDMINE_DATABASE_PORT_NUMBER=${MARIADB_PORT}
        - REDMINE_DATABASE_USER=redmine
        - REDMINE_DATABASE_PASSWORD=qwer1234
        - REDMINE_DATABASE_NAME=redmine
        volumes:
        - redmine_data:/bitnami/redmine:rw
        depends_on:
        - mariadb
        links:
        - mariadb


## ----- FOR SERVICE
    postgresql:
        image: postgres:${POSTGRESQL_VER}
        hostname: postgresql
        container_name: postgresql
        networks:
            opdnet:
                ipv4_address: ${POSTGRESQL_IP}
        ports:
        - ${POSTGRESQL_PORT}:5432
        environment:
            POSTGRES_USER: ${POSTGRESQL_ID}
            POSTGRES_PASSWORD: ${POSTGRESQL_PW}
        volumes:
        - postgresql_data:/var/lib/postgresql/data:rw
    mariadb:
        image: mariadb:${MARIADB_VER}
        hostname: mariadb
        container_name: mariadb
        networks:
            opdnet:
                ipv4_address: ${MARIADB_IP}
        ports:
        - ${MARIADB_PORT}:3306
        environment:
            MARIADB_ROOT_PASSWORD: ${MARIADB_ROOT_PW}
        volumes:
        - mariadb_data:/var/lib/mysql:rw
        - ./mariadb/query/:/docker-entrypoint-initdb.d/:rw

7> Dockerfile

7-1> gitlab

sudo mkdir /data/gitlab
sudo vi /data/gitlab/Dockerfile
FROM gitlab/gitlab-ce:14.9.5-ce.0

# RUN
# COPY

7-2> jenkins

sudo mkdir /data/jenkins
sudo vi /data/jenkins/Dockerfile
FROM jenkins/jenkins:2.354

# RUN
# COPY

7-3> sonarqube

sudo mkdir /data/sonarqube
sudo vi /data/sonarqube/Dockerfile
FROM sonarqube:9.4-community

# RUN
# COPY

7-4> mediawiki

sudo mkdir /data/mediawiki
sudo vi /data/mediawiki/Dockerfile
FROM bitnami/mediawiki:1.37.2

# RUN
# COPY

7-5> redmine

sudo mkdir /data/redmine
sudo vi /data/redmine/Dockerfile
FROM bitnami/redmine:5.0.1

# RUN
# COPY

7-6> mariadb

sudo mkdir /data/mariadb
sudo mkdir /data/mariadb/query
sudo vi /data/mariadb/query/init.sql
CREATE USER IF NOT EXISTS root@localhost IDENTIFIED BY 'qwer1234';
SET PASSWORD FOR root@localhost = PASSWORD('qwer1234');
GRANT ALL ON *.* TO root@localhost WITH GRANT OPTION;

CREATE USER IF NOT EXISTS root@'%' IDENTIFIED BY 'qwer1234';
SET PASSWORD FOR root@'%' = PASSWORD('qwer1234');
GRANT ALL ON *.* TO root@'%' WITH GRANT OPTION;

CREATE USER IF NOT EXISTS mediawiki@'%' IDENTIFIED BY 'qwer1234';
SET PASSWORD FOR mediawiki@'%' = PASSWORD('qwer1234');

CREATE USER IF NOT EXISTS redmine@'%' IDENTIFIED BY 'qwer1234';
SET PASSWORD FOR redmine@'%' = PASSWORD('qwer1234');

CREATE DATABASE IF NOT EXISTS mediawiki;
GRANT ALL ON mediawiki.* TO mediawiki@'%';

CREATE DATABASE IF NOT EXISTS redmine;
GRANT ALL ON redmine.* TO redmine@'%';

8> docker-compose up & down

# root 권한으로 실행
sudo su
# yml파일이 있는 폴더에서 실행
cd /data
# docker compose up
docker-compose -f docker-compose.networks.yml -f docker-compose.volumes.yml -f docker-compose.services.yml up -d
# docker compose down
docker-compose -f docker-compose.networks.yml -f docker-compose.volumes.yml -f docker-compose.services.yml down
# docker compose down & delete volume
docker-compose -f docker-compose.networks.yml -f docker-compose.volumes.yml -f docker-compose.services.yml down --volumes
# log
docker-compose -f docker-compose.networks.yml -f docker-compose.volumes.yml -f docker-compose.services.yml logs
# build
docker-compose -f docker-compose.networks.yml -f docker-compose.volumes.yml -f docker-compose.services.yml up -d --build

9> 추가설정

9-1> mediawiki

sudo vi /var/lib/docker/volumes/mediawiki_data/_data/LocalSettings.php
----------------------------------------------------
# 변경전
$wgServer = '//localhost';
# 변경후
$wgServer = '//wiki.opendocs.co.kr';
----------------------------------------------------
# 컨테이너 확인
sudo docker ps -a
# 컨테이터 재시작
sudo docker container restart [CONTAINER_ID]

10> docker-compose.*.yml 이 수정되었을 경우

docker-compose up -d [서비스_이름]

11> Dockerfile 이 수정되었을 경우

docker-compose up -d --build [서비스_이름]