victorzadorozhnyy
@victorzadorozhnyy

Как открыть Docker порт для подключения TCP?

В проекте есть 4 докер контейнера в одном nginx, в другом Postgressql база данных.
Нужно подключить IDE DataGrip для работы с базами данных.
Как открыть порт к контейнеру с бд?

Пробовал из папки бд через терминал
docker exec -it 75710d6cf29f /bin/bash
я попадаю в root@75710d6cf29f, но IDE подключить не могу

Проект выглядит так
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'

Запускаю проект через 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;
  • Вопрос задан
  • 107 просмотров
Пригласить эксперта
Ответы на вопрос 1
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через TM ID
Похожие вопросы