[Docker DB]Docker에서 Postgres, MongoDB, Redis 구동하기

2022. 2. 8. 23:06Dev/Etc

Postgres

psql

 

//이미지를 먼저 pull 받는다, 태그가 없으면 latest가 기본
docker pull postgres

//docker 컨테이너를 실행한다
docker run --name [컨테이너 이름] -p [컨테이너 외부에서 접근할 포트]:5432 -e POSTGRES_PASSWORD=[슈퍼유저 비밀번호] -d postgres

--name : 컨테이너 이름 설정

-p : 도커 컨테이너에 접속할 포트 설정, 뒤에 붙은 포트는 이미지가 실행 될 포트

-d : 백그라운드에서 실행

-e : 컨테이너의 환경 변수, image마다 다르다. postgres는 반드시 POSTGRES_PASSWORD 값이 필요함, 이것은 슈퍼유저 비밀번호를 설정하게 된다.

The PostgreSQL image uses several environment variables which are easy to miss. The only variable required is POSTGRES_PASSWORD, the rest are optional.

Warning: the Docker specific variables will only have an effect if you start the container with a data directory that is empty; any pre-existing database will be left untouched on container startup.

POSTGRES_PASSWORD

This environment variable is required for you to use the PostgreSQL image. It must not be empty or undefined. This environment variable sets the superuser password for PostgreSQL. The default superuser is defined by the POSTGRES_USER environment variable.

Note 1: The PostgreSQL image sets up trust authentication locally so you may notice a password is not required when connecting from localhost (inside the same container). However, a password will be required if connecting from a different host/container.

Note 2: This variable defines the superuser password in the PostgreSQL instance, as set by the initdb script during initial container startup. It has no effect on the PGPASSWORD environment variable that may be used by the psql client at runtime, as described at https://www.postgresql.org/docs/current/libpq-envars.html. PGPASSWORD, if used, will be specified as a separate environment variable.

 

라고 되어있으니 참고, 더 많은 환경 변수 설정은 아래의 링크를 타고 들어가면 확인 가능하다.

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

 

Postgres - Official Image | Docker Hub

We and third parties use cookies or similar technologies ("Cookies") as described below to collect and process personal data, such as your IP address or browser information. You can learn more about how this site uses Cookies by reading our privacy policy

hub.docker.com

 

MongoDB

mongo

//mongo 이미지를 받아온다. 여기서는 4버전을 받아와보도록 한다. 태그 생략되면 latest
docker pull mongo:4

//docker container 실행
docker run --name [컨테이너 이름] -p [컨테이너에 접속할 포트]:27017 -d mongo:4

 

Redis

redis

//redis 이미지를 pull
docker pull redis

//docker container 생성
docker run --name [컨테이너 이름] -p [컨테이너 접속 포트]:6379 -d redis

 

공통

* 컨테이너 목록 확인

//실행중인 목록
docker ps

//전체 목록
docker ps -a

 

* 컨테이너 접속

docker exec -it [컨테이너 이름 | 컨테이너 아이디]

 

* 컨테이너 정지 / 실행

docker start [컨테이너 이름 | 컨테이너 아이디]
docker stop [컨테이너 이름 | 컨테이너 아이디]

 

* 컨테이너 삭제

docker rm [컨테이너 이름 | 컨테이너 아이디]

 

* 이미지 삭제

docker image rm [이미지 이름]

 


Reference

https://judo0179.tistory.com/96

 

Docker Postgresql 설치 및 셋팅하기

리눅스 컨테이너 즉 docker는 프로세스 형태로 자원을 격리하여 사용하기 때문에 컨테이너가 삭제되면 기존에 저장되었던 데이터는 사라진다. 이를 예방하기 위해서 docker volume을 사용하거나 로

judo0179.tistory.com

https://poiemaweb.com/docker-mongodb

 

Docker를 사용하여 MongoDB 설치하고 접속하기 | PoiemaWeb

 

poiemaweb.com