외부에서 도커 컨테이너의 PostgreSQL에 연결
도커 컨테이너의 서버에 Postgresql이 있습니다. 외부, 즉 로컬 컴퓨터에서 어떻게 연결할 수 있습니까? 이를 위해 어떤 설정을 적용해야합니까?
이 방법으로 Postgres를 실행할 수 있습니다 (포트 매핑).
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres
이제 컨테이너의 포트 5432를 서버의 포트 5432에 매핑했습니다. -p <host_port>:<container_port>
이제 postgres에 액세스 할 수 있습니다.public-server-ip:5432
테스트하려면 : postgres 데이터베이스를 실행하십시오 (위의 명령).
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05b3a3471f6f postgres "/docker-entrypoint.s" 1 seconds ago Up 1 seconds 0.0.0.0:5432->5432/tcp some-postgres
컨테이너 내부로 이동하여 데이터베이스를 작성하십시오.
docker exec -it 05b3a3471f6f bash
root@05b3a3471f6f:/# psql -U postgres
postgres-# CREATE DATABASE mytest;
postgres-# \q
로컬 호스트 (도구 또는 psql 클라이언트가있는 곳)로 이동하십시오.
psql -h public-ip-server -p 5432 -U postgres
(암호 mysecretpassword)
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
mytest | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres
따라서 로컬 호스트에서 데이터베이스 (서버의 docker에서 실행중인)에 액세스하고 있습니다.
이 게시물 에서는 자세히 설명되어 있습니다.
나는 리눅스에서 실행할 수 있었다.
1 먼저 docker postgres를 실행하고 포트가 게시되어 있는지 확인하십시오. 경량이므로 알파인을 사용합니다.
sudo docker run --rm -P -p 127.0.0.1:5432:5432 -e POSTGRES_PASSWORD="1234" --name pg postgres:alpine
2 다른 터미널을 사용하고 postgres URI를 사용하여 호스트에서 데이터베이스에 액세스
psql postgresql://postgres:1234@localhost:5432/postgres
Mac 사용자의 경우 psql을 pgcli로 교체하십시오.
다음과 같이 docker exec 명령을 통해 액세스 할 수도 있습니다.
$ docker exec -it postgres-container bash
# su postgres
$ psql
또는
$ docker exec -it postgres-container psql -U postgres
이미 호스트 컴퓨터에서 postgres를 실행 중이었고 네트워크 연결을 허용하고 싶지 않았으므로 컨테이너에서 임시 postgres 인스턴스를 실행하고 단 두 줄로 데이터베이스를 만들었습니다.
# Run PostgreSQL
docker run --name postgres-container -e POSTGRES_PASSWORD=password -it -p 5433:5432 postgres
# Create database
docker exec -it postgres-container createdb -U postgres my-db
로컬 호스트에서 연결하려면 '--net host'를 추가해야합니다.
docker run --name some-postgres --net host -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres
다음을 사용하여 로컬 호스트에서 exec를 사용하지 않고 서버에 직접 액세스 할 수 있습니다.
psql -h localhost -p 5432 -U postgres
Docker 컨테이너에서 postgres와 함께 django를 사용하고 있습니다. docker-compose 파일에서 다음을 추가하십시오.
db:
image: postgres:10-alpine
environment:
- POSTGRES_DB=app
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=supersecretpassword
**ports:
- "6543:5432"**
which will add accessible port by your local machine. for myself, I connected DBeaver to it. this will prevent port clashes between your app request and local machine request. at first, I got a message saying that the port 5432 is in use (which is by django app) so I couldn't access by pgAdmin or DBeaver.
I tried to connect from localhost (mac) to a postgres container. I changed the port in the docker-compose file from 5432 to 3306 and started the container. No idea why I did it :|
Then I tried to connect to postgres via PSequel and adminer and the connection could not be established.
After switching back to port 5432 all works fine.
db:
image: postgres
ports:
- 5432:5432
restart: always
volumes:
- "db_sql:/var/lib/mysql"
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: password
POSTGRES_DB: postgres_db
This was my experience I wanted to share. Perhaps someone can make use of it.
first open the docker image for the postgres
docker exec -it <container_name>
then u will get the root --root@868594e88b53:/#
it need the database connection
psql postgresql://<username>:<databasepassword>@postgres:5432/<database>
There are good answers here but If you like to have some interface for postgres database management, you can install pgAdmin on your local computer and connect to the remote machine using its IP and the postgres exposed port (by default 5432).
For some reason 5432 port seems protected. I changed my port config from 5432:5432
to 5416:5432
and the following command worked to connect to your postgres database from outside its docker container:
psql -h localhost -p 5416 -U <my-user> -d <my-database>
In case, it is a django backend application, you can do something like this.
docker exec -it container_id python manage.py dbshell
docker ps -a
to get container ids then docker exec -it psql -U -W
'Programing' 카테고리의 다른 글
data.frame에서 그룹당 평균 (0) | 2020.06.18 |
---|---|
논리적 조건으로 data.frame 행 필터링 (0) | 2020.06.18 |
브라우저의 뷰포트를 기준으로 요소의 최상위 위치를 얻는 방법은 무엇입니까? (0) | 2020.06.18 |
base64로 이미지 파일 인코딩 (0) | 2020.06.18 |
한 클래스에서 멤버 함수와 함께 일반 std :: function 객체 사용 (0) | 2020.06.18 |