|
|
(25 intermediate revisions by the same user not shown) |
Line 1: |
Line 1: |
| =Internal= | | =Internal= |
|
| |
|
| * [[Postgresql#Subjects|Postgresql]] | | * [[PostgreSQL#Subjects|PostgreSQL]] |
|
| |
|
| =Overview= | | =Overview= |
|
| |
|
| In its simplest form:
| | This article describes how to stand up and operate a Docker-based PostgreSQL instance. The instance will be accessible on the standard PostgreSQL port on the local host and it will be secured by a conventional user and password. Since the password is visible in the terminal used to create the container image, this approach is not really secure, but it is good enough for development purposes. The container-generated data will be stored on an [[Docker_Storage_Concepts#Anonymous_Volume|anonymous]] [[Docker_Storage_Concepts#Local_Volume_Driver|local]] volume. |
|
| |
|
| <syntaxhighlight lang='bash'>
| | =Operations= |
| docker run postgres
| |
| </syntaxhighlight >
| |
|
| |
|
| command starts a transient Posgres container instance that does not expose any port on the local host, it is initialized on the fly to allow access to any user and uses an [[Docker_Storage_Concepts#Anonymous_Volume|anonymous]] [[Docker_Storage_Concepts#Local_Volume_Driver|local]] volume that will '''not''' be reattached to during the next run. To get a usable Postgres instance we can shut down and restart and that regains access to stored data between restarts, we should use create a dedicated [[Docker_Storage_Concepts#Named_Volume|named volume]] or a [[Docker_Storage_Concepts#Bind_Mount|bind mount]], initialize the database with a username and a password during the first initialization run and map ports to the local host.
| | ==Create the Container== |
|
| |
|
| =Dedicated Storage=
| | This is an one-time operation: |
|
| |
|
| We prefer using a [[Docker_Storage_Concepts#Bind_Mount|bind mount]] as it gives easy access to the stored state, in case we need to troubleshoot. A [[Docker_Storage_Concepts#Named_Volume|named volume]] can be also used.
| | docker run -p 5432:5432/tcp -e POSTGRES_USER=admin -e POSTGRES_PASSWORD=<some-password> --name postgres postgres |
|
| |
|
| <syntaxhighlight lang='bash'>
| | If you want to use a local directory for storage, which will be made available to the container with a [[Docker_Storage_Concepts#Bind_Mount|bind mount]], add the following arguments. Note that this is only necessary if you want to have convenient access to the database files and logs, for troubleshooting and debugging purposes: |
| docker run ... --mount type=bind,source=/Users/ovidiu/runtime/docker-volumes/postgres,target=/var/lib/postgresql/data --name postgres ... postgres
| |
| </syntaxhighlight >
| |
|
| |
|
| =Authenticated Access= | | --mount type=bind,source=<local-directory>,target=/var/lib/postgresql/data |
|
| |
|
| When the container is run the first time, it goes through an initialization sequence, and if no specific authentication configuration is provided in form of environment variables, it will configure itself to allow unauthenticated access. To prevent that from happening, start the container '''for the first time''' as shown:
| | ==Start the Container== |
|
| |
|
| <syntaxhighlight lang='bash'>
| | docker start postgres |
| docker run --mount type=bind,source=/Users/ovidiu/runtime/docker-volumes/postgres,target=/var/lib/postgresql/data -e POSTGRES_USER=admin -e POSTGRES_PASSWORD=<some-password> --name postgres postgres | |
| </syntaxhighlight >
| |
|
| |
|
| This will initialized the database to allow access as admin/<''some-password''>.
| | ==Stop the Container== |
|
| |
|
| Subsequent startups will not require specification of -e POSTGRES_USER and -e POSTGRES_PASSWORD, as long as we use the same local directory Postgres initialized its state on.
| | docker stop postgres |
|
| |
|
| =Exposing Ports on the Local Host= | | ==Access Container Logs== |
|
| |
|
| Postgres binds by default on port 5432 and this is the port published by the following command:
| | docker logs -f postgres |
| | |
| <syntaxhighlight lang='bash'>
| |
| docker run ... -p 5432:5432/tcp ...
| |
| </syntaxhighlight>
| |
| | |
| | |
| =Procedures=
| |
| | |
| ==Initialization==
| |
| | |
| Create a local directory <tt>/Users/ovidiu/runtime/docker-volumes/postgres</tt> and then execute the initialization run:
| |
| | |
| <syntaxhighlight lang='bash'>
| |
| docker run --mount type=bind,source=/Users/ovidiu/runtime/docker-volumes/postgres,target=/var/lib/postgresql/data -e POSTGRES_USER=admin -e POSTGRES_PASSWORD=admin123 --name postgres postgres
| |
| </syntaxhighlight>
| |
| | |
| The container can be removed after initialization with:
| |
| | |
| <syntaxhighlight lang='bash'>
| |
| docker stop postgres
| |
| docker rm postgres
| |
| </syntaxhighlight>
| |
| | |
| ==Routine Start==
| |
| | |
| | |
| | |
| ==Log Into the Container==
| |
| | |
| <syntaxhighlight lang='bash'>
| |
| docker exec -it <container-id> bash
| |
| </syntaxhighlight>
| |
Internal
Overview
This article describes how to stand up and operate a Docker-based PostgreSQL instance. The instance will be accessible on the standard PostgreSQL port on the local host and it will be secured by a conventional user and password. Since the password is visible in the terminal used to create the container image, this approach is not really secure, but it is good enough for development purposes. The container-generated data will be stored on an anonymous local volume.
Operations
Create the Container
This is an one-time operation:
docker run -p 5432:5432/tcp -e POSTGRES_USER=admin -e POSTGRES_PASSWORD=<some-password> --name postgres postgres
If you want to use a local directory for storage, which will be made available to the container with a bind mount, add the following arguments. Note that this is only necessary if you want to have convenient access to the database files and logs, for troubleshooting and debugging purposes:
--mount type=bind,source=<local-directory>,target=/var/lib/postgresql/data
Start the Container
docker start postgres
Stop the Container
docker stop postgres
Access Container Logs
docker logs -f postgres