docker-compose

이동건·2022년 11월 2일

Docker

목록 보기
2/2

도커 컴포즈 파일 생성

cat docker-compose.yml

version: '3'
services:

  video-streaming:
    image: video-streaming
    build:
      context: ./video-streaming
      dockerfile: Dockerfile
    container_name: video-streaming
    ports:
     - "4000:80"
    environment:
      - PORT=80
    restart: "no"

version : 파일 형식
services : services 항목을 넣고 컨테이너 내용을 이하에 기술
video-streaming : 비디오 스트리밍 마이크로서비스를 설정
image : 이미지 이름 설정
build : 이미지를 만들 때 필요한 파라미터 설정
context : 마이크로서비스 디렉터리 설정
dockerfile : 이미지를 빌드하는 도커파일 설정
container_name : 인스턴스로 생성될 컨테이너 이름 설정
ports : 매핑할 포트를 지정. 도커의 -p 와 같은 역할
"4000:80" : 마이크로서비스의 80번 포트를 호스트 운영체제의 4000번 포트와 연계
environment : 컨테이너에 입력으로 전달할 환경변수를 설정
restart : 마이크로서비스가 비정상 종료될 경우에 재시작 여부

도커컴포즈 실행

docker-compose up --build
[+] Building 3.9s (11/11) FINISHED
 => [internal] load build definition from Dockerfile                                                                         0.0s
 => => transferring dockerfile: 195B                                                                                         0.0s
 => [internal] load .dockerignore                                                                                            0.0s
 => => transferring context: 2B                                                                                              0.0s
 => [internal] load metadata for docker.io/library/node:12.18.1-alpine                                                       3.7s
 => [internal] load build context                                                                                            0.1s
 => => transferring context: 1.15MB                                                                                          0.0s
 => [1/6] FROM docker.io/library/node:12.18.1-alpine@sha256:3af7615925ac3a000990b74cb1f98d1ab33644a895fb88a554cd9288c9da960  0.0s
 => CACHED [2/6] WORKDIR /usr/src/app                                                                                        0.0s
 => CACHED [3/6] COPY package*.json ./                                                                                       0.0s
 => CACHED [4/6] RUN npm install --only=production                                                                           0.0s
 => [5/6] COPY ./src ./src                                                                                                   0.0s
 => [6/6] COPY ./videos ./videos                                                                                             0.0s
 => exporting to image                                                                                                       0.0s
 => => exporting layers                                                                                                      0.0s
 => => writing image sha256:bba74fbf0871ecc2accd4ce041aaffb51081c5535b2275c6d492aa23013fd0ee                                 0.0s
 => => naming to docker.io/library/video-streaming                                                                           0.0s
[+] Running 2/1
 ⠿ Network example-1_default  Created                                                                                        0.0s
 ⠿ Container video-streaming  Created                                                                                        0.0s
Attaching to video-streaming
video-streaming  |
video-streaming  | > example-1@1.0.0 start /usr/src/app
video-streaming  | > node ./src/index.js
video-streaming  |
video-streaming  | Microservice online.

왼쪽은 컨테이너 이름

실행중인 컨테이너 목록 확인

docker-compose ps
NAME                COMMAND                  SERVICE             STATUS              PORTS
video-streaming     "docker-entrypoint.s…"   video-streaming     running             0.0.0.0:4000->80/tcp

앱 종료

docker-compose down
[+] Running 2/2
 ⠿ Container video-streaming  Removed                                                          0.2s
 ⠿ Network example-1_default  Removed                                                          0.1s

다중 앱 띄우기

cat docker-compose.yml
version: '3'
services:

  azure-storage:
    image: azure-storage
    build:
      context: ./azure-storage
      dockerfile: Dockerfile
    container_name: video-storage
    ports:
     - "4000:80"
    environment:
      - PORT=80
      - STORAGE_ACCOUNT_NAME=<insert your Azure storage account name here>
      - STORAGE_ACCESS_KEY=<insert your Azure storage account access key here>
    restart: "no"

  video-streaming:
    image: video-streaming
    build:
      context: ./video-streaming
      dockerfile: Dockerfile
    container_name: video-streaming
    ports:
     - "4001:80"
    environment:
      - PORT=80
      - VIDEO_STORAGE_HOST=video-storage
      - VIDEO_STORAGE_PORT=80
    restart: "no"

