Is it possible to see other containers which are running in the same host from the intermediate containers which would be created by Dockerfile
?
I need to connect to my dockerized database, it is already up and running. From my Dockerfile
How can I connect to it? is it possible or not?
postgres.docker-compose.yml
this is my postgres.docker-compose.yml
which is the first container that I run it:
version: '3.7'
services:
postgres:
image: postgres:13
env_file:
- .postgres.env
restart: always
networks:
- take-report
networks:
take-report:
name: take-report
A simple container. Please note that I can connect to it outside of Dockerfile
but I want to connect to this dockerized postgres from This Dockerfile
:
Dockerfile
# Note: I had issues with npm ci, therefore I did it once in a temp Dockerfile and create a new base image with the installed 3rd party packages and I put this name for it: take-report:dep
FROM take-report:dep as build_stage
ARG DATABASE_URL
ENV DATABASE_URL=$DATABASE_URL
# README: Because WORKDIR is set in the take-report:dep I ignore to use it here again
# WORKDIR /app
COPY prisma ./prisma/
COPY . .
RUN npx prisma generate
RUN npm run build
# Cannot connect to Database from here even due the Database_URL is correct.
RUN echo $DATABASE_URL
# error message: Error: P1001: Can't reach database server at `postgres`:`5432`
RUN npm run prisma:dev
RUN npm prune --production
FROM node:16.14.0-alpine3.15
WORKDIR /app
COPY --from=build_stage /app/node_modules ./node_modules
COPY --from=build_stage /app/package*.json ./
COPY --from=build_stage /app/tsconfig*.json ./
COPY --from=build_stage /app/dist ./dist
COPY --from=build_stage /app/dist/prisma ./prisma
COPY --from=build_stage /app/prisma/schema.prisma ./prisma/schema.prisma
# COPY --from=build_stage /app/prisma ./prisma
EXPOSE $APP_PORT
docker-compose.yml
version: '3.7'
services:
take-report:
image: take-report:v1
restart: unless-stopped
build:
context: .
dockerfile: Dockerfile
args:
- DATABASE_URL
ports:
- ${APP_EXPOSED_PORT}:$APP_PORT
env_file:
- .env
networks:
- take-report
- traefik_default
labels:
- "traefik.enable=true"
command: npm run start:prod
networks:
traefik_default:
external: true
take-report:
external: true
As you can see I put the take-report
and postgres
containers in the same network. In this way they can see each other. Note that the created container by docker-compose.yml
file can see and connect to the DATABASE_URL
. So I guess all I need is to specify the intermediate containers' network that docker creates to build my custom image. In other word I want to some how tell docker to use which external network while building this custom image with written Dockerfile
.
Is that possible? In case that something was not clear please tell me to clarify it Thanks regardless.
Edit #1 - Add more info:
I have to say that when I issued docker-compose -f postgres.docker-compose.yml up
it will creates the take-report
network for me and I can connect to it in the docker-compose.yml
.
The second info: docker-compose.yml
can see the postgres because they're in the same network, I meant take-report
network.