Postgres Docker
First we'll need to create a docker image for our database.
Note: If you're not familiar with Docker, and have pluralsight subscription, I highly recommend watching this docker deep-dive course by Nigel Poulton. This is a really great course and it gives a quick ramp up on docker, just enough to get started. There is also a plenty of other learning docker resources available. Anyway even without basic docker knowledge, you should be able to proceed with the tutorial.
Let's create a separate postgres
directory inside project's root folder to keep track of database-related files.
Now we can download postgres_create.sql
script and save it under newly created postgres
directory.
The db image will be based on the official postgres
Docker image. On top of that, we'll run our postgres_create.sql
script. This can be declared in the lines of following Dockerfile (create Dockerfile
under postgres
directory):
FROM postgres
COPY postgres_create.sql /docker-entrypoint-initdb.d/postgres_create.sql
The COPY
instruction will place the script in a special directory inside the container, from which all scripts are run when the image is built.
With the script and Dockerfile in place, we can proceed to building the Docker image for database. Run following command from the root directory:
> docker build -t theimowski/suavemusicstore_db:0.1 postgres
The command will build database image with a theimowski/suavemusistore_db
tag in version 0.1
using Dockerfile from postgres
directory.
After running the script and typing docker images
, you should spot the newly built image:
REPOSITORY TAG IMAGE ID CREATED SIZE
theimowski/suavemusicstore_db 0.1 45ac0e98f557 4 days ago 266 MB
postgres latest ecd991538a0f 2 weeks ago 265 MB
To spin up a DB container from the image, simply type:
> docker run --name suavemusicstore_db -e POSTGRES_PASSWORD=mysecretpassword `
-d -p 5432:5432 theimowski/suavemusicstore_db:0.1
Arguments to the above command are:
- proper name of the container (
--name <name>
), - environment variable for Postgres password (
-e <VAR>=<val>
), -d
switch to indicate detached (background) mode,- mapped standard Postgres port 5432 from the container to host (
-p <hostPort>:<containerPort>
), - the tag of image (
theimowski/suavemusicstore_db:0.1
).
If everything went fine, we should be able to see a running container with the docker ps
command:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a47a4917d6af theimowski/suavemusicstore_db:0.1 "docker-entrypoint..." 47 hours ago Up 4 seconds 0.0.0.0:5432->5432/tcp suavemusicstore_db
Full name: SuaveMusicStore.Path.IntPath
type PrintfFormat<'Printer,'State,'Residue,'Result> =
new : value:string -> PrintfFormat<'Printer,'State,'Residue,'Result>
member Value : string
Full name: Microsoft.FSharp.Core.PrintfFormat<_,_,_,_>
--------------------
type PrintfFormat<'Printer,'State,'Residue,'Result,'Tuple> =
inherit PrintfFormat<'Printer,'State,'Residue,'Result>
new : value:string -> PrintfFormat<'Printer,'State,'Residue,'Result,'Tuple>
Full name: Microsoft.FSharp.Core.PrintfFormat<_,_,_,_,_>
--------------------
new : value:string -> PrintfFormat<'Printer,'State,'Residue,'Result>
--------------------
new : value:string -> PrintfFormat<'Printer,'State,'Residue,'Result,'Tuple>
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
val string : value:'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = System.String
Full name: Microsoft.FSharp.Core.string
Full name: Microsoft.FSharp.Core.unit
Full name: SuaveMusicStore.Path.withParam
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
Full name: SuaveMusicStore.Path.home
from SuaveMusicStore.Path
Full name: SuaveMusicStore.Path.Store.overview
Full name: SuaveMusicStore.Path.Store.browse
Full name: SuaveMusicStore.Path.Store.details
Full name: SuaveMusicStore.Path.Store.browseKey
from Suave
Full name: SuaveMusicStore.View.cssLink
Full name: Suave.Html.link
Full name: SuaveMusicStore.View.h2
Full name: Suave.Html.tag
Full name: SuaveMusicStore.View.ul
Full name: SuaveMusicStore.View.li
Full name: SuaveMusicStore.View.home
Full name: SuaveMusicStore.View.store
Full name: Suave.Html.p
module List
from Microsoft.FSharp.Collections
--------------------
type List<'T> =
| ( [] )
| ( :: ) of Head: 'T * Tail: 'T list
interface IEnumerable
interface IEnumerable<'T>
member GetSlice : startIndex:int option * endIndex:int option -> 'T list
member Head : 'T
member IsEmpty : bool
member Item : index:int -> 'T with get
member Length : int
member Tail : 'T list
static member Cons : head:'T * tail:'T list -> 'T list
static member Empty : 'T list
Full name: Microsoft.FSharp.Collections.List<_>
Full name: Microsoft.FSharp.Collections.List.length
from SuaveMusicStore
Full name: Suave.Html.a
Full name: SuaveMusicStore.View.browse
Full name: SuaveMusicStore.View.details
Full name: SuaveMusicStore.View.index
Full name: Suave.Html.html
Full name: Suave.Html.head
Full name: Suave.Html.title
Full name: Suave.Html.body
Full name: Suave.Html.div
Full name: Suave.Html.htmlToString
from SuaveMusicStore
from Suave
from Suave
from Suave
from Suave
from Suave
Full name: SuaveMusicStore.App.html
Full name: Suave.Successful.OK
from SuaveMusicStore
Full name: SuaveMusicStore.View.index
Full name: SuaveMusicStore.App.browse
Full name: Suave.Http.request
Full name: SuaveMusicStore.View.browse
Full name: Suave.RequestErrors.BAD_REQUEST
Full name: SuaveMusicStore.App.webPart
Full name: Suave.WebPart.choose
Full name: Suave.Filters.path
Full name: SuaveMusicStore.View.home
Full name: SuaveMusicStore.View.store
Full name: Suave.Filters.pathScan
Full name: SuaveMusicStore.Path.Store.details
Full name: SuaveMusicStore.View.details
Full name: Suave.Filters.pathRegex
from Suave
Full name: Suave.Files.browseHome
Full name: Suave.Web.startWebServer
Full name: Suave.Web.defaultConfig
Show code from this section on GitHub