Wednesday, May 6, 2020

How to create a Docker container for PostgreSQL


  1. install Docker Desktop from https://www.docker.com/get-started based on your OS.
  2. create your Docker account at https://hub.docker.com and log on.
  3. open a command window (in Windows), and type docker --version to check the docker version.
    • c:\users\testuser>docker --version
    • Docker version 19.03.8, build afacb8b
  4. start Docker Desktop and log on your Docket account.
  5. search postgres from  https://hub.docker.com and click postgres Office Image.
  6. in the command window, type "docker pull postgres" to get the latest PostgreSQL image. Note: by default, the PostgreSQL version is the latest. To get a particular version, execute "docker pull postgres:12.2".
  7. in the command window, type the following to create and start a PostgreSQL container (-p for port number <trigger port #:port# in container>):
    • docker run --name containername -h localhost -p 5435:5432 -e POSTGRES_PASSWORD=mypassword -d postgres:12.2
  8. use "docker ps" or "docker container ls" to show the running container status. "docker ps -a" lists all existing containers.
  9. to stop a docket container, type "docker stop containername"
  10. to restart an existing container, type "docker start containername"
  11. get in the docker container by typing "docker exec -it containername bash"
  12. within the container bash shell, you can then log in PostgreSQL as usual
    • psql -U postgres postgres
  13. if you specified host and port numbers when creating the docker postgres container, you can access from the local machine using postgres client
    • psql -h localhost -p 5435 -U postgres -W
  14. to remove the container, use "docker rm -f containername"
  15. to list docker images, type "docker images" or "docker image ls"
  16. to remove an image by name, use "docker image rm -f imagename", or by id using "docker image rmi -f 93d5585e579d"

No comments:

Post a Comment