azure-storage : 새로운 마이크로서비스를 앱에 추가한다
image : 이미지 이름을 설정한다
container_name : 두 개의 마이크로서비스를 연결하는 컨테이너의 이름
VIDEO_STORAGE_HOST, VIDEO_STORAGE_PORT : 마이크로서비스가 video-storage 마이크로서비스에 연결하도록 설정

결과

docker-compose up --build
[+] Building 5.4s (16/16) FINISHED
 => [azure-storage internal] load build definition from Dockerfile                                                                                                                                   0.0s
 => => transferring dockerfile: 172B                                                                                                                                                                 0.0s
 => [video-streaming internal] load build definition from Dockerfile                                                                                                                                 0.0s
 => => transferring dockerfile: 172B                                                                                                                                                                 0.0s
 => [azure-storage internal] load .dockerignore                                                                                                                                                      0.0s
 => => transferring context: 2B                                                                                                                                                                      0.0s
 => [video-streaming internal] load .dockerignore                                                                                                                                                    0.0s
 => => transferring context: 2B                                                                                                                                                                      0.0s
 => [video-streaming internal] load metadata for docker.io/library/node:12.18.1-alpine                                                                                                               2.7s
 => [video-streaming 1/5] FROM docker.io/library/node:12.18.1-alpine@sha256:3af7615925ac3a000990b74cb1f98d1ab33644a895fb88a554cd9288c9da960a                                                         0.0s
 => [azure-storage internal] load build context                                                                                                                                                      0.1s
 => => transferring context: 250.74kB                                                                                                                                                                0.1s
 => [video-streaming internal] load build context                                                                                                                                                    0.0s
 => => transferring context: 98.38kB                                                                                                                                                                 0.0s
 => CACHED [azure-storage 2/5] WORKDIR /usr/src/app                                                                                                                                                  0.0s
 => CACHED [video-streaming 3/5] COPY package*.json ./                                                                                                                                               0.0s
 => CACHED [video-streaming 4/5] RUN npm install --only=production                                                                                                                                   0.0s
 => [video-streaming 5/5] COPY ./src ./src                                                                                                                                                           0.0s
 => [azure-storage] exporting to image                                                                                                                                                               0.2s
 => => exporting layers                                                                                                                                                                              0.1s
 => => writing image sha256:24abe0ff2bc81fb44a6d8c4b63b6e126940d2118e7d04fd08cfbad5721b8756e                                                                                                         0.0s
 => => naming to docker.io/library/video-streaming                                                                                                                                                   0.0s
 => => writing image sha256:ff049466b3f6ea9f815263fe98cc45bc5d7573efe05ca6b8504452360ef95926                                                                                                         0.0s
 => => naming to docker.io/library/azure-storage                                                                                                                                                     0.0s
 => [azure-storage 3/5] COPY package*.json ./                                                                                                                                                        0.0s
 => [azure-storage 4/5] RUN npm install --only=production                                                                                                                                            2.3s
 => [azure-storage 5/5] COPY ./src ./src                                                                                                                                                             0.0s
[+] Running 3/1
 ⠿ Network example-2_default  Created                                                                                                                                                                0.0s
 ⠿ Container video-streaming  Created                                                                                                                                                                0.0s
 ⠿ Container video-storage    Created                                                                                                                                                                0.1s
Attaching to video-storage, video-streaming
video-streaming  |
video-streaming  | > example-1@1.0.0 start /usr/src/app
video-streaming  | > node ./src/index.js
video-streaming  |
video-storage    |
video-storage    | > example-1@1.0.0 start /usr/src/app
video-storage    | > node ./src/index.js
video-storage    |
video-streaming  | Forwarding video requests to video-storage:80.
video-streaming  | Microservice online
video-storage    | Serving videos from Azure storage account donggun.
video-storage    | Microservice online

DB 추가

cat docker-compose.yml
version: '3'
services:

  db:
    image: mongo:4.2.8
    container_name: db
    ports:
     - "4000:27017"
    restart: always

  azure-storage:
    image: azure-storage
    build:
      context: ./azure-storage
      dockerfile: Dockerfile
    container_name: video-storage
    ports:
     - "4001:80"
    environment:
      - PORT=80
      - STORAGE_ACCOUNT_NAME=<insert your Azure storage account name here>
      - STORAGE_ACCESS_KEY=<insert your Azure storage account access key here>
    restart: "no"

  video-streaming:
    image: video-streaming
    build:
      context: ./video-streaming
      dockerfile: Dockerfile
    container_name: video-streaming
    ports:
     - "4002:80"
    environment:
      - PORT=80
      - DBHOST=mongodb://db:27017
      - DBNAME=video-streaming
      - VIDEO_STORAGE_HOST=video-storage
      - VIDEO_STORAGE_PORT=80
    depends_on:
      - db
    restart: "no"

