본문 바로가기

Web Dev/Docker

DockerCompose 파일 docker-compose.yaml 작성하는 방법 with MongoDB

반응형

 

<mongoshell -명령어>

  • use [DB]
  • show collections
  •  db.[collections].find();

 mongoshell이 작동을 안해서 

mongo-compass로 확인해 봤는데 

mongoDB서버는 잘 작동 되고 있다 . 

목적은 docker에 mongo DB를 설치하는 방법과 

그렇게되면 BackEnd 서버와 DB  서버 두개가 한 docker안에서 

작동 될 예정인데 그거를 일일이  port forwarding을 하는게 아닌 

docker-compose 파일을 작성하여 두개를 연결해주는 방법을 알아볼 것이다. 

 일단

2023.02.17 - [일상 공부] - [MongoDB] Window OS에 MongoDB 설치 방법[error체크]

1. mongoDB용 Dockerfile을 하나 더 생성한다 

2. Dockerfile.mongo 작성해준다.  

FROM mongo:5

3.docker-compose.yaml

.yaml or .yml 둘 다 상관 없다고 한다 (통일 좀 해주지) 

yaml 파일은 간격에 취약하다 띄어쓰기를 잘해줘야한단다

그래서 extension 힘을 빌려보기로한다. 얼마나 도움 되는지 모르겠지

이거 도움 안될려나 

요령은 일단 작성 후 계속 천천히 탭키를 치다 보면 에러가 없어지는 구간까지 이동시켜주면 된다.

version: '3.7'

services:
  my-backend:
    build:
      context: .
      dockerfile: Dockerfile

  my-database:
    build:
      context: .
      dockerfile: Dockerfile.mongo

코드 위치를 동일한 위치에 놓는게 중요하다

반응형

+port forwarding 추가 

version: '3.7'

services:
  my-backend:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 3000:3000

  my-database:
    build:
      context: .
      dockerfile: Dockerfile.mongo
    ports:
      - 27017:27017

yaml 파일이 있는 공간에서 

docker-compose build

docker-compose build
 docker-compose build
