Centos+Postgresql (Docker)

To share the experiences to create docker containers of databases, for stream duplication of data via master and slave.

  • Windows home additon does not support Docker ce for windows.
    • You may encounter error when installing Docker with the installer downloaded from the official website.
  • Database services should be run standalone when the new container is created.
    • You may encounter D-Bus failure when you want to run the "posgresql*.*-setup initdb". This is solvable if your create a standalone command when creating the new container.
~$ sudo docker run -d --name=containername --privileged=true imagename:tagname /usr/sbin/init
    • "run" is equavilent to "create";
    • -d means the command is working at background;
    • --name gives the name of your container;
    • --privileged specifies the root privilege in the container;
    • imagename:tagname gives the image used to create the container;
    • /usr/sbin/init is the guard process running for -d.
  • "exec" works better than "attach".
    • attach does not have rich options to run script in the container.
1. 安装DockerCentOS上安装Docker,可以使用官方的安装脚本。打开终端并运行以下命令: ``` $ curl -fsSL https://get.docker.com/ | sh ``` 如果您的系统上没有curl,请先安装它: ``` $ yum install curl ``` 2. 下载PostgreSQLDockerfile 我们可以从官方的Docker Hub上下载PostgreSQLDockerfile。使用以下命令: ``` $ curl -O https://raw.githubusercontent.com/docker-library/postgres/master/13/alpine/Dockerfile ``` 3. 编辑Dockerfile 使用vim或nano等编辑器打开下载下来的Dockerfile文件,进行以下配置: ``` FROM centos:latest ENV POSTGRES_USER postgres ENV POSTGRES_PASSWORD postgres ENV POSTGRES_DB postgres RUN yum update -y && \ yum install -y postgresql-server postgresql-contrib && \ yum clean all USER postgres RUN initdb --encoding=UTF8 --locale=C -D /var/lib/pgsql/data && \ pg_ctl -D /var/lib/pgsql/data -l logfile start && \ psql --command "CREATE USER postgres WITH SUPERUSER PASSWORD 'postgres';" && \ createdb -O postgres postgres VOLUME ["/var/lib/pgsql/data"] EXPOSE 5432 CMD ["postgres", "-D", "/var/lib/pgsql/data", "-c", "config_file=/var/lib/pgsql/data/postgresql.conf"] ``` 4. 构建Docker镜像 使用以下命令构建Docker镜像: ``` $ docker build -t my_postgresql . ``` 这将构建一个名为“my_postgresql”的新Docker镜像。 5. 运行PostgreSQL容器 使用以下命令运行PostgreSQL容器: ``` $ docker run -d -p 5432:5432 --name my_postgresql_container my_postgresql ``` 这将创建一个名为“my_postgresql_container”的新容器,并将容器的端口5432映射到主机的端口5432。 6. 测试PostgreSQL容器 为了测试新的PostgreSQL容器,请使用以下命令: ``` $ psql -h localhost -U postgres -d postgres ``` 您应该现在可以通过psql连接到PostgreSQL容器。 现在您已经成功地使用Docker创建了一个基于CentOSPostgreSQL镜像,并运行了一个新的PostgreSQL容器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值