db : MongoDB 데이터베이스 서버를 마이크로서비스 앱에 추가한다
image : 이미지명과 버전을 설정한다. 도커허브에서 공개된 몽고디비 이미지를 가져왔다.
container_name : 앱 안에서 인스턴스로 생성될 컨테이너 이름을 설정한다. 마이크로서비스는 데이터베이스에 연결할 때 이 이름을 사용한다.
ports : 몽고디비 표준 포트인 27017을 4000번 포트에 매핑한다. 4000번 포트를 통해 몽고디비 연결과 동작을 확인할 수 있다.
restart : 재시작 정책을 always로 설정했다. 비정상종료되면 자동으로 재시작된다.
DBHOST : 데이터베이스에 연결하기 위한 마이크로서비스 설정이다.
DBNAME : 데이터베이스를 사용하는 마이크로서비스 이름을 설정한다.

앱에서는 video-streaming 마이크로 서비스가 데이터베이스에 연결할 것이다.

docker-compose up --build
[+] Building 1.1s (16/16) FINISHED
 => [video-streaming internal] load build definition from Dockerfile                           0.0s
 => => transferring dockerfile: 32B                                                            0.0s
 => [azure-storage internal] load build definition from Dockerfile                             0.0s
 => => transferring dockerfile: 32B                                                            0.0s
 => [video-streaming internal] load .dockerignore                                              0.0s
 => => transferring context: 2B                                                                0.0s
 => [azure-storage internal] load .dockerignore                                                0.0s
 => => transferring context: 2B                                                                0.0s
 => [azure-storage internal] load metadata for docker.io/library/node:12.18.1-alpine           0.9s
 => [video-streaming 1/5] FROM docker.io/library/node:12.18.1-alpine@sha256:3af7615925ac3a000  0.0s
 => [azure-storage internal] load build context                                                0.0s
 => => transferring context: 127B                                                              0.0s
 => [video-streaming internal] load build context                                              0.0s
 => => transferring context: 127B                                                              0.0s
 => CACHED [azure-storage 2/5] WORKDIR /usr/src/app                                            0.0s
 => CACHED [video-streaming 3/5] COPY package*.json ./                                         0.0s
 => CACHED [video-streaming 4/5] RUN npm install --only=production                             0.0s
 => CACHED [video-streaming 5/5] COPY ./src ./src                                              0.0s
 => CACHED [azure-storage 3/5] COPY package*.json ./                                           0.0s
 => CACHED [azure-storage 4/5] RUN npm install --only=production                               0.0s
 => CACHED [azure-storage 5/5] COPY ./src ./src                                                0.0s
 => [video-streaming] exporting to image                                                       0.0s
 => => exporting layers                                                                        0.0s
 => => writing image sha256:a638a90a0e1a213e86d856038bef37d7bc9110523a31728e758e3b5c575deec3   0.0s
 => => naming to docker.io/library/azure-storage                                               0.0s
 => => writing image sha256:14a3b5ccd508a12aa29e36bf0a529e35068131240b58fe52f980e60f6fda863d   0.0s
 => => naming to docker.io/library/video-streaming                                             0.0s
[+] Running 1/0
 ⠿ Container video-streaming  Created                                                          0.0s
