[도커 이미지]

# 이미지 다운로드

$ docker pull mongo

 

# 컨테이너 실행
$ docker run --name mongodb-container -d -p 27017:27017 mongo

 

# 컨테이너 접속

$ docker exec -it mongodb-container bash

$ mongo

 

 

[docker-compose 설치]

 

$ vi docker-compose.yml

version: '3'
services:
    mongodb:
        image: mongo
        restart: always
        ports:
            - 27017:27017
        volumes:
            - /usr/local/data/mongodb:/data/db
        container_name: mongodb
        environment:
            - MONGO_INITDB_ROOT_USERNAME=root
            - MONGO_INITDB_ROOT_PASSWORD=1234 
            - MONGO_INITDB_DATABASE=mydb

 

[.env 환경설정 분리]

 

$ vi docker-compose.yml

version: '3'
services:
    mongodb:
        image: mongo
        restart: always
        ports:
            - "${MONGO_PORT}:27017"
        volumes:
            - /usr/local/data/mongodb:/data/db
        container_name: "mongodb"
        env_file:
            - .env

$ vi .env

## Mongodb
MONGO_HOST=localhost
MONGO_PORT=27017
MONGO_INITDB_ROOT_USERNAME=root
MONGO_INITDB_ROOT_PASSWORD=1234
MONGO_INITDB_DATABASE=mydb

 

$ docker-compose up -d

 

 

[mongo db 명령어]

 

$ docker exec -it mongodb bash

>  mongo -u username -p password --authenticationDatabase admin

 

# database 리스트 확인
show dbs
show databases

# 컬렉션 리스트 확인
show collections

# document 만들기
db.user.insert({"name" : "mike", "age" : 3})

 

 

'Infra Structure > Docker' 카테고리의 다른 글

Docker image를 tar 파일로 저장  (0) 2022.10.23
[Ubuntu] Docker offline 설치  (0) 2022.10.23
Dockerfile 만들기  (0) 2022.07.31
[Docker] ubuntu 컨테이너 실행  (0) 2022.07.16
[Docker] sudo 명령없이 docker 사용  (0) 2022.07.16

+ Recent posts