无法使用Docker compose从Robot Framework容器访问Web应用程序

3phpmpom  于 2023-11-17  发布在  Docker
关注(0)|答案(1)|浏览(143)

bounty将在4天后过期。回答此问题可获得+50声望奖励。user2439278希望引起更多关注此问题。

我正在使用docker-compose进行Web应用程序设置,并在docker-compose中的单独Docker容器中运行robot框架

version: '3.7'

services:
  frontend:
    container_name: isp-frontend
    build:
      context: ./frontend
      dockerfile: Dockerfile.local
    ports:
      - "3000:3000"
    networks:
      - isp-network
    expose:
      - 3000  
    environment:
      # (while it's applied when TS is compiled)
      # For the frontend can be applied only during the build! 
      # You have to build manually without cache if one of those are changed at least for the prod mode.
      - REACT_APP_BACKEND_API=https://localhost:8000/api/v1
      - HTTPS=true
      - SSL_CRT_FILE=/app/dev-cert.dev-pem 
      - SSL_KEY_FILE=/app/dev-key.dev-pem
      - CI=true
      - CHOKIDAR_USEPOLLING=true
    #  - REACT_APP_ENV=sandbox
      
  postgres:
    image: postgres
    environment:
      POSTGRES_USER: test
      POSTGRES_PASSWORD: test
      PGDATA: /data/postgres
    volumes:
       - postgres:/data/postgres
    ports:
      - "5432:5432"
    networks:
      - isp-network
    restart: unless-stopped
  
  pgadmin:
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: "[email protected]"
      PGADMIN_DEFAULT_PASSWORD: dev
    volumes:
       - pgadmin:/root/.pgadmin
       - ./pgadmin-config/servers.json:/pgadmin4/servers.json
    ports:
      - "5050:80"
    networks:
      - isp-network
    restart: unless-stopped

  backend:
    container_name: isp-backend
    build:
      context: ./backend
      dockerfile: Dockerfile.local
    # these ports will be accessible internally and published on the host machine.
    ports:
      - "8000:443"
    volumes:
      - ./backend:/app
    command: > 
      bash -c "pip list && env && exec /start-reload.sh"
    networks:
      - isp-network
    depends_on:
      - postgres
    # These ports will be accessible by other services connected to the same network, 
    # but won't be published on the host machine.
    expose:
      - 80
      - 443
    environment:
      - GOOGLE_APPLICATION_CREDENTIALS=/app/.secret/secret.json
      - APP_DB_CONNECTION_STRING=postgresql+psycopg2://test:test@postgres:5432/postgres
      - LOG_LEVEL=debug
      - SQLALCHEMY_ECHO=True
      - CORS=https://localhost:3000,http://localhost:3000,https://isp-frontend:3000
      - PORT=443
      - AUTH_ALLOWED_DOMAINS=*
      - AUTH_REDIRECT_URL=https://localhost:3000/login
      # make uvicorn run as SSL/HTTPS
      - UVICORN_SSL_CERTFILE=/app/dev-cert.dev-pem
      - UVICORN_SSL_KEYFILE=/app/dev-key.dev-pem
      - UVICORN_RELOAD_EXCLUDE=env\*,venv\*,env,venv,.gitlab\*,.pytest_cache\*,.scannerwork\*,.secret\*,.vscode
      - UVICORN_RELOAD_DIR=/app/
      # port of uvicorn inside of the container
      - PRE_START_PATH=/app/scripts/alembic-upgrade.sh

  testing:
    container_name: testing
    build:
      context: ./isp-testing
      dockerfile: Dockerfile
    volumes:
      - ./isp-testing:/isp-app
    command: > 
      bash -c "/wait
      && mkdir -p /isp-app/tests/report
      && cd /isp-app/tests/ 
      && robot --outputdir "./report/FE_log" --include US24 ."
    networks:
      - isp-network
    depends_on:
      - backend
      - frontend  
    environment:
      - WAIT_HOSTS=frontend:3000
      - WAIT_TIMEOUT=3000
      - WAIT_SLEEP_INTERVAL=300
      - WAIT_HOST_CONNECT_TIMEOUT=300
      - FRONTEND_URL=https://isp-frontend:3000
      - BACKEND_URL=https://isp-backend:8000/api/v1
      - [email protected]
      - ROCHE_PASS=RJ2ND
      - ROCHE_USERNAME=TEST

volumes:
    postgres:
    pgadmin:
    testing:

networks:
    isp-network:
        driver: bridge

字符串
前端和后端服务使用localhost成功连接。但是测试服务,robot framework无法使用localhost连接到前端服务(https://localhost:3000和https://localhost:8000/api/v1)。它使用服务名称工作。我们在前端和后端之间使用服务名称,并且它使用localhost工作正常。只有在测试服务中,它无法使用localhost连接到应用程序。
有没有办法做到这一点?

fgw7neuy

fgw7neuy1#

我已经设法解决了这个问题,通过将network标志添加到builds部分,测试Docker容器可以使用localhost访问应用程序。

testing:
    container_name: isp-testing
    build:
      context: ./isp-testing
      dockerfile: Dockerfile
      network: host  # This helps to access the application running in host machine using localhost
    volumes:
      - ./isp-testing:/isp-app
    command: > 
      bash -c "/wait
      && mkdir -p /isp-app/tests/report
      && cd /isp-app/tests/ 
      && robot --outputdir "./report/FE_log" --include US24 ."
    network_mode: "host"
    depends_on:
      - backend
      - frontend  
    environment:
      - WAIT_HOSTS=docker.for.win.localhost:3000
      - WAIT_TIMEOUT=3000
      - WAIT_SLEEP_INTERVAL=300
      - WAIT_HOST_CONNECT_TIMEOUT=300
      - FRONTEND_URL=https://localhost:3000
      - BACKEND_URL=https://localhost:8000/api/v1

字符串

相关问题