Skip to content

Instantly share code, notes, and snippets.

@jd-apprentice
Last active May 8, 2024 04:37
Show Gist options
  • Save jd-apprentice/5818628a7815975b1e56e00c5e87704a to your computer and use it in GitHub Desktop.
Save jd-apprentice/5818628a7815975b1e56e00c5e87704a to your computer and use it in GitHub Desktop.
# Dockerize a database + DBeaver

How to build and run our containers.

First create a .env file with the following structure (names are like this because i'm using postgres)

POSTGRES_CONTAINER_NAME=
POSTGRES_CONTAINER_IMAGE_NAME=

postgres_USER=
postgres_PASS=
postgres_DB=
postgres_PORT=

Fill the data in the .env file where the fields are from

  • POSTGRES_CONTAINER_NAME // ContainerName
  • POSTGRES_CONTAINER_IMAGE_NAME // Image name you can get it from the Docker Hub -> https://hub.docker.com/_/postgres/
  • postgres_USER // User usually postgres for working locally
  • postgres_PASS // Password usually postgress too
  • postgres_DB // For working locally postgres uses to be a standard name too
  • postgres_PORT // 5433 Default

Create the docker-compose.yml

version: "3.8"
services:
  postgres:
    container_name: ${POSTGRES_CONTAINER_NAME}
    image: ${POSTGRES_CONTAINER_IMAGE_NAME}
    restart: "no"
    env_file:
      - .env
    environment:
      - POSTGRES_PASSWORD=${postgres_PASS}
      - POSTGRES_USER=${postgres_USER}
      - POSTGRES_DB=${postgres_DB}
    ports:
      - ${postgres_PORT}:5432
    volumes:
      - ./data_postgres:/var/lib/postgresql/data

once we have ready our docker-compose.yml we run the following command

_ docker compose up -d

to check if we have our container running we can check it with the following command

docker ps

image

now for connecting to our db in DBeaver we are going to need the ip for the container to do that copy the container id from there and run the next command

docker inspect <Container-ID>

at the end of the response we get the following structure

"Networks": {
                "vehiclesretail_default": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "string",
                        "string",
                        "string"
                    ],
                    "NetworkID": "string",
                    "EndpointID": "string",
                    "Gateway": "string",
                    "IPAddress": "string" // This is the line we need
                    "IPPrefixLen": "number",
                    "IPv6Gateway": "string",
                    "GlobalIPv6Address": "string",
                    "GlobalIPv6PrefixLen": "number",
                    "MacAddress": "string",
                    "DriverOpts": null
                }
            }

now we are going to open DBeaver and create a new connection

  • Database
  • New Database Collection

image

populate the fields required and click on test connection

image

if everything is okay we should see the following image

image

after that click finish and here we go! we have our DBeaver connected to our docker container.

@MKAbdElrahman
Copy link

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment