docker-compose-mongo.yml
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_ROOT_PASSWORD}
volumes:
- ${MONGO_STORAGE}:/data/db
- ./:/app
working_dir: /app
hostname: mongo-host
ports:
- 27017:27017
networks:
- backend
networks:
backend:
driver: bridge
external: true
prod.env 환경 설정 파일
MONGO_ROOT_PASSWORD=example
MONGO_STORAGE=/home/ec2-user/mongo-storage
start-mongo.sh
docker-compose -f docker-compose-mongo.yml --env-file $1.env up -d
처음 한 번 backend 네트워크 생성
docker network create backend
./start-mongo.sh prod
[ec2-user@ip-172-31-28-80 docker]$ ./start-mongo.sh prod
WARN[0000] /home/ec2-user/docker/docker-compose-mongo.yml: `version` is obsolete
[+] Running 1/1
✔ Container docker-mongo-1 Started
개발 환경일 경우 db 조회 쉽게하기위해 mongo-express 설치
docker-compose-mongo-exp.yml
version: '3.1'
services:
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGO_ROOT_PASSWORD}
ME_CONFIG_BASICAUTH_USERNAME: root
ME_CONFIG_BASICAUTH_PASSWORD: ${MONGO_ROOT_PASSWORD}
ME_CONFIG_MONGODB_URL: mongodb://root:${MONGO_ROOT_PASSWORD}@mongo-host:27017/
networks:
- backend
networks:
backend:
driver: bridge
external: true
ME_CONFIG_BASICAUTH_USERNAME 이 mongo-express WEB 접속 인증이다. mongo db 와 동일하게 했다. (나중에 추가된듯)
start-mongo-exp.sh
docker-compose -f docker-compose-mongo-exp.yml --env-file $1.env up -d
테스트 ./start-mongo-exp.sh prod
[ec2-user@ip-172-31-28-80 docker]$ ./start-mongo-exp.sh prod
WARN[0000] /home/ec2-user/docker/docker-compose-mongo-exp.yml: `version` is obsolete
[+] Running 9/9
✔ mongo-express Pulled 6.3s
✔ c6b39de5b339 Pull complete 0.4s
✔ 0c0a7ac4815a Pull complete 2.6s
✔ 97ac9df737b5 Pull complete 2.7s
✔ d7a8660c6370 Pull complete 2.7s
✔ 1426d6918b38 Pull complete 2.8s
✔ 1f82042b6a59 Pull complete 2.8s
✔ bc2c7645d47a Pull complete 5.8s
✔ 394e3f5d7287 Pull complete 5.8s
WARN[0006] Found orphan containers ([docker-mongo-1]) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
[+] Running 1/1
✔ Container docker-mongo-express-1 Started 1.1s
[ec2-user@ip-172-31-28-80 docker]$
접속 확인.. 8081 포트 외부 접속 허용하고(귀찮아서 임시로 80포트로 연결하고 테스트 함) 브라우저로 접속하면 인증창이 뜨고 페이지가 열린다..
리눅스 재부팅시 mongo와 mongo-express가 재시작되는 것을 확인했는데. restart: always 를 no로 바뀌면 mongo-express는 자동 시작을 하지 않도록 할 수 있다