Django开发总结-(三)Docker安装Postgresql
Docker安装Postgresql
文章目录
1. 安装docker
卸载旧版本docker
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
安装系统依赖包
(venv) [[email protected] ~]$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
添加软件源,sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
(venv) [[email protected] ~]$ sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
更新 yum 缓存sudo yum makecache fast
(venv) [[email protected] ~]$ sudo yum makecache fast
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/2): docker-ce-stable/x86_64/primary_db | 23 kB 00:00:00
(2/2): docker-ce-stable/x86_64/updateinfo | 55 B 00:00:00
Metadata Cache Created
安装 Docker-ce:
(venv) [[email protected] ~]$ sudo yum -y install docker-ce
启动 Docker 后台服务
(venv) [[email protected] ~]$ sudo systemctl start docker
测试运行 hello-world
(venv) [[email protected] ~]$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
2. 配置docker用户组
创建docker用户组
(venv) [[email protected] ~]$ sudo groupadd docker
groupadd: group 'docker' already exists
应用用户加入docker用户组
(venv) [[email protected] ~]$ sudo usermod -aG docker ${USER}
设置开启自启
(venv) [[email protected] ~]$ sudo chkconfig docker on
Note: Forwarding request to 'systemctl enable docker.service'.
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
重启docker服务
(venv) [[email protected] ~]$ sudo systemctl restart docker
退出当前SSH登录再从新登入,以上配置会才生效
3. 配置阿里云镜像加速器
登录阿里云docker镜像站,点击右上方管理中心,注册用户后,获取到自己的镜像加速器地址
针对Docker客户端版本大于 1.10.0 的用户,可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://********.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
4. 查找Postgresql镜像
- 登录阿里云docker镜像站
- 查询postgres,根据下载量、收藏量、认证情况查找符合自己需求的镜像
- 或者通过命令方式查找
[[email protected] ~]$ docker search postgres
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
postgres The PostgreSQL object-relational database sy… 6269 [OK]
sameersbn/postgresql 140 [OK]
paintedfox/postgresql A docker image for running Postgresql. 77 [OK]
orchardup/postgresql https://github.com/orchardup/docker-postgres… 48 [OK]
kiasaki/alpine-postgres PostgreSQL docker image based on Alpine Linux 44 [OK]
centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 37
bitnami/postgresql Bitnami PostgreSQL Docker Image 31 [OK]
postgrest/postgrest REST API for any Postgres database 18
begriffs/postgrest Moved to https://hub.docker.com/r/postgrest/… 16 [OK]
circleci/postgres The PostgreSQL object-relational database sy… 16
crunchydata/crunchy-postgres Crunchy PostgreSQL is an open source, unmodi… 16
centos/postgresql-94-centos7 PostgreSQL is an advanced Object-Relational … 15
schickling/postgres-backup-s3 Backup PostgresSQL to S3 (supports periodic … 14 [OK]
wrouesnel/postgres_exporter Postgres metrics exporter for Prometheus. 12
postdock/postgres PostgreSQL server image, can work in master … 11 [OK]
centos/postgresql-10-centos7 PostgreSQL is an advanced Object-Relational … 8
centos/postgresql-95-centos7 PostgreSQL is an advanced Object-Relational … 6
blacklabelops/postgres Postgres Image for Atlassian Applications 4 [OK]
cfcommunity/postgresql https://github.com/cloudfoundry-community/po… 0
ansibleplaybookbundle/postgresql-apb An APB which deploys RHSCL PostgreSQL 0 [OK]
ansibleplaybookbundle/rds-postgres-apb An APB that deploys an RDS instance of Postg… 0 [OK]
postgresqlaasdockerhub/docker-postgresql94 docker hub build for postgresql 94 0 [OK]
fredboat/postgres PostgreSQL 10.0 used in FredBoat's docker-co… 0
postgresqlaasdockerhub/docker-postgresql96 docker hub build for postgresql 96 0 [OK]
manageiq/postgresql Container with PostgreSQL and built on CentO… 0 [OK]
5. 获取到镜像地址后,拉取镜像
[[email protected] ~]$ docker pull postgres
Using default tag: latest
latest: Pulling from library/postgres
6ae821421a7d: Pull complete
060245e5c056: Pull complete
d6c2e60cfc1c: Pull complete
38d23c24a9df: Pull complete
9998068fb35e: Pull complete
2bb2385a4bb9: Pull complete
2ad14c51497d: Pull complete
77519615ed40: Pull complete
aef70912f86d: Pull complete
74cab331a5d3: Pull complete
afdf28fbe06f: Pull complete
46c6c5ea887a: Pull complete
ae19128049b6: Pull complete
3968885a0fc0: Pull complete
Digest: sha256:50479afd704ca2bfbec54ddcd0655f7f35b0992434f7fdc1f2ca6e075f887d99
Status: Downloaded newer image for postgres:latest
[[email protected] ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
postgres latest f97a959a7d9c 11 days ago 312MB
hello-world latest fce289e99eb9 8 weeks ago 1.84kB
6. 启动pgsql服务
创建pgsql
数据挂在卷目录,实现数据持久化,就是容器被删除,数据依然保留在主机上,创建新的容器时可以直接挂在到旧的数据卷上,数据不会丢失
[[email protected] ~]$ mkdir -p ~/pgdata
[[email protected] ~]$ ll
total 0
drwxrwxr-x. 2 vagrant vagrant 6 Feb 27 13:36 pgdata
drwxrwxrwx. 1 vagrant vagrant 0 Feb 25 13:52 software
drwxrwxrwx. 1 vagrant vagrant 0 Feb 26 12:41 src
启动pgsql
容器,使用--name
设置容器名称,-v
设置目录挂载,-p
命令设置暴露端口,-e
设置容器内的环境变量,-d
设置启动的镜像名及版本
[[email protected] ~]$ docker run --name pgsql -v ~/pgdata:/var/lib/postgresql/data -p 5432:5432 -e POSTGRES_PASSWORD=postgres -d postgres:latest
28b904f3c4854c733333b840332979b02847c9dd9b6a6056ee931029b25fce5d
使用命令docker ps
查看后台启动的容器
[[email protected] ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
28b904f3c485 postgres:latest "docker-entrypoint.s…" 21 seconds ago Up 20 seconds 0.0.0.0:5432->5432/tcp pgsql
[[email protected] ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
28b904f3c485 postgres:latest "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:5432->5432/tcp pgsql
00f27826830b hello-world "/hello" 24 hours ago Exited (0) 24 hours ago kind_greider frosty_yalow
使用命令docker exec -it pgsql bash
进入容器内部
[[email protected] ~]$ docker exec -it pgsql bash
[email protected]:/# cd /var/lib/postgresql/data/
[email protected]:/var/lib/postgresql/data# ls
base pg_commit_ts pg_hba.conf pg_logical pg_notify pg_serial pg_stat pg_subtrans pg_twophase pg_wal postgresql.auto.conf postmaster.opts
global pg_dynshmem pg_ident.conf pg_multixact pg_replslot pg_snapshots pg_stat_tmp pg_tblspc PG_VERSION pg_xact postgresql.conf postmaster.pid
[email protected]:/var/lib/postgresql/data# exit
exit
[[email protected] ~]$
通过修改配置文件,可以配置数据库相关参数,postgresql.conf文件为参数配置文件,pg_hba.conf文件监听配置文件,修改完成后可以通过docker restart containerID
命令重启容器。
[[email protected] ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
28b904f3c485 postgres:latest "docker-entrypoint.s…" 23 minutes ago Up 23 minutes 0.0.0.0:5432->5432/tcp pgsql
[[email protected] ~]$ docker restart 28b904f3c485
28b904f3c485
[[email protected] ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
28b904f3c485 postgres:latest "docker-entrypoint.s…" 23 minutes ago Up 4 seconds 0.0.0.0:5432->5432/tcp pgsql