PostgreSQL 定制Docker镜像
描述
基于postgres镜像创建Dockerfile,定制镜像,添加vim,构建镜像。
一.基于postgres:12.14构建支持vim的镜像
容器内部需要vim,所以定制镜像,安装vim。
1.构建镜像
Dockerfile
FROM postgres:12.14
RUN set -eux \
&& apt-get update \
&& apt-get install -y --no-install-recommends vim \
&& rm -rf /var/lib/apt/lists/*根据当前目录下的Dockerfile文件构建镜像
# 根据当前目录下的Dockerfile文件构建镜像
docker build -t postgres_test:12.14-vim .
# 通过代理构建
# docker build --tag postgres-vi --build-arg http_proxy=http://proxy_host:proxy_port/ .
# 打标签
docker tag postgres_test:12.14-vim swr.cn-east-2.myhuaweicloud.com/common-server/postgres:12.14-vim
# 上传镜像仓库
docker push swr.cn-east-2.myhuaweicloud.com/common-server/postgres:12.14-vim2.启动容器
# 创建目录
bash -c "mkdir /data/postgres/{data,var_lib_postgresql_temp,var_lib_postgresql_data} -p"
# 启动容器
docker run -itd --name postgres \
-p 5432:5432 \
--cpus 2 -m 4G \
--restart=unless-stopped \
--memory-swappiness=0 \
--log-opt max-size=512m \
--log-opt max-file=3 \
-l basic.hostname=$(hostname) \
-l basic.ip=$(hostname -I | awk '{print $1}') \
-v /etc/hosts:/etc/hosts \
-v /etc/localtime:/etc/localtime \
-v /data/postgres/data:/data \
-v /data/postgres/var_lib_postgresql_data:/var/lib/postgresql/data \
-v /data/postgres/var_lib_postgresql_temp:/var/lib/postgresql/temp \
-e POSTGRES_PASSWORD=postgres \
swr.cn-east-2.myhuaweicloud.com/common-server/postgres:12.14-vim3.问题
遇到启动失败的情况
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
user@002:/$ docker logs -f --tail 100 postgres
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted
ls: cannot access '/docker-entrypoint-initdb.d/': Operation not permitted解决方法:
权限不足,于是通过在docker run时 添加参数 --privileged 问题解决
docker run -itd --name postgres \
-p 5432:5432 \
--cpus 2 -m 4G \
--restart=unless-stopped \
--memory-swappiness=0 \
--log-opt max-size=512m \
--log-opt max-file=3 \
-l basic.hostname=$(hostname) \
-l basic.ip=$(hostname -I | awk '{print $1}') \
--privileged \
-v /etc/hosts:/etc/hosts \
-v /etc/localtime:/etc/localtime \
-v /data/postgres/data:/data \
-v /data/postgres/var_lib_postgresql_data:/var/lib/postgresql/data \
-v /data/postgres/var_lib_postgresql_temp:/var/lib/postgresql/temp \
-e POSTGRES_PASSWORD=postgres \
swr.cn-east-2.myhuaweicloud.com/common-server/postgres:12.14-vim二.基于postgres:14.4构建支持vim的镜像
1.构建镜像
Dockerfile
FROM postgres:14.4
RUN set -eux \
&& apt-get update \
&& apt-get install -y --no-install-recommends vim \
&& rm -rf /var/lib/apt/lists/*Dockerfile优化,更新国内源安装vim
FROM postgres:14.4
RUN set -eux \
&& sed -i s@/deb.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list \
&& apt-get update \
&& apt-get install -y --no-install-recommends vim \
&& rm -rf /var/lib/apt/lists/*构建
# 构建镜像
docker build -t postgres_test:14.4-vim .
# 通过代理
# docker build --tag postgres_test:14.4-vim --build-arg http_proxy=http://proxy_host:proxy_port/ .
# 打标签
docker tag postgres_test:14.4-vim swr.cn-east-2.myhuaweicloud.com/common-server/postgres:14.4-vim
# 上传镜像仓库
docker push swr.cn-east-2.myhuaweicloud.com/common-server/postgres:14.4-vim2.启动
# 初始化目录
# 之前的数据库目录 postgres_14 ,部署两个数据库时,目录添加版本号,区分数据库
mkdir /data/postgres_14/{data,var_lib_postgresql_temp,var_lib_postgresql_data} -p
# 启动
# 修改名称,端口,挂载目录,镜像名称
docker run -itd \
--name postgres_14 \
-p 15432:5432 \
--cpus 2 -m 4G \
--restart=unless-stopped \
--memory-swappiness=0 \
--log-opt max-size=512m \
--log-opt max-file=3 \
-l basic.hostname=$(hostname) \
-l basic.ip=$(hostname -I | awk '{print $1}') \
-v /etc/hosts:/etc/hosts \
-v /etc/localtime:/etc/localtime \
-v /data/postgres_14/data:/data \
-v /data/postgres_14/var_lib_postgresql_data:/var/lib/postgresql/data \
-v /data/postgres_14/var_lib_postgresql_temp:/var/lib/postgresql/temp \
-e POSTGRES_PASSWORD=postgres \
swr.cn-east-2.myhuaweicloud.com/common-server/postgres:14.4-vim3.问题
docker build 构建速度慢,安装vim超时
xxx@localhost:/tmp/test$ docker build -t postgres_test:14.4-vim .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM postgres:14.4
14.4: Pulling from library/postgres
1efc276f4ff9: Pull complete
66c520df917d: Pull complete
5b124e6748c9: Pull complete
1a06bb042d01: Pull complete
e3849c675ec5: Pull complete
d3b2eaf1435b: Pull complete
074399829dc9: Pull complete
6feb085525d8: Pull complete
02f61377c93d: Pull complete
d639033f0429: Pull complete
1a040507c35b: Pull complete
aa2ec30a462d: Pull complete
11209ef4fb4e: Pull complete
Digest: sha256:9ceb24f8c5f15c053d973a3610866f473690875dc13eb3282b45302189321040
Status: Downloaded newer image for postgres:14.4
---> e09e90144645
Step 2/2 : RUN set -eux && apt-get update && apt-get install -y --no-install-recommends vim && rm -rf /var/lib/apt/lists/*
---> Running in 4ae50231e39e
+ apt-get update
Get:1 http://apt.postgresql.org/pub/repos/apt bullseye-pgdg InRelease [91.7 kB]
Get:2 http://apt.postgresql.org/pub/repos/apt bullseye-pgdg/14 amd64 Packages [2,577 B]
Get:3 http://apt.postgresql.org/pub/repos/apt bullseye-pgdg/main amd64 Packages [269 kB]
Get:4 http://deb.debian.org/debian bullseye InRelease [116 kB]
Get:5 http://deb.debian.org/debian-security bullseye-security InRelease [48.4 kB]
Err:5 http://deb.debian.org/debian-security bullseye-security InRelease
Connection timed out [IP: 146.75.114.132 80]
Get:6 http://deb.debian.org/debian bullseye-updates InRelease [44.1 kB]
Get:7 http://deb.debian.org/debian bullseye/main amd64 Packages [8,183 kB]
Ign:7 http://deb.debian.org/debian bullseye/main amd64 Packages
Get:8 http://deb.debian.org/debian bullseye-updates/main amd64 Packages [14.6 kB]
Get:7 http://deb.debian.org/debian bullseye/main amd64 Packages [8,183 kB]
Ign:7 http://deb.debian.org/debian bullseye/main amd64 Packages
Get:7 http://deb.debian.org/debian bullseye/main amd64 Packages [11.1 MB]
Ign:7 http://deb.debian.org/debian bullseye/main amd64 Packages
Ign:7 http://deb.debian.org/debian bullseye/main amd64 Packages
Ign:7 http://deb.debian.org/debian bullseye/main amd64 Packages
Err:7 http://deb.debian.org/debian bullseye/main amd64 Packages
Connection timed out [IP: 146.75.114.132 80]
Fetched 538 kB in 5min 37s (1,594 B/s)
Reading package lists...
W: Failed to fetch http://deb.debian.org/debian-security/dists/bullseye-security/InRelease Connection timed out [IP: 146.75.114.132 80]
W: Failed to fetch http://deb.debian.org/debian/dists/bullseye/main/binary-amd64/Packages Connection timed out [IP: 146.75.114.132 80]
W: Some index files failed to download. They have been ignored, or old ones used instead.
+ apt-get install -y --no-install-recommends vim
Reading package lists...
Building dependency tree...
Reading state information...
E: Unable to locate package vim
The command '/bin/sh -c set -eux && apt-get update && apt-get install -y --no-install-recommends vim && rm -rf /var/lib/apt/lists/*' returned a non-zero code: 100解决方法:
修改apt源为阿里源
# sed -i s@/deb.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.listDockerfile
FROM postgres:14.4
RUN set -eux \
&& sed -i s@/deb.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list \
&& apt-get update \
&& apt-get install -y --no-install-recommends vim \
&& rm -rf /var/lib/apt/lists/*