# docker + postgres
###### tags: `docker` `環境工具`
[ToC]
## 安裝
https://www.docker.com/products/personal
windows 過程中需要再另外安裝 WSL2 套件

直接去訪問 https://aka.ms/wsl2kernel
下載,安裝升級後點 Restart 重啟 docker
## doker 內建教學
這邊可以跳過
```
# Clone
docker run --name repo alpine/git clone https://github.com/docker/getting-started.git
docker cp repo:/git/getting-started/ .
# Build
cd getting-started
docker build -t docker101tutorial .
# Run
docker run -d -p 80:80 --name docker-tutorial docker101tutorial
# save and share your image
docker tag docker101tutorial {userName}/docker101tutorial
docker push {userName}/docker101tutorial
```
## 安狀並啟用 postgres
[docker-hub-postgres](https://hub.docker.com/_/postgres)
[gitlab-安裝PostgreSQL](https://morosedog.gitlab.io/docker-20190505-docker12/)
```bash=
docker pull postgres
docker volume create --name postgresql-data
docker run -d -p 5432:5432 --name timpostgres --restart always -v postgresql-data:/var/lib/postgresql/data -e POSTGRES_PASSWORD=pd123 postgres
```
-d :後台執行 Container ,並返回ID
-p 5432:5432 :將 Container 的 5432 Port 映射到主機的 5432 Port (前面代表主機,後面代表容器)
-name timpostgres :將 Container 取名為 timpostgres
-restart always :如果 container 遇到例外的情況被 stop 掉,例如是重新開機,docker 會試著重新啟動此 container
-v postgresql-data:/var/lib/postgresql/data :使用剛建立的volume,postgresql-data 掛載到 Container 的 /var/lib/postgresql/data。
postgres :指定安裝的鏡像postgres
登入帳號: postgres
登入密碼: pd123

## pgAdmin 4
[官網](https://www.pgadmin.org/docs/pgadmin4/latest/container_deployment.html)
```
docker pull dpage/pgadmin4
docker volume create --name pgadmin-data
docker run -d -p 5432:5432 --name timpostgres --restart always -v postgresql-data:/var/lib/pgadmin/data -e POSTGRES_PASSWORD=pd123 postgres
```
## 教學文章
https://morosedog.gitlab.io/docker-20190323-docker1/