[+] Building 32.0s (9/16)
 => [back-my-backend 1/6] FROM docker.io/library/node:14@sha256:1b5300317e95ed8bb2a1c25003f57e52400ce7af1e2e1efd9f524072  26.5s
 => => sha256:61f659024e304142fe11bb0455df68c5526e9bd397bffc0af9947e8d09078b28 90.18MB / 191.92MB                         26.4s 
 => => sha256:b762bebe7db7bfcbd3db996b540900e5bf7222d3a259ae4089f4e0bacc580dd6 35.28MB / 35.28MB                          17.2s 
 => => extracting sha256:e97ef50ee5a8f962aeac812883159a9ffe3f00819fcb446f0b1fc87d7060e16e                                  0.2s
 => => extracting sha256:dfb1477a1a0e7cfeb697ce3d2aae0e31f6af42409c4581a3c23e0df0b832da6f                                  0.2s 
 => => sha256:2aa033b30c593f1735fe656dcaf9e4b17fdebf607c35402dc8376721a00bcec1 2.29MB / 2.29MB                            18.5s 
 => => sha256:690c8610fc7ec569cb8d6d50d5353815a43bffde0a0d099a454e5167fccbef24 452B / 452B                                18.7s 
 => => extracting sha256:838447eff6a7c0b7b49fd5248bd06fe00feedb8f836c346bf3a58c0516a9502d                                  1.4s
 => [back-my-backend internal] load build context                                                                          0.1s
 => => transferring context: 82.59kB                                                                                       0.0s 
 => [back-my-database 1/1] FROM docker.io/library/mongo:5@sha256:6cf671de7c85493d6dde9aafef383570161809336d6c41f431ac3a6  26.3s
 => => resolve docker.io/library/mongo:5@sha256:6cf671de7c85493d6dde9aafef383570161809336d6c41f431ac3a692155ece9           0.1s
 => => sha256:200589b2fd195d9128ca163764e375ecc3c4df49ae03c1b0eb39c2b2cc943eb0 7.94kB / 7.94kB                             0.0s
 => => sha256:a8fae999d4720fc72cc39e83c5ebfdc1602cca6701ab0569f7c1a1f3c9715c77 2.20kB / 2.20kB                             0.0s 
 => => sha256:6cf671de7c85493d6dde9aafef383570161809336d6c41f431ac3a692155ece9 1.04kB / 1.04kB                             0.0s 
 => => sha256:7608715873ec5c02d370e963aa9b19a149023ce218887221d93fe671b3abbf58 28.58MB / 28.58MB                          25.3s 
 => => sha256:aa7638a79c68d650ad78bf1c04cbc9ef160606e02e19fcdfc357d3a49d9f3dfb 1.83kB / 1.83kB                            19.4s
 => => sha256:6edb8e32447e402814b615208f1a93870bff3994e2b654ae16c438879bc31277 8.35MB / 8.35MB                            22.0s 
 => => sha256:9b078a16e980d806afa564ded9aaef5b7cf74b44a75880db13e1912297a6bc0c 1.26MB / 1.26MB                            22.6s 
 => => sha256:a09083ae98273683297e023dfe39760a10a453891715f75c451db8f0916beecd 149B / 149B                                23.0s 
 => => sha256:6d64b6b8a202c58544a68188edb02828e522b2d84d35a1a91962fc6f5a03bb06 1.45kB / 1.45kB                            23.3s 
 => => sha256:8e34d62e5cec276034d5d75f5ab5952d86c070d81f6f2885b0a9231bc3b3011e 258B / 258B                                23.5s
 => => sha256:2703ba5c18c1e234bae9c2b2c5502d53d9f015a2d1132b085aa573ffd64a7477 13.63MB / 205.51MB                         26.2s 
 => => sha256:580c82e7b61ba3a9769cbf220ca3aab5afe935f0de2d25cf6bb9ea4d29d68b4a 4.96kB / 4.96kB                            25.6s 
 => => extracting sha256:7608715873ec5c02d370e963aa9b19a149023ce218887221d93fe671b3abbf58

빌드 했으니 이제 up

docker-compose up
docker-compose up
[+] Running 3/3
 - Network back_default          Created                                                                                   0.1s 
 - Container back-my-database-1  Created                                                                                   0.2s 
 - Container back-my-backend-1   Created                                                                                   0.2s 
