Docker Easy Build Database (PostgreSQL)

TL;DR It is an article that let's make PostgreSQL easily with Docker. Since it is a training article, tools etc. are decided, but that area is Donmai.

Let's download it first

This time the SQL client uses DBeaver. Just get the latest version for the time being. https://dbeaver.io/

Let's launch DB quickly while downloading.

Docker Desktop for Windows Without this, we can't talk. Let's install it first. https://docs.docker.jp/docker-for-windows/install.html

Hit `docker ps` in PowerShell and you're ready to go:

> docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED        STATUS        PORTS                    NAMES

Let's start PostgreSQL for the time being

Since it is a training, from how to carefully search for an image. Below, the command is after ```> .

> docker search postgres
NAME                                    DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
postgres                                The PostgreSQL object-relational database sy…   8763      [OK]

I found it quickly when I searched. Basically, it is better to take the image of OFFICIAL (official).

Here is the following command.

docker run -d --rm --name db -p 5555:5432 -e POSTGRES_HOST_AUTH_METHOD=trust postgres

Download the image and start it. The explanation of the command will be described later. If the container is started with `` `docker ps```, it is complete.

> docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                    NAMES
9676081a5f72   postgres   "docker-entrypoint.s…"   46 seconds ago   Up 46 seconds   0.0.0.0:5555->5432/tcp   db

Did you download DBeaver?

Let's install and launch it.

image.png

Port at 5555. image.png

Did you connect? Let's make a table or column. image.png

This time, I made a "test table" as follows. image.png

Try connecting with psql

docker exec -it db bashConnect to the container with.

> docker exec -it db bash
root@9676081a5f72:/#
root@9676081a5f72:/# psql -U postgres
psql (13.0 (Debian 13.0-1.pgdg100+1))
Type "help" for help.

postgres=# \db
       List of tablespaces
    Name    |  Owner   | Location
------------+----------+----------
 pg_default | postgres |
 pg_global  | postgres |
(2 rows)

postgres=# \dt
             List of relations
 Schema |      Name      | Type  |  Owner
--------+----------------+-------+----------
 public |Test table| table | postgres
(1 row)

postgres=#

It's done properly. Exit with \ q and escape from the container with` `` exit```.

This container disappears when it's done and the data isn't persisted

> docker ps --all
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS                        PORTS                    NAMES
9676081a5f72   postgres          "docker-entrypoint.s…"   20 minutes ago   Up 20 minutes                 0.0.0.0:5555->5432/tcp   db

It exists like this now, but let's stop this container. docker stop dbIt is.

Is it disappearing properly?

> docker ps --all
CONTAINER ID   IMAGE             COMMAND                  CREATED        STATUS                        PORTS                    NAMES

Explanation of this Docker command

DB started up quickly with this one line. Seriously Docker-san, God?

docker run -d --rm --name db -p 5555:5432 -e POSTGRES_HOST_AUTH_METHOD=trust postgres
Overview Remarks
run Start-up https://docs.docker.jp/engine/reference/run.html
-d Detached In short background
--rm Delete container when exiting container You can delete it yourself with docker rm db which is not required
--name db Container name This timedbAnd said. Optional
-p 5555:5432 Port forwarding Container 5432 to host 5555
-e POSTGRES_HOST_AUTH_METHOD=trust No password If you want to give a password**-e POSTGRES_PASSWORD=password**Let's use
postgres Image name The guy I searched for

Bonus (docker-compose and persistence)

Folder structure of docker-compose and sample of Dockerfile and docker-compose.yml. This sample should respond on host 5434. Add ** app ** to ** services ** and connect to DB immediately from app via db-net. The image is that init.sql contains SQL to create DB and tables. I made it a long time ago, so please forgive me if it's old or not cool.

./
- docker-compose.yml
+ database/
  - Dockerfile
+ postgres-init/
  - init.sql
+ postgres/
FROM postgres:9.6.16

RUN localedef -i ja_JP -c -f UTF-8 -A /usr/share/locale/locale.alias ja_JP.UTF-8
ENV LANG ja_JP.utf8

docker-compose.yml


version: "3.5"

services:
  db:
    build:
      context: database
    volumes:
      - ./postgres:/var/lib/postgresql
      - ./postgres-init:/docker-entrypoint-initdb.d
    expose:
      - '5432'
    ports: 
      - "5434:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_INITDB_ARGS: "--encoding=UTF-8"
    restart: 'always'
    networks:
      - db-net

networks:
  db-net:
    driver: bridge

Recommended Posts

Docker Easy Build Database (PostgreSQL)
Build a development environment for Docker + Rails6 + Postgresql
Migrate GitBucket database to postgreSQL
Build docker environment with WSL
Build DynamoDB local with Docker
multi-project docker build using jib
Build Couchbase local environment with Docker
Build a Node.js environment with Docker
Build PlantUML environment with VSCode + Docker
[Splunk] Build Splunk Enterprise (docker file creation)
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Build Clang x VSCode on Docker (1)
Easy database access with Java Sql2o
Build Unity development environment on docker
Build docker + laravel environment with laradock
How to build CloudStack using Docker
Build WebRTC Janus with Docker container
[Splunk] Build Splunk Enterprise (use docker image)
Spring Boot gradle build with Docker
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)