Attaching to db, video-storage, video-streaming
db               | 2022-11-04T04:10:56.470+0000 I  CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
db               | 2022-11-04T04:10:56.472+0000 W  ASIO     [main] No TransportLayer configured during NetworkInterface startup
db               | 2022-11-04T04:10:56.473+0000 I  CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=1ff75266083d
db               | 2022-11-04T04:10:56.473+0000 I  CONTROL  [initandlisten] db version v4.2.8
db               | 2022-11-04T04:10:56.473+0000 I  CONTROL  [initandlisten] git version: 43d25964249164d76d5e04dd6cf38f6111e21f5f
db               | 2022-11-04T04:10:56.473+0000 I  CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.1.1  11 Sep 2018
db               | 2022-11-04T04:10:56.473+0000 I  CONTROL  [initandlisten] allocator: tcmalloc
db               | 2022-11-04T04:10:56.473+0000 I  CONTROL  [initandlisten] modules: none
db               | 2022-11-04T04:10:56.473+0000 I  CONTROL  [initandlisten] build environment:
db               | 2022-11-04T04:10:56.473+0000 I  CONTROL  [initandlisten]     distmod: ubuntu1804
db               | 2022-11-04T04:10:56.473+0000 I  CONTROL  [initandlisten]     distarch: aarch64
db               | 2022-11-04T04:10:56.473+0000 I  CONTROL  [initandlisten]     target_arch: aarch64
db               | 2022-11-04T04:10:56.473+0000 I  CONTROL  [initandlisten] options: { net: { bindIp: "*" } }
db               | 2022-11-04T04:10:56.473+0000 I  STORAGE  [initandlisten]
db               | 2022-11-04T04:10:56.473+0000 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
db               | 2022-11-04T04:10:56.473+0000 I  STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
db               | 2022-11-04T04:10:56.473+0000 I  STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=7481M,cache_overflow=(file_max=0M),session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress],
video-storage    |
video-storage    | > example-1@1.0.0 start /usr/src/app
video-storage    | > node ./src/index.js
video-storage    |
db               | 2022-11-04T04:10:56.496+0000 I  STORAGE  [initandlisten] WiredTiger message [1667535056:496681][1:0xffff879f9450], txn-recover: Set global recovery timestamp: (0, 0)
db               | 2022-11-04T04:10:56.505+0000 I  RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
db               | 2022-11-04T04:10:56.513+0000 I  STORAGE  [initandlisten] Timestamp monitor starting
db               | 2022-11-04T04:10:56.516+0000 I  CONTROL  [initandlisten]
db               | 2022-11-04T04:10:56.516+0000 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
db               | 2022-11-04T04:10:56.516+0000 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
db               | 2022-11-04T04:10:56.516+0000 I  CONTROL  [initandlisten]
db               | 2022-11-04T04:10:56.517+0000 I  STORAGE  [initandlisten] createCollection: admin.system.version with provided UUID: d51dbbda-53e8-4c42-82e1-dae3e3f44655 and options: { uuid: UUID("d51dbbda-53e8-4c42-82e1-dae3e3f44655") }
db               | 2022-11-04T04:10:56.525+0000 I  INDEX    [initandlisten] index build: done building index _id_ on ns admin.system.version
db               | 2022-11-04T04:10:56.525+0000 I  SHARDING [initandlisten] Marking collection admin.system.version as collection version: <unsharded>
db               | 2022-11-04T04:10:56.525+0000 I  COMMAND  [initandlisten] setting featureCompatibilityVersion to 4.2
db               | 2022-11-04T04:10:56.525+0000 I  SHARDING [initandlisten] Marking collection local.system.replset as collection version: <unsharded>
db               | 2022-11-04T04:10:56.526+0000 I  STORAGE  [initandlisten] Flow Control is enabled on this deployment.
db               | 2022-11-04T04:10:56.526+0000 I  SHARDING [initandlisten] Marking collection admin.system.roles as collection version: <unsharded>
db               | 2022-11-04T04:10:56.526+0000 I  STORAGE  [initandlisten] createCollection: local.startup_log with generated UUID: a7b52d42-551e-4f65-85bb-ba2ecb715d73 and options: { capped: true, size: 10485760 }
db               | 2022-11-04T04:10:56.534+0000 I  INDEX    [initandlisten] index build: done building index _id_ on ns local.startup_log
db               | 2022-11-04T04:10:56.534+0000 I  SHARDING [initandlisten] Marking collection local.startup_log as collection version: <unsharded>
db               | 2022-11-04T04:10:56.535+0000 I  FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
db               | 2022-11-04T04:10:56.536+0000 I  SHARDING [LogicalSessionCacheRefresh] Marking collection config.system.sessions as collection version: <unsharded>
db               | 2022-11-04T04:10:56.537+0000 I  NETWORK  [listener] Listening on /tmp/mongodb-27017.sock
db               | 2022-11-04T04:10:56.537+0000 I  NETWORK  [listener] Listening on 0.0.0.0
db               | 2022-11-04T04:10:56.537+0000 I  NETWORK  [listener] waiting for connections on port 27017
db               | 2022-11-04T04:10:56.538+0000 I  CONTROL  [LogicalSessionCacheReap] Sessions collection is not set up; waiting until next sessions reap interval: config.system.sessions does not exist
db               | 2022-11-04T04:10:56.538+0000 I  STORAGE  [LogicalSessionCacheRefresh] createCollection: config.system.sessions with provided UUID: 45994c14-ce39-484e-a08e-6fd9f0f14a0e and options: { uuid: UUID("45994c14-ce39-484e-a08e-6fd9f0f14a0e") }
db               | 2022-11-04T04:10:56.546+0000 I  INDEX    [LogicalSessionCacheRefresh] index build: done building index _id_ on ns config.system.sessions
db               | 2022-11-04T04:10:56.555+0000 I  INDEX    [LogicalSessionCacheRefresh] index build: starting on config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 } using method: Hybrid
db               | 2022-11-04T04:10:56.555+0000 I  INDEX    [LogicalSessionCacheRefresh] build may temporarily use up to 200 megabytes of RAM
db               | 2022-11-04T04:10:56.555+0000 I  INDEX    [LogicalSessionCacheRefresh] index build: collection scan done. scanned 0 total records in 0 seconds
db               | 2022-11-04T04:10:56.555+0000 I  INDEX    [LogicalSessionCacheRefresh] index build: inserted 0 keys from external sorter into index in 0 seconds
db               | 2022-11-04T04:10:56.557+0000 I  INDEX    [LogicalSessionCacheRefresh] index build: done building index lsidTTLIndex on ns config.system.sessions
video-storage    | Serving videos from Azure storage account <insert your Azure storage account name here>.
video-storage    | Microservice online
video-streaming  |
video-streaming  | > example-1@1.0.0 start /usr/src/app
video-streaming  | > node ./src/index.js
video-streaming  |
video-streaming  | Forwarding video requests to video-storage:80.
video-streaming  | (node:18) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
video-streaming  | (node:18) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
db               | 2022-11-04T04:10:56.898+0000 I  NETWORK  [listener] connection accepted from 172.21.0.4:39172 #1 (1 connection now open)
db               | 2022-11-04T04:10:56.899+0000 I  NETWORK  [conn1] received client metadata from 172.21.0.4:39172 conn1: { driver: { name: "nodejs", version: "3.3.2" }, os: { type: "Linux", name: "linux", architecture: "arm64", version: "5.10.124-linuxkit" }, platform: "Node.js v12.18.1, LE, mongodb-core: 3.3.2" }
video-streaming  | Microservice online.
video-streaming  | Microservice listening, please load the data file db-fixture/videos.json into your database before testing this microservice.
db               | 2022-11-04T04:10:57.001+0000 I  SHARDING [ftdc] Marking collection local.oplog.rs as collection version: <unsharded>

