Cypress Docker容器未连接到正在运行的服务器

eyh26e7m  于 2022-12-29  发布在  Docker
关注(0)|答案(1)|浏览(235)

[编辑:即使问题可能是duplicate of this one,我也不会删除,因为原始问题可能更难搜索。如果不建议这样做,请随时删除/关闭。]
我有这样的码头 composer :

x-common-postgres-env:
  &common-postgres-env
    POSTGRES_DB: ${POSTGRES_DB}
    POSTGRES_USER: ${POSTGRES_USER}
    POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    POSTGRES_PORT: 5432

x-common-postgres:
  &common-postgres
  image: postgres:13.4
  hostname: postgres
  environment:
    << : *common-postgres-env
  ports:
    - "5432:5432"
  healthcheck:
    test: ["CMD", "pg_isready", "-U", "${POSTGRES_USER}", "-d", "${POSTGRES_DB}"]

x-common-django:
  &common-django
  build: .
  environment:
    &common-django-env
    << : *common-postgres-env
    DJANGO_SECRET: ${DJANGO_SECRET}
    ALLOWED_HOSTS: ".localhost 127.0.0.1 [::1]"
    CORS_ALLOWED_ORIGINS: "http://localhost:8000"
    CSRF_TRUSTED_ORIGINS: "http://localhost:8000"
  healthcheck:
    test: ["CMD", "wget", "-qO", "/dev/null", "http://localhost:8000"]
  ports:
    - "8000:8000"

services:

  db:
    << : *common-postgres
    profiles:
      - prod
    volumes:
      - ./data/db:/var/lib/postgresql/data

  db-test:
    << : *common-postgres
    profiles:
      - test

  web:
    << : *common-django
    profiles:
      - prod
    command: pdm run python manage.py runserver 0.0.0.0:8000
    environment:
      << : *common-django-env
      POSTGRES_HOST: db
    volumes:
      - ./KJ_import:/code/KJ_import
      - ./docs:/code/docs
      - ./KJ-JS:/code/KJ-JS
      - ./static:/code/static
      - ./media:/code/media
      - ./templates:/code/templates
    depends_on:
      db:
        condition: service_healthy

  web-test:
    << : *common-django
    profiles:
      - test
    command: pdm run python manage.py runserver 0.0.0.0:8000
    environment:
      << : *common-django-env
      POSTGRES_HOST: db-test
    depends_on:
      db-test:
        condition: service_healthy

  cypress:
    # image: "cypress/included:9.2.0"
    profiles:
      - test
    build:
      context: .
      dockerfile: Dockerfile.cy
    # command: ["--browser", "chrome"]
    environment:
      CYPRESS_baseUrl: http://localhost:8000/
    working_dir: /code/KJ-JS
    volumes:
      - ./KJ-JS:/code/KJ-JS
      - ./media:/code/media
    depends_on:
      web-test:
        condition: service_healthy

This Dockerfile.cy

FROM cypress/included:9.2.0

# WORKDIR /code/KJ-JS
COPY system.conf /etc/dbus-1/system.conf
RUN chmod 644 /etc/dbus-1/system.conf

COPY entrypoint.cy.sh /
ENTRYPOINT ["/bin/sh", "/entrypoint.cy.sh"]

and this entrypoint.cy.sh to activate the Cypress tests:

#!/bin/sh

echo "### Create DBus"

dbus-uuidgen > /var/lib/dbus/machine-id

mkdir -p /var/run/dbus
dbus-daemon --config-file=/usr/share/dbus-1/system.conf --print-address &

# Wait for the D-Bus system bus address to be available
while [ -f /var/run/dbus/system_bus_socket ]; do
   sleep 1
done

# Check if the dbus-daemon process is running
if ps -ef | grep -v grep | grep dbus-daemon > /dev/null; then
   echo "### D-Bus daemon is running"
else
   echo "### D-Bus daemon is not running"
fi

# Check if the D-Bus configuration files are correctly configured
if [ -f /etc/dbus-1/system.conf ]; then
   echo "### D-Bus system configuration file is present"
else
   echo "### D-Bus system configuration file is missing"
fi

# Make sure that the /var/run/dbus directory exists and is writable by the dbus-daemon process
if [ -d /var/run/dbus ]; then
   if [ -w /var/run/dbus ]; then
      echo "### /var/run/dbus is writable by the dbus-daemon process"
   else
      echo "### /var/run/dbus is not writable by the dbus-daemon process"
   fi
else
   echo "### /var/run/dbus does not exist"
fi

# Remove the /var/run/dbus/pid file if it exists
if [ -f /var/run/dbus/pid ]; then
   rm -f /var/run/dbus/pid
   echo "### /var/run/dbus/pid file removed"
