PostgreSQL with Docker: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 5: Line 5:
=Overview=
=Overview=


In its simplest form,
In its simplest form:


<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
Line 11: Line 11:
</syntaxhighlight >
</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 anonymous 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 the following:
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]], initialize the database with a username and a password during the first initialization run and map ports to the local host.
<font color=darkgray>
* simplest image - data is lost
* external volume
* port mapping.
</font>


=Running a Transient Instance=
=Running a Transient Instance=

Revision as of 17:22, 18 October 2018

Internal

Overview

In its simplest form:

docker run postgres

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 anonymous 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 named volume, initialize the database with a username and a password during the first initialization run and map ports to the local host.

Running a Transient Instance

Postgres binds by default on port 5432 and this is the port published by the following command:

docker run -p 5432:5432/tcp  postgres

The command will implicitly create a local anonymous volume that will linger around after the container stops. The data on the anonymous local volume can be accessed by starting the stopped container. Test

You can attach to the container with:

docker exec -it <container-id> bash