我有一个项目,其中包含一个我正在尝试运行的docker设置。我的Dockerfile是:
FROM node:8-alpine
EXPOSE 3000
ARG NODE_ENV
ENV NODE_ENV $NODE_ENV
RUN mkdir /app
WORKDIR /app
ADD package.json yarn.lock seed.js /app/
RUN yarn --pure-lockfile
ADD . /app
CMD ["yarn seed"]
CMD ["yarn", "docker:start"]
和我的docker-compose:
version: "2"
services:
isawyou-server:
build: .
environment:
- MONGO_URI=mongodb://mongodb:27017/isawyou
volumes:
- .:/app
ports:
- "3000:3000"
depends_on:
- mongodb
mongodb:
image: mongo
ports:
- "27017:27017"
现在,对于 docker-compose.test,我正在尝试按照以下说明进行设置https://docs.docker.com/docker-cloud/builds/automated-testing/。我基本上想有一个不同的数据库进行测试。我尝试了不同的配置,但都不起作用。我得到的错误是:
yarn run v1.3.2
$ docker-compose -f docker-compose.yml -f docker-compose.test.yml up --abort-on-container-exit
Starting server_mongodb_1 ...
Starting server_mongodb_1 ... done
Recreating server_sut_1 ...
Recreating server_sut_1
Starting server_isawyou-server_1 ...
Recreating server_sut_1 ... error
ERROR: for server_sut_1 Cannot start service sut: driver failed programming external connectivity on endpoint server_sut_1 (bbStarting server_isawyou-server_1 ... done
ERROR: for sut Cannot start service sut: driver failed programming external connectivity on endpoint server_sut_1 (bbfc8b734ab6d4df2f3d3060245c81e0c88e945952dfe62c74f2aae6510d2cfe): Bind for 0.0.0.0:3000 failed: port is already allocated
ERROR: Encountered errors while bringing up the project.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
我的 docker-compose.test 当前看起来像:
version: "2"
services:
sut:
build: .
environment:
- MONGO_URI=mongodb://mongodb:27017/isawyou
volumes:
- .:/app
ports:
- "3000:3000"
depends_on:
- mongodb
command: yarn test
mongodb:
image: mongo
ports:
- "27017:27017"
答案1
我认为您必须向 docker-compose.test 文件添加数据库服务。说明您提供的信息表明“您可以在此文件中定义任意数量的链接服务。唯一的要求是定义 sut。”所以我认为缺少 mongodb 服务,无法找到测试所需的数据库。我找了一个docker-compose.test 示例这可能有助于理解它应该如何工作。
答案2
我需要使用 mongo 服务名称引用我的 mongo db URI。这个答案对我有帮助:https://stackoverflow.com/questions/25828021/unable-to-connect-to-mongodb-running-in-docker-container