else
   echo "### /var/run/dbus/pid file does not exist"
fi

echo "### Bus active"

cd /code/KJ-JS
cypress run --headed --browser chrome
echo "### after cypress run"

exec "$@"

当我运行Docker compose--profile测试时,数据库旋转良好,django启动并运行,但Cypress似乎无法连接。
它抱怨没有运行Dbus,所以我将其添加到上面所示的入口点中,并测试了它的所有组件,但错误消息仍然出现:

kj_import-web-test-1  | System check identified no issues (0 silenced).
kj_import-web-test-1  | December 28, 2022 - 02:32:40
kj_import-web-test-1  | Django version 2.2.28, using settings 'KJ_import.settings'
kj_import-web-test-1  | Starting development server at http://0.0.0.0:8000/
kj_import-web-test-1  | Quit the server with CONTROL-C.
kj_import-web-test-1  | [28/Dec/2022 02:32:42] "GET / HTTP/1.1" 200 5776
kj_import-web-test-1  | [28/Dec/2022 02:32:42] "GET /static/favicon.ico HTTP/1.1" 200 9662
kj_import-web-test-1  | [28/Dec/2022 02:32:46] "GET /docs/register/ HTTP/1.1" 200 6551
kj_import-web-test-1  | [28/Dec/2022 02:32:49] "GET / HTTP/1.1" 200 5776
kj_import-web-test-1  | [28/Dec/2022 02:33:02] "GET / HTTP/1.1" 200 5776
kj_import-cypress-1   | ### Create DBus
kj_import-cypress-1   | ### D-Bus daemon is running
kj_import-cypress-1   | ### D-Bus system configuration file is present
kj_import-cypress-1   | ### /var/run/dbus is writable by the dbus-daemon process
kj_import-cypress-1   | ### /var/run/dbus/pid file does not exist
kj_import-cypress-1   | ### Bus active
kj_import-cypress-1   | unix:path=/var/run/dbus/system_bus_socket,guid=1181acd37ea51796e63af6a863ab9ccf
kj_import-cypress-1   | [26:1228/013304.773071:ERROR:bus.cc(392)] Failed to connect to the bus: Address does not contain a colon
kj_import-cypress-1   | [26:1228/013304.773122:ERROR:bus.cc(392)] Failed to connect to the bus: Address does not contain a colon
kj_import-cypress-1   | [213:1228/013304.794142:ERROR:gpu_init.cc(453)] Passthrough is not supported, GL is swiftshader, ANGLE is 
kj_import-cypress-1   | Cypress could not verify that this server is running:
kj_import-cypress-1   | 
kj_import-cypress-1   |   > http://localhost:8000/
kj_import-cypress-1   | 
kj_import-cypress-1   | We are verifying this server because it has been configured as your `baseUrl`.
kj_import-cypress-1   | 
kj_import-cypress-1   | Cypress automatically waits until your server is accessible before running tests.
kj_import-cypress-1   | 
kj_import-cypress-1   | We will try connecting to it 3 more times...
kj_import-cypress-1   | We will try connecting to it 2 more times...
kj_import-cypress-1   | We will try connecting to it 1 more time...
kj_import-cypress-1   | 
kj_import-cypress-1   | Cypress failed to verify that your server is running.
kj_import-cypress-1   | 
kj_import-cypress-1   | Please start this server and then run Cypress again.
kj_import-cypress-1   | ### after cypress run
kj_import-cypress-1 exited with code 0
kj_import-web-test-1  | [28/Dec/2022 02:33:32] "GET / HTTP/1.1" 200 5776
kj_import-web-test-1  | [28/Dec/2022 02:34:02] "GET / HTTP/1.1" 200 5776
kj_import-web-test-1  | [28/Dec/2022 02:34:32] "GET / HTTP/1.1" 200 5776

请注意,服务器运行良好,您可以从上面的日志中看到(GET回复为200,甚至在Cypress容器开始尝试连接之前),我可以从本地浏览器访问它。
我错过了什么?
先谢了!

ivqmmu1c

ivqmmu1c1#

最后,事情可能很简单:容器中的localhost仅指容器本身,而不是主机。这个答案为我指明了正确的方向。
因此,为了正确指示Cypress观察/测试服务,需要传入的urldocker-compose.yml中的CYPRESS_baseUrl)的格式为http://[service-name]:[port],在我的例子中为http://web-test:8000/
请注意:

  • Cypress测试也需要指向那里,而且很可能
  • ALLOWED_HOSTS需要包含service-name

PS:可能还有第二个问题在起作用:在我的搜索中,我发现了这个reported bug,一些评论指出cypress/included:9.2.0映像可能受到影响。2我决定转到9.7.0

相关问题