Attaching to back-my-backend-1, back-my-database-1
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:49.879+00:00"},"s":"I",  "c":"NETWORK",  "id":4915701, "ctx":"-","msg":"Initialized wire specification","attr":{"spec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":13},"outgoing":{"minWireVersion":0,"maxWireVersion":13},"isInternalClient":true}}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:49.881+00:00"},"s":"I",  "c":"CONTROL",  "id":23285,   "ctx":"-","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:49.882+00:00"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:49.882+00:00"},"s":"I",  "c":"NETWORK",  "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:49.883+00:00"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:49.883+00:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationDonorService","ns":"config.tenantMigrationDonors"}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:49.883+00:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationRecipientService","ns":"config.tenantMigrationRecipients"}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:49.883+00:00"},"s":"I",  "c":"CONTROL",  "id":5945603, "ctx":"main","msg":"Multi threading initialized"}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:49.884+00:00"},"s":"I",  "c":"CONTROL",  "id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":1,"port":27017,"dbPath":"/data/db","architecture":"64-bit","host":"1e270f883b07"}}    
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:49.884+00:00"},"s":"I",  "c":"CONTROL",  "id":23403,   "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"5.0.14","gitVersion":"1b3b0073a0b436a8a502b612f24fb2bd572772e5","openSSLVersion":"OpenSSL 1.1.1f  31 Mar 2020","modules":[],"allocator":"tcmalloc","environment":{"distmod":"ubuntu2004","distarch":"x86_64","target_arch":"x86_64"}}}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:49.884+00:00"},"s":"I",  "c":"CONTROL",  "id":51765,   "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Ubuntu","version":"20.04"}}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:49.884+00:00"},"s":"I",  "c":"CONTROL",  "id":21951,   "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{"net":{"bindIp":"*"}}}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:49.885+00:00"},"s":"I",  "c":"STORAGE",  "id":22297,   "ctx":"initandlisten","msg":"Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem","tags":["startupWarnings"]}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:49.885+00:00"},"s":"I",  "c":"STORAGE",  "id":22315,   "ctx":"initandlisten","msg":"Opening WiredTiger","attr":{"config":"create,cache_size=3418M,session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),builtin_extension_config=(zstd=(compression_level=6)),file_manager=(close_idle_time=600,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress,compact_progress],"}}
back-my-backend-1   | 
back-my-backend-1   | > back@1.0.0 dev /myfolder
back-my-backend-1   | > nodemon index.js
back-my-backend-1   |
back-my-backend-1   | [nodemon] 2.0.20
back-my-backend-1   | [nodemon] to restart at any time, enter `rs`
back-my-backend-1   | [nodemon] watching path(s): *.*
back-my-backend-1   | [nodemon] watching extensions: js,mjs,json
back-my-backend-1   | [nodemon] starting `node index.js`
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.478+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1676861870:478830][1:0x7fa3bddf6c80], txn-recover: [WT_VERB_RECOVERY_ALL] Set global recovery timestamp: (0, 0)"}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.478+00:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"initandlisten","msg":"WiredTiger message","attr":{"message":"[1676861870:478894][1:0x7fa3bddf6c80], txn-recover: [WT_VERB_RECOVERY_ALL] Set global oldest timestamp: (0, 0)"}}
back-my-backend-1   | Example app listening on port 3000
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.509+00:00"},"s":"I",  "c":"STORAGE",  "id":4795906, "ctx":"initandlisten","msg":"WiredTiger opened","attr":{"durationMillis":624}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.509+00:00"},"s":"I",  "c":"RECOVERY", "id":23987,   "ctx":"initandlisten","msg":"WiredTiger recoveryTimestamp","attr":{"recoveryTimestamp":{"$timestamp":{"t":0,"i":0}}}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.580+00:00"},"s":"I",  "c":"STORAGE",  "id":22262,   "ctx":"initandlisten","msg":"Timestamp monitor starting"}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.602+00:00"},"s":"W",  "c":"CONTROL",  "id":22120,   "ctx":"initandlisten","msg":"Access control is not enabled for the database. Read and write access to data and configuration is unrestricted","tags":["startupWarnings"]}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.602+00:00"},"s":"W",  "c":"CONTROL",  "id":22178,   "ctx":"initandlisten","msg":"/sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'","tags":["startupWarnings"]}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.602+00:00"},"s":"I",  "c":"STORAGE",  "id":20320,   "ctx":"initandlisten","msg":"createCollection","attr":{"namespace":"admin.system.version","uuidDisposition":"provided","uuid":{"uuid":{"$uuid":"cf3a3fd9-91c7-4ade-bdb6-f4f36f76a64b"}},"options":{"uuid":{"$uuid":"cf3a3fd9-91c7-4ade-bdb6-f4f36f76a64b"}}}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.645+00:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"initandlisten","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"admin.system.version","index":"_id_","commitTimestamp":null}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.645+00:00"},"s":"I",  "c":"REPL",     "id":20459,   "ctx":"initandlisten","msg":"Setting featureCompatibilityVersion","attr":{"newVersion":"5.0"}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.645+00:00"},"s":"I",  "c":"NETWORK",  "id":4915702, "ctx":"initandlisten","msg":"Updated wire specification","attr":{"oldSpec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":13},"outgoing":{"minWireVersion":0,"maxWireVersion":13},"isInternalClient":true},"newSpec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":13,"maxWireVersion":13},"outgoing":{"minWireVersion":13,"maxWireVersion":13},"isInternalClient":true}}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.645+00:00"},"s":"I",  "c":"NETWORK",  "id":4915702, "ctx":"initandlisten","msg":"Updated wire specification","attr":{"oldSpec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":13,"maxWireVersion":13},"outgoing":{"minWireVersion":13,"maxWireVersion":13},"isInternalClient":true},"newSpec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":13,"maxWireVersion":13},"outgoing":{"minWireVersion":13,"maxWireVersion":13},"isInternalClient":true}}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.645+00:00"},"s":"I",  "c":"STORAGE",  "id":5071100, "ctx":"initandlisten","msg":"Clearing temp directory"}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.646+00:00"},"s":"I",  "c":"CONTROL",  "id":20536,   "ctx":"initandlisten","msg":"Flow Control is enabled on this deployment"}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.647+00:00"},"s":"I",  "c":"FTDC",     "id":20625,   "ctx":"initandlisten","msg":"Initializing full-time diagnostic data capture","attr":{"dataDirectory":"/data/db/diagnostic.data"}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.647+00:00"},"s":"I",  "c":"STORAGE",  "id":20320,   "ctx":"initandlisten","msg":"createCollection","attr":{"namespace":"local.startup_log","uuidDisposition":"generated","uuid":{"uuid":{"$uuid":"25eb9c3f-e487-4c9a-9cd9-d5b57d7eb53a"}},"options":{"capped":true,"size":10485760}}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.691+00:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"initandlisten","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"local.startup_log","index":"_id_","commitTimestamp":null}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.692+00:00"},"s":"I",  "c":"REPL",     "id":6015317, "ctx":"initandlisten","msg":"Setting new configuration state","attr":{"newState":"ConfigReplicationDisabled","oldState":"ConfigPreStart"}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.693+00:00"},"s":"I",  "c":"CONTROL",  "id":20712,   "ctx":"LogicalSessionCacheReap","msg":"Sessions collection is not set up; waiting until next sessions reap interval","attr":{"error":"NamespaceNotFound: config.system.sessions does not exist"}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.693+00:00"},"s":"I",  "c":"STORAGE",  "id":20320,   "ctx":"LogicalSessionCacheRefresh","msg":"createCollection","attr":{"namespace":"config.system.sessions","uuidDisposition":"generated","uuid":{"uuid":{"$uuid":"bfcac209-295c-4f1b-8c08-dd69e11ea5ea"}},"options":{}}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.693+00:00"},"s":"I",  "c":"NETWORK",  "id":23015,   "ctx":"listener","msg":"Listening on","attr":{"address":"/tmp/mongodb-27017.sock"}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.693+00:00"},"s":"I",  "c":"NETWORK",  "id":23015,   "ctx":"listener","msg":"Listening on","attr":{"address":"0.0.0.0"}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.693+00:00"},"s":"I",  "c":"NETWORK",  "id":23016,   "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.757+00:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"LogicalSessionCacheRefresh","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"config.system.sessions","index":"_id_","commitTimestamp":null}}
back-my-database-1  | {"t":{"$date":"2023-02-20T02:57:50.757+00:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"LogicalSessionCacheRefresh","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"config.system.sessions","index":"lsidTTLIndex","commitTimestamp":null}}
 docker ps
CONTAINER ID   IMAGE              COMMAND                  CREATED          STATUS          PORTS                      NAMES
19d579a94f7e   back-my-backend    "docker-entrypoint.s…"   35 seconds ago   Up 33 seconds   0.0.0.0:3000->3000/tcp     back-my-backend-1
1e270f883b07   back-my-database   "docker-entrypoint.s…"   35 seconds ago   Up 33 seconds   0.0.0.0:27017->27017/tcp   back-my-database-1

docker ps 명령어로 검색해보면 두개의 image가 구동되는걸 볼 수 있다. 

Mongo-compass에서도 docker 안에 있는 mongoDB 접속이 잘 된다 .

반응형