RabbitMQ 붙이기

cat docker-compose.yml
version: '3'
services:

  db:
    image: mongo:4.2.8
    container_name: db
    ports:
      - "4000:27017"
    restart: always

  rabbit:
    image: rabbitmq:3.8.5-management
    container_name: rabbit
    ports:
      - "5672:5672"
      - "15672:15672"
    expose:
      - "5672"
      - "15672"
    restart: always

  video-streaming:
    image: video-streaming
    build:
      context: ./video-streaming
      dockerfile: Dockerfile-dev
    container_name: video-streaming
    volumes:
      - /tmp/video-streaming/npm-cache:/root/.npm:z
      - ./video-streaming/src:/usr/src/app/src:z
      - ./video-streaming/videos:/usr/src/app/videos:z
    ports:
      - "4001:80"
    environment:
      - PORT=80
      - RABBIT=amqp://guest:guest@rabbit:5672
      - NODE_ENV=development
    depends_on:
      - rabbit
    restart: "no"

  history:
    image: history
    build:
      context: ./history
      dockerfile: Dockerfile-dev
    container_name: history
    volumes:
      - /tmp/history/npm-cache:/root/.npm:z
      - ./history/src:/usr/src/app/src:z
    ports:
     - "4002:80"
    environment:
      - PORT=80
      - RABBIT=amqp://guest:guest@rabbit:5672
      - DBHOST=mongodb://db:27017
      - DBNAME=history
      - NODE_ENV=development
    depends_on:
      - db
      - rabbit
    restart: "no"

rabbit : 래빗MQ 서버를 호스팅하는 컨테이너 정의
image : 래빗MQ 이미지의 management 버전. 대시보드가 포함된다.
container_name : 래빗MQ 서버를 연결할 때 사용. 컨테이너 이름 설정.
ports : 호스트 운영체제와 컨테이너 간의 포트를 매핑
expose : 호스트 운영체제와 컨테이너 간의 포트를 매핑
restart : 래빗 MQ네 문제가 생기면 자동으로 재시작

RABBIT : 래빗MQ에 연결할 URI를 설정한다.
rabbit : 히스토리 마이크로서비스는 이제 rabbit 컨테이너에 의존한다.

profile
코드를 통한 세계의 창조

0개의 댓글