1. 前提安装好docker
2. 安装PostgreSQL
docker pull postgres:9.6
Dockerfile
指定版本9.6,使用阿里镜像
FROM postgres:9.6
MAINTAINER Kristian Larsson <kristian@spritelink.net>
ENV DEBIAN_FRONTEND=noninteractive
RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak && \
echo "deb http://mirrors.aliyun.com/debian/ buster main non-free contrib" >/etc/apt/sources.list && \
echo "deb http://mirrors.aliyun.com/debian-security buster/updates main" >>/etc/apt/sources.list && \
echo "deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib" >>/etc/apt/sources.list && \
echo "deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib" >>/etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/debian-security buster/updates main" >>/etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib" >>/etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib" >>/etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib" >>/etc/apt/sources.list
RUN apt-get update \
&& apt-get install -qy postgresql-9.6-ip4r \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
指定版本9.6.15,使用阿里镜像
FROM postgres:9.6.15
MAINTAINER Kristian Larsson <kristian@spritelink.net>
ENV DEBIAN_FRONTEND=noninteractive
RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak && \
echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian/ buster main non-free contrib" >/etc/apt/sources.list && \
echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main" >>/etc/apt/sources.list && \
echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main non-free contrib" >>/etc/apt/sources.list && \
echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main non-free contrib" >>/etc/apt/sources.list && \
echo "deb-src http://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main" >>/etc/apt/sources.list && \
echo "deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ buster main non-free contrib" >>/etc/apt/sources.list && \
echo "deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main non-free contrib" >>/etc/apt/sources.list && \
echo "deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main non-free contrib" >>/etc/apt/sources.list
RUN apt-get update \
&& apt-get install -qy postgresql-9.6-ip4r \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
指定版本9.6.15,不修改镜像
FROM postgres:9.6.15
MAINTAINER Kristian Larsson <kristian@spritelink.net>
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update \
&& apt-get install -qy postgresql-9.6-ip4r \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
3. 创建容器
docker build -t postgresql-ip4r:9.6 .
docker run --name postgres96 -e POSTGRES_PASSWORD=123456! -p 54321:5432 -d postgres:9.6
docker run --name postgres1 -e POSTGRES_PASSWORD=password -p 54321:5432 -d postgres:9.6
docker run --name postgres96-ip4r -e POSTGRES_PASSWORD=123456! -p 54321:5432 -d postgresql-ip4r:9.6
docker run --name postgres96-ip4r -e POSTGRES_PASSWORD=123456 -p 54321:5432 -d postgresql-ip4r:9.6
docker build -t postgresql-ip4r:9.6.15 .
docker run --name postgres96-ip4r -e POSTGRES_PASSWORD=123456! -p 54321:5432 -d postgresql-ip4r:9.6.15
docker run --name postgres96-ip4r -e POSTGRES_PASSWORD=123456 -p 54321:5432 -d postgresql-ip4r:9.6.15
docker run --name postgres96-ip4r -e POSTGRES_PASSWORD=ezcloud123miege -p 54321:5432 -d postgresql-ip4r:9.6.15
解释:
run,创建并运行一个容器;
–name,指定创建的容器的名字;
-e POSTGRES_PASSWORD=password,设置环境变量,指定数据库的登录口令为password;
-p 54321:5432,端口映射将容器的5432端口映射到外部机器的54321端口;
-d postgres:9.4,指定使用postgres:9.4作为镜像。
4. 验证结果
docker ps -a
5. 连接数据库
psql -U postgres -h 192.168.100.172 -p 54321
注意: postgres镜像默认的用户名为postgres, 登陆口令为创建容器是指定的值。