본문 바로가기

Devops/Docker

[Docker] 도커로 몽고db 설치하기

몽고DB 이미지는 아래 도커 허브에 공식적으로 릴리즈 되고 있다.

 

https://hub.docker.com/_/mongo

 

 

우선 docker 이미지를 받아본다.

root@DESKTOP-GH94F8C:~# docker pull mongo
Using default tag: latest
latest: Pulling from library/mongo
bccd10f490ab: Pull complete
b00c7ff578b0: Pull complete
a1f43ab85151: Pull complete
9e72f6a5998a: Pull complete
8424336879e4: Pull complete
85a6d3c2e6c8: Pull complete
c533c21e5fb8: Pull complete
1fddf702bb73: Pull complete
Digest: sha256:0e145625e78b94224d16222ff2609c4621ff6e2c390300e4e6bf698305596792
Status: Downloaded newer image for mongo:latest
docker.io/library/mongo:latest

 

 

잘 받아졌는지 확인해본다. 755MB 의 사이즈로 나타난다.

root@DESKTOP-GH94F8C:~# docker images
REPOSITORY                      TAG       IMAGE ID       CREATED         SIZE
mongo                           latest    24041ceefc56   4 days ago      755MB
172.17.0.1:443/library/quartz   latest    b509576b225a   2 months ago    403MB
nginx                           latest    d453dd892d93   4 months ago    187MB
goharbor/redis-photon           v2.8.2    6f4498a430ca   9 months ago    121MB
goharbor/harbor-registryctl     v2.8.2    fa61a236a6d6   9 months ago    142MB
goharbor/registry-photon        v2.8.2    f80e71363231   9 months ago    79.3MB
goharbor/nginx-photon           v2.8.2    3d009028f260   9 months ago    120MB
goharbor/harbor-log             v2.8.2    2914d282d9bf   9 months ago    127MB
goharbor/harbor-jobservice      v2.8.2    40118f1568a8   9 months ago    141MB
goharbor/harbor-core            v2.8.2    0bbbd1f379fc   9 months ago    165MB
goharbor/harbor-portal          v2.8.2    3e74e0758aa4   9 months ago    127MB
goharbor/harbor-db              v2.8.2    5126635ae9f0   9 months ago    174MB
goharbor/prepare                v2.8.2    eb3cf3cdd17a   9 months ago    163MB
kindest/node                    <none>    36d37c652064   11 months ago   936MB

 

 

몽고DB 컨테이너를 실행해본다.

# 도커 네트워크 생성
root@DESKTOP-GH94F8C:~# docker network create some-network
9d75402f7ecd27fedf526e4151be1f65307879bf37756f6406a33dc879c799ff

# 컨테이너 실행
root@DESKTOP-GH94F8C:~# docker run -d --network some-network \
--name some-mongo -v ~/data:/data/db  \
-e MONGO_INITDB_ROOT_USERNAME=mongoadmin \
-e MONGO_INITDB_ROOT_PASSWORD=secret \
-p 27017:27017 mongo
3609cce273605d6e67f8c8676f781e65365352dbfadb03e36c11694dee753b36

 

컨테이너 실행 시 data 위치는 본인의 환경에 맞게 바꾼다. MONGO_INITDB_ROOT_USERNAME, MONGO_INITDB_ROOT_PASSWD 는 초기 어드민 계정이다.

 

컨테이너가 잘 떳는지 확인한다.

root@DESKTOP-GH94F8C:~# docker ps -a
CONTAINER ID   IMAGE                                COMMAND                  CREATED         STATUS                            PORTS                                                                            NAMES
3609cce27360   mongo                                "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes                      0.0.0.0:27017->27017/tcp, :::27017->27017/tcp                                    some-mongo
5eed2fe46e49   goharbor/harbor-jobservice:v2.8.2    "/harbor/entrypoint.…"   2 weeks ago     Restarting (2) 40 seconds ago                                                                                      harbor-jobservice
f0629c19aaaa   goharbor/nginx-photon:v2.8.2         "nginx -g 'daemon of…"   2 weeks ago     Exited (128) 12 minutes ago       0.0.0.0:80->8080/tcp, :::80->8080/tcp, 0.0.0.0:443->8443/tcp, :::443->8443/tcp   nginx
11fcd5a45b94   goharbor/harbor-core:v2.8.2          "/harbor/entrypoint.…"   2 weeks ago     Up 2 seconds (health: starting)                                                                                    harbor-core
8c3a0c1e5137   goharbor/harbor-db:v2.8.2            "/docker-entrypoint.…"   2 weeks ago     Exited (128) 12 minutes ago                                                                                        harbor-db
311be2cec715   goharbor/harbor-registryctl:v2.8.2   "/home/harbor/start.…"   2 weeks ago     Exited (128) 12 minutes ago                                                                                        registryctl
fac664cdeca8   goharbor/registry-photon:v2.8.2      "/home/harbor/entryp…"   2 weeks ago     Exited (128) 12 minutes ago                                                                                        registry
bc6a06e26c05   goharbor/redis-photon:v2.8.2         "redis-server /etc/r…"   2 weeks ago     Exited (128) 12 minutes ago                                                                                        redis
fbf5c2e96f18   goharbor/harbor-portal:v2.8.2        "nginx -g 'daemon of…"   2 weeks ago     Exited (128) 12 minutes ago                                                                                        harbor-portal
1e64423918c7   goharbor/harbor-log:v2.8.2           "/bin/sh -c /usr/loc…"   2 weeks ago     Up 12 minutes (healthy)           127.0.0.1:1514->10514/tcp                                                        harbor-log
b22037159a2f   kindest/node:v1.26.3                 "/usr/local/bin/entr…"   2 months ago    Up 12 minutes                                                                                                      kubernetes-worker2
e09014c15c11   kindest/node:v1.26.3                 "/usr/local/bin/entr…"   2 months ago    Up 12 minutes                                                                                                      kubernetes-worker
46179caf7a17   kindest/node:v1.26.3                 "/usr/local/bin/entr…"   2 months ago    Up 12 minutes                     0.0.0.0:30080->30080/tcp, 0.0.0.0:31000->31000/tcp, 127.0.0.1:43967->6443/tcp    kubernetes-control-plane

 

새로 설치된 몽고DB에 접속해본다. 파이썬을 이용해 접속해보았다.

import json
import pprint
import pymongo
from pymongo import MongoClient

my_client = MongoClient("mongodb://localhost:27017/", username="mongoadmin", password="secret")
print(my_client.list_database_names())

database = my_client['kindlove']
collection = database['products']
collection.delete_many({})

first_doc = {'_id': 'dial1', 'name': 'iPhone 15', 'brand': 'Apple', 'type': 'phone', 'price': 250, 'warranty_years': 1, 'available': True}
collection.insert_one(first_doc)
print(collection.find_one())

 

실행결과

['admin', 'config', 'kindlove', 'local']
{'_id': 'dial1', 'name': 'iPhone 15', 'brand': 'Apple', 'type': 'phone', 'price': 250, 'warranty_years': 1, 'available': True}

 

정상적으로 database 및 document 조회가 됨을 볼 수 있다.

 

-- The End --