无法使用 docker compose 从机器人框架容器访问 Web 应用程序

Unable to access web application from robot framework container using docker compose

提问人:user2439278 提问时间:11/10/2023 更新时间:11/13/2023 访问量:69

问:

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

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 连接成功。但是测试服务,机器人框架无法使用 localhost(https://localhost:3000 和 https://localhost:8000/api/v1) 连接到前端服务。它使用服务名称工作。我们在前端和后端之间使用服务名称,它与 localhost 配合得很好。只有在测试服务中,它才能使用 localhost 连接到应用程序。

有什么办法可以做到这一点吗?

docker docker-compose 机器人框架

评论

0赞 Prateek Jain 11/13/2023
您是否尝试过使用 docker ip 而不是 localhost 进行连接?看看这个线程 stackoverflow.com/questions/40746453/...

答:

2赞 user2439278 11/13/2023 #1

我已经设法通过将标志添加到该部分来解决这个问题,测试 docker 容器能够使用 localhost 访问应用程序。networkbuilds

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