Database creation with Docker

Now, we will have a real database to save our entities.

We will use Docker to have a container for a Postgres database. Create, at the root of the project, the file docker-compose.yml with following content:

services:
  database:
    image: postgres
    restart: always
    ports:
      - 5432:5432
    environment:
      POSTGRES_PASSWORD: pass123      
    volumes:
      - database_data:/var/lib/postgresql/data

volumes:
  database_data:

We are writing our database password in plain text. This is a security issue. In the Configuration section, we'll learn how to fix this.

To turn on the container, use the following command:

docker-compose up -d

If you want to turn it off at some point, just use:

docker-compose down

Finally, to access our database, we can use pgAdmin. We shall register a server with the following settings:

  • Name - Conrod

  • Host name/address - localhost

  • Port - 5432

  • Password - pass123

  • Save password? - Yes

Commit - Creating database

Last updated