N
N
Ninja Mate2018-02-10 01:00:47
PostgreSQL
Ninja Mate, 2018-02-10 01:00:47

How to open Docker port for TCP connection?

The project has 4 docker containers in one nginx, in another Postgressql database.
You need to connect the DataGrip IDE to work with databases.
How to open a port to a container with a database?

Tried from db folder via terminal

docker exec -it 75710d6cf29f /bin/bash
I get to [email protected], but I can't connect the IDE

The project looks like
docker-compose
version: '2'
services:
  nginx-api:
    image: nginx
    depends_on:
      - api
    ports:
      - "3001:443"
    environment:
      NODE_TLS_REJECT_UNAUTHORIZED: 0
    volumes:
      - ./api/api.nginx.site:/etc/nginx/conf.d/default.conf
      - ./api/certs/.:/etc/nginx/certs
  api:
    build: api/.
    command: yarn start
    depends_on:
      - db
    environment:
      NODE_TLS_REJECT_UNAUTHORIZED: 0
      NODE_ENV: development
      COOKIE_SECRET: ChangeMe1234
    env_file:
      - api/.env
    ports:
      - '8080:3000'
    volumes:
      - ./api/.:/opt/app
      - /opt/app/node_modules
  nginx-frontend:
    image: nginx
    depends_on:
      - frontend
    ports:
      - "3000:443"
    environment:
      NODE_TLS_REJECT_UNAUTHORIZED: 0
    volumes:
      - ./frontend/frontend.nginx.site:/etc/nginx/conf.d/default.conf
      - ./frontend/certs/.:/etc/nginx/certs
  frontend:
    build: frontend/.
    command: yarn start
    environment:
      NODE_TLS_REJECT_UNAUTHORIZED: 0
      NODE_ENV: development
      COOKIE_SECRET: ChangeMe1234
      API_URL: "https://nginx-api"
    ports:
      - "0.0.0.0:3030:3000"
    volumes:
      - ./frontend/.:/opt/frontend
      - /opt/frontend/node_modules
  db:
    build: db/.
    environment:
      NODE_TLS_REJECT_UNAUTHORIZED: 0
      POSTGRES_PASSWORD: localdev
      PGDATA: /var/lib/postgresql/data/pgdata
    volumes:
      - ./db/postgres-data:/var/lib/postgresql/data/pgdata
    ports:
      - '8001:5432'

I run the project through docker/run.sh
#!/bin/bash

set -e

COMPOSE_PROJECT_NAME=rpgboilerplate

# Linux fix
CONFIG_FILE=docker-compose.yml

CACHE_INIT=false

SERVICES=(api frontend)
SERVICES_DIR=(api frontend)

BUILD_CMD="docker-compose -f $CONFIG_FILE build"
UP_CMD="docker-compose -f $CONFIG_FILE up"

function rmContainers() {
  echo "Removing containers"
  docker-compose -f $CONFIG_FILE rm -fv $SERVICE_NAME || true
}

function initCache() {
  # Init empty cache file
  touch $SERVICE_DIR/yarn.lock
  if [ ! -f $SERVICE_DIR/.yarn-cache.tgz ]; then
    echo "Init empty $SERVICE_DIR/.yarn-cache.tgz"
    tar cvzf $SERVICE_DIR/.yarn-cache.tgz --files-from /dev/null
    CACHE_INIT=true
  fi
}

function retrieveYarnCache() {
  echo "Checking if I need to retrieve Yarn cache and lock file for $SERVICE_NAME"
  RETRIEVE=false

  # Case #1: we initialized an empty cache (usually, first run after cloning)
  if [ "$CACHE_INIT" = true ]; then
    RETRIEVE=true
  fi

  # Case #2: yarn packages changed during build
  docker run --rm --entrypoint cat ${COMPOSE_PROJECT_NAME}_$SERVICE_NAME:latest /cache/yarn.lock > /tmp/${SERVICE_NAME}_yarn.lock
  if ! diff -q $SERVICE_DIR/yarn.lock /tmp/${SERVICE_NAME}_yarn.lock > /dev/null  2>&1; then
    RETRIEVE=true
  fi

  if [ "$RETRIEVE" = true ]; then
    echo "Retrieving"
    docker run --rm --entrypoint cat ${COMPOSE_PROJECT_NAME}_$SERVICE_NAME:latest /.yarn-cache.tgz > $SERVICE_DIR/.yarn-cache.tgz
    cp /tmp/${SERVICE_NAME}_yarn.lock $SERVICE_DIR/yarn.lock
  else
    echo "Not retrieving"
  fi
  echo "Done"
}

for i in "${!SERVICES[@]}"
do
  SERVICE_NAME=${SERVICES[$i]} SERVICE_DIR=${SERVICES_DIR[$i]} rmContainers
  SERVICE_NAME=${SERVICES[$i]} SERVICE_DIR=${SERVICES_DIR[$i]} initCache
done

COMPOSE_PROJECT_NAME=$COMPOSE_PROJECT_NAME $BUILD_CMD

for i in "${!SERVICES[@]}"
do
  SERVICE_NAME=${SERVICES[$i]} SERVICE_DIR=${SERVICES_DIR[$i]} retrieveYarnCache
done

COMPOSE_PROJECT_NAME=$COMPOSE_PROJECT_NAME $UP_CMD


SQL
CREATE DATABASE localdb;
CREATE ROLE rpg_app with login superuser password 'localdev';
grant all privileges on database localdb to rpg_app;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2018-02-10
@Xuxicheta

db:
    - '5432:5432'

And then your database is visible from the host on port 8001.
Why this is done, I don’t know. Maybe not to interfere with another postgress.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question