The problem is you should not be running python base.py
as part of the RUN
directive.
The RUN
directive is executed only when you are building the image. The postgres
container is not running at this point, nor has the network been created. Instead you want to use the CMD
directive.
Change the Dockerfile
to this:
FROM ubuntu:16.04
RUN apt-get update
RUN apt-get -y install python-pip
RUN apt-get update
RUN pip install --upgrade pip
RUN pip install psycopg2-binary
COPY base.py base.py
CMD ["python", "base.py"]
The above should result in the hostname db
to be resolved. However if your python code doesn't have any reconnection logic for connecting to the database the container will likely still error out. This because the postgres
container will be running but the database won't be ready to accept connections.
This can be temporarily fixed by adding restart: always
to your docker-compose.yml
.
version: '3'
services:
db:
image: 'postgres:latest'
expose:
- "5432"
environment:
POSTGRES_PASSWORD: pw1234
POSTGRES_DB: base123
aprrka:
restart: always
build: .
depends_on:
- db
Hopefully this will get you up and running.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…