赞
踩
宿主机是ubuntu 22.04版本
ubuntu宿主机上安装docker,参见官方文档https://docs.docker.com/engine/install/ubuntu/, docker-ce是社区版 docker-ee是企业版
1、检查Docker是否安装
root@ODS1SPGOFSDEV:~# docker
Command 'docker' not found, but can be installed with:
snap install docker # version 24.0.5, or
apt install docker.io # version 24.0.5-0ubuntu1~22.04.1
apt install podman-docker # version 3.4.4+ds1-1ubuntu1.22.04.2
See 'snap info docker' for additional versions.
root@ODS1SPGOFSDEV:~#
2、查看Docker各个版本,也可以参见https://download.docker.com/linux/ubuntu/dists/jammy/pool/stable/amd64/
root@ODS1SPGOFSDEV:~# snap info docker
...
channels:
latest/stable: 24.0.5 2024-02-01 (2915) 136MB -
latest/candidate: 24.0.5 2024-02-01 (2915) 136MB -
latest/beta: 24.0.5 2023-10-30 (2915) 136MB -
latest/edge: 24.0.5 2024-01-26 (2920) 136MB -
core18/stable: 20.10.17 2023-03-13 (2746) 146MB -
core18/candidate: ↑
core18/beta: ↑
core18/edge: ↑
root@ODS1SPGOFSDEV:~#
3、设置 Docker的apt仓库
# Add Docker's official GPG key:
apt-get update
apt-get install ca-certificates curl
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update
4、安装docker20版本
# List the available versions:
apt-cache madison docker-ce | awk '{ print $3 }'
# Select the desired version and install:
VERSION_STRING=5:20.10.24~3-0~ubuntu-jammy
apt-get install docker-ce=$VERSION_STRING docker-ce-cli=$VERSION_STRING containerd.io docker-buildx-plugin docker-compose-plugin
#Verify that the Docker Engine installation is successful by running the hello-world image.
docker run hello-world
docker rmi -f hello-world
#Verify that the Docker is enable to startup while OS Startup
systemctl is-enabled docker
Docker环境安装postgresql 15.6
步骤1:下载postgresql 15.6的docker镜像,首先看有没有postgresql的docker镜像,再看有没有postgresql 15.6的docker镜像,可以到https://hub.docker.com/_/postgres/tags页面查找
root@ODS1SPGOFSDEV:~# docker search postgresql --no-trunc --filter is-official=true
root@ODS1SPGOFSDEV:~# docker pull postgres:15.6
步骤2:查看postgresql 15.6的docker镜像是否下载到了本机
root@ODS1SPGOFSDEV:~# docker images
步骤3:查看postgresql 15.6的docker镜像信息,比如端口port和变量PGDATA的信息,比如查到"ExposedPorts": {“5432/tcp”: {}}和"PGDATA=/var/lib/postgresql/data"
root@ODS1SPGOFSDEV:~# docker inspect postgres:15.6
步骤4:使用postgresql 15.6的docker镜像运行docker,一些参数可能需要修改到本机的信息比如变量PGDATA的信息,运行命令可以参考https://hub.docker.com/_/postgres
root@ODS1SPGOFSDEV:~# docker run -d --name postgresql15.6 --restart=always -e POSTGRES_PASSWORD=Dai2013! -e PGDATA=/pgdata -p 5432:5432 -v /var/lib/docker/volumes/pg_wal/_data:/pgdata postgres:15.6
**备注1:**必须要加-p 5432:5432,前一个5432表示宿主机(本机)的端口,后一个5432表示容器内部的端口,一定要把容器内部的端口映射到宿主机上,否则只是容器内部通信,我们连宿主机就无法连上
不映射的情况下,看到结果如下。当然如果如果把-p 5432:5432改成的话–network=host,这样也行,不过就是宿主机和容器公共网络,也就不再需要做端口映射了
root@ODS1SPGOFSDEV:/# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6b1285cdf987 postgres:15.6 "docker-entrypoint.s…" 2 hours ago Up 40 minutes 5432/tcp postgresql15.6
映射的情况下,看到结果如下
root@ODS1SPGOFSDEV:/# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d3842f39b5f9 postgres:15.6 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp postgresql15.6
**备注2:**加上-v 来绑定宿主机和容器内部的目录,这个/pgdata就是自定义的docker里面的postgresql的PGDATA环境变量值,而没有使用docker里面默认的PGDATA变量值PGDATA=/var/lib/postgresql/data,这个/var/lib/docker/volumes/pg_wal/_data就是我宿主机(本机)挂载的卷,这样我在宿主机(本机)的目录/var/lib/docker/volumes/pg_wal/_data就是docker容器里面的目录/pgdata1,宿主机没有/pgdata1这个目录,容器里面也没有/var/lib/docker/volumes/pg_wal/_data这个目录,详情见下面的实验结果
如果宿主机目录是/pgdata/data,docker image中PGDATA变量是/var/lib/postgresql/data,我们只是想把宿主机目录/pgdata/data绑定到docker目录/var/lib/postgresql/data而不想更换docker image中PGDATA变量值/var/lib/postgresql/data的话,那么直接执行如下就行
root@ODS1SPGOFSDEV:~# docker run -d --name postgresql15.6 --restart=always -e POSTGRES_PASSWORD=Dai2013! -p 5432:5432 -v /pgdata/data:/var/lib/postgresql/data postgres:15.6
**备注3:**发现这个docker的postgresql的pg_hba.conf文件最后面有一行host all all all scram-sha-256这条信息,所以不再需要额外加host all all 0.0.0.0/0 md5这种信息,外部的程序还是可以连接postgresql不会报错connection failed: FATAL: no pg_hba.conf entry for host “IP Address”, user “XX”, database “postgres”, SSL off。
步骤5:查看刚刚运行的docker的日志
root@ODS1SPGOFSDEV:~# docker logs postgresql15.6
docker logs 后面可以是docker名称postgresql15.6(docker run -d --name postgresql15.6 …),也可以是docker id,但是不能是docker image名称postgres:15.6
docker run -v /var/lib/docker/volumes/pg_wal/_data:/pgdata1时,/var/lib/docker/volumes/pg_wal/_data表示宿主机的目录,/pgdata1表示容器里面的目录
root@ODS1SPGOFSDEV:/var/lib/docker/volumes/pg_wal/_data# docker exec -it 6b1285cdf987 /bin/bash root@6b1285cdf987:/# df -h Filesystem Size Used Avail Use% Mounted on overlay 18G 13G 4.1G 76% / tmpfs 64M 0 64M 0% /dev shm 64M 1.1M 63M 2% /dev/shm /dev/mapper/pg_wal_vg-pg_wal 10G 145M 9.9G 2% /pgdata1 /dev/mapper/pg_syslog_vg-pg_syslog 5.0G 68M 5.0G 2% /etc/hosts tmpfs 7.8G 0 7.8G 0% /proc/acpi tmpfs 7.8G 0 7.8G 0% /proc/scsi tmpfs 7.8G 0 7.8G 0% /sys/firmware root@6b1285cdf987:/# root@6b1285cdf987:/# ls /pgdata1 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 root@6b1285cdf987:/# root@6b1285cdf987:/# ls /var/lib/docker/volumes/pg_wal/_data ls: cannot access '/var/lib/docker/volumes/pg_wal/_data': No such file or directory root@6b1285cdf987:/# root@6b1285cdf987:/# exit exit root@ODS1SPGOFSDEV:~# ll /pgdata1 ls: cannot access '/pgdata1': No such file or directory root@ODS1SPGOFSDEV:~# root@ODS1SPGOFSDEV:~# df -h Filesystem Size Used Avail Use% Mounted on tmpfs 1.6G 1.7M 1.6G 1% /run /dev/mapper/ubuntu--vg-ubuntu--lv 18G 13G 4.1G 76% / tmpfs 7.8G 0 7.8G 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock /dev/sda2 2.0G 374M 1.5G 21% /boot /dev/mapper/pg_syslog_vg-pg_syslog 5.0G 68M 5.0G 2% /var/lib/docker/containers tmpfs 1.6G 0 1.6G 0% /run/user/0 /dev/mapper/pg_wal_vg-pg_wal 10G 143M 9.9G 2% /var/lib/docker/volumes/pg_wal/_data root@ODS1SPGOFSDEV:~# root@ODS1SPGOFSDEV:~# ls /var/lib/docker/volumes/pg_wal/_data 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
docker run --network=host是,PORTS一列没有信息,且docker exec -it dockerid /bin/bash进入容器时,不再像root@d3842f39b5f9这样显示容器名,而是直接变成了root@ODS1SPGOFSDEV:/#主机名,只不过~变成了/
root@ODS1SPGOFSDEV:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c1d3f8f35e27 postgres:15.6 "docker-entrypoint.s…" About an hour ago Up About an hour postgresql15.6
root@ODS1SPGOFSDEV:~#
root@ODS1SPGOFSDEV:~# docker exec -it c1d3f8f35e27 /bin/bash
root@ODS1SPGOFSDEV:/#
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。