我有一个scrapy
应用程序,正在尝试将其容器化。基本上,这是我的docker-compose.yml
文件:
version: '3'
services:
scrapper:
container_name: scrapper
build: .
ports:
- 80:80
depends_on:
- db
links:
- db
db:
volumes:
- ./scrapper/sql:/docker-entrypoint-initdb.d
image: postgres
container_name: postgres
restart: always
ports:
- 5432:5432
这是我的Dockerfile
:
FROM python:3
WORKDIR /usr/app
COPY requirements.txt .
RUN pip3 install -r requirements.txt
COPY . .
但是当我尝试使用以下命令执行应用程序时:docker run -it scrapper_scrapper scrapy crawl angeloni
,我收到此消息:
File "/usr/local/lib/python3.7/site-packages/scrapy/crawler.py", line 88, in crawl
yield self.engine.open_spider(self.spider, start_requests)
psycopg2.OperationalError: could not translate host name "db" to address: Name or service not known
为什么会这样?当我执行docker-compose ps
命令时,它显示:
Name Command State Ports
--------------------------------------------------------------------------
postgres docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp
scrapper python3 Exit 0
答案 0 :(得分:1)
运行docker-compose up
以启动db
时,该容器将在其网络下运行,该网络也是由docker compose创建的。因此,运行docker run ...
将无法连接到该实例,因为它不在同一网络上运行。但是您可以通过以下方式指定它:
docker run --network $network_name
要使docker网络可用,您可以运行:
docker networks ls
答案 1 :(得分:0)