Access a container from another - docker

I see other posts but don't work in my case, maybe i'm beeing dumb.
I tried everything.
My last version of Dockerfile:
version: '3.4'
services:
bookmemoriesfrontend:
image: ${DOCKER_REGISTRY-}bookmemoriesfrontend
build:
context: .
dockerfile: Frontend\BookMemoriesFrontend\Dockerfile
ports:
- "8000:443"
depends_on:
- booksapi
- authorsapi
authorsapi:
image: ${DOCKER_REGISTRY-}authorsapi
build:
context: .
dockerfile: Services\AuthorsAPI\Dockerfile
ports:
- "8002:443"
booksapi:
image: ${DOCKER_REGISTRY-}booksapi
build:
context: .
dockerfile: Services\BooksAPI\Dockerfile
ports:
- "8001:443"
When I open CLI from BookMemoriesFrontend, I tried several curl commmand, when i try :
curl Booksapi/api/book
I think it will receive the api result and I dont receive nothing in result.
When I run CLI my machine:
curl -s https://localhost:8001/api/book
It gives the API result.
Please help me, I'm around this almost a week.

You should be able to use curl https://booksapi/api/book or curl https://booksapi:443/api/book.
Since it's https, it'll make the request on port 443 by default, which is what the container is listening on, so there's no need to specify a port.
Port 8001 is only used when you access the API from the host machine. When you're on the bridge network that docker-compose creates, you use the container ports directly.

Related

Can't access web container from outside (Windows Docker-Desktop)

i'm using Docker-Desktop on Windows and i'm trying to get running 3 containers inside docker-desktop.
After few research and test, i get the 3 container running [WEB - API - DB], everything seems to compile/run without issue in the logs but i'can't access my web container from outside.
Here's my dockerfile and docker-compose, what did i miss or get wrong ?
[WEB] dockerfile
FROM node:16.17.0-bullseye-slim
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
#EXPOSE 4200 (the issue is the same with or without this line)
CMD ["npm", "run", "start"]
[API] dockerfile
FROM openjdk:17.0.1-jdk-slim
WORKDIR /app
COPY ./target/test-0.0.1-SNAPSHOT.jar /app
#EXPOSE 2022 (the issue is the same with or without this line)
CMD ["java", "-jar", "test-0.0.1-SNAPSHOT.jar"]
Docker-compose file
version: "3.8"
services:
### FRONTEND ###
web:
container_name: wallet-web
restart: always
build: ./frontend
ports:
- "80:4200"
depends_on:
- "api"
networks:
customnetwork:
ipv4_address: 172.20.0.12
#networks:
# - "api"
# - "web"
### BACKEND ###
api:
container_name: wallet-api
restart: always
build: ./backend
ports:
- "2022:2022"
depends_on:
- "db"
networks:
customnetwork:
ipv4_address: 172.20.0.11
#networks:
# - "api"
# - "web"
### DATABASE ###
db:
container_name: wallet-db
restart: always
image: postgres
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=postgres
networks:
customnetwork:
ipv4_address: 172.20.0.10
#networks:
# - "api"
# - "web"
networks:
customnetwork:
driver: bridge
ipam:
config:
- subnet: 172.20.0.0/16
gateway: 172.20.0.1
# api:
# web:
Listening on:
enter image description here
I found several issue similar to mine but the solution didn't worked for me.
If i understand you are trying to access on port 80. To do that, you have to map your container port 4200 to 80 in yaml file 80:4200 instead of 4200:4200.
https://docs.docker.com/config/containers/container-networking/
Have you looked in the browsers development console, if there comes any error. Your docker-compose seems not to have any issue.
How ever lets try to debug it:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6245eaffd67e nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:4200->80/tcp test-api-1
copy the container id then execute:
docker exec -it 6245eaffd67e bin/bash
Now you are inside the container. Instead of the id you can use also the containers name.
curl http://localhost:80
Note: in my case here i just create a container from an nginx image.
In your case use the port where your app is running. Control it in your code if you arent sure. A lot of Javascript-frameworks start default on 3000.
If you get an error: curl command not found, install it in your image:
FROM node:16.17.0-bullseye-slim
USER root # to install dependencies you need sudo permissions su we tell the image that it is root
RUN apt update -y && apt install curl -y
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
#EXPOSE 4200 (the issue is the same with or without this line)
USER node # we dont want to execute the image as root so we put user node (this user is defined in the node:16.17.0-bullseye-slim image)
CMD ["npm", "run", "start"]
Now the curl should work (if it doesnt already).
The same should work from your host.
Here is an important thing:
The localhost, always refers to the fisical computer, or the container itselfs where you are refering. Every container and your PC have localhost and they are not the same.
In the docker-compose you just map the port host/container, so your PC (host) where docker is running can access the docker network from the host on the host port you defined, inside the port of the container.
If you cant still access from your host, try to change the host ports 2022, 4200 ecc. Could be possible that something conflicts on your Windows machine.
It happens sometimes that the docker networks can create some conflicts.
Execute a docker-compose down, so it should be delete and recreated.
Still not working?
Reset docker-desktop to factory settings, control if you have last version (this is always better).
If all this doesnt help, let me know so we can debugg further.
For the sake of clarity i post you here the docker-compose which i used to check. I just used nginx to test the ports as i dont have your images.
version: "3.8"
services:
### FRONTEND ###
web:
restart: always
image: nginx
ports:
- "4200:80"
depends_on:
- "api"
networks:
- "web"
### BACKEND ###
api:
restart: always
image: nginx
ports:
- "2022:80"
depends_on:
- "db"
networks:
- "api"
- "web"
### DATABASE ###
db:
restart: always
image: postgres
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=postgres
networks:
- "api"
networks:
api:
web:
```
Update:
You can log what happens in the conatiner like so:
```
docker logs containerid/name
```
If you are using Visualcode there is excellent extension for docker build also by Microsoft:
Just search docker in the extensions. Has something like 20.000.000 downloads and can help you a lot debugging containers ecc. After installing it you see the dockericon on the left toolbar.
If you can see directly the errors that occurs in the logs, maybe you can post them partially. So it would be possible to understand. Please tell also something about your Frontendapp architecture, (react-app, angular). There are some frameworks that need to be startet on 0.0.0.0 instead of 127.0.0.1 or they dont work.

Docker cURL one container from another

Theres a good chance I'm being dumb here, but I have the following endpoint which works in the browser and returns a result:
http://localhost:8000/api/ebay/getSellers
This is on my app Docker container, and I want to use cURL on my api container to retrieve the result.
My dockerfile looks like so:
services:
server:
build:
context: .
dockerfile: dockerfiles/prod/nginx.dockerfile
ports:
- "8000:80"
volumes:
- ./src:/var/www/html
- ./nginx/prod/nginx.conf:/etc/nginx/conf.d/default.conf:ro
container_name: toolkit-server
image: my/toolkit-server:latest
depends_on:
- app
- api
app:
build:
context: .
dockerfile: dockerfiles/prod/php.dockerfile
volumes:
- ./src:/var/www/html:delegated
container_name: toolkit-app
image: my/toolkit-app:latest
api:
build:
context: .
dockerfile: dockerfiles/prod/api.dockerfile
ports:
- "8100:80"
volumes:
- ./api:/var/www/html/
container_name: toolkit-api
image: my/toolkit-api:latest
I've tried using cURL like so:
http://localhost:8000/api/ebay/getSellers
http://app:8000/api/ebay/getSellers
http://toolkit-app:8000/api/ebay/getSellers
But the respose I get is always:
Failed to connect to toolkit-app port 8000: Connection refused
Is there a way to do this?
Edit:
Sorry to clarify, my curl is within the api container and I want to query the app container
The api container is mapping the services port 80 to the external port 8100.
Try to request http://localhost:8100/
That's because you're querying the wrong port.
In your compose file, you have this configuration for your app container :
ports:
- "8000:80"
This block means that you're mapping the port 80 from the container to the port 8000 on your host. It is always port_on_host:port_on_container
So if you want to access the app container from the api container, you need to curl with this url :
curl http://app:80/api/ebay/getSellers

docker-compose - using localhost inside and outside of contaniner aspnet core

I have 2 serviceA and serviceB that I use from my pc at
https://localhost:5001/servicea
https://localhost:5002/serviceb
The services could also call each other.
Now I would like to use docker-compose
version: '2.4'
services:
servicea:
image: ${DOCKER_REGISTRY-}servicea
ports:
- "5001:5001"
environment:
- ASPNETCORE_URLS=https://host.docker.internal:5001
build:
network: host
context: .
dockerfile: Service1/Dockerfile
serviceb:
image: ${DOCKER_REGISTRY-}serviceb
ports:
- "5002:5002"
environment:
- ASPNETCORE_URLS=https://host.docker.internal:5002
build:
network: host
context: .
dockerfile: Service2/Dockerfile
The services are accessible from my pc via localhost, but not from inside of one container to another
If I log into serviceA container:
curl --insecure https://host.docker.internal:5002/serviceB - working
curl --insecure https://127.0.0.1:5002/serviceB - not working
curl --insecure https://localhost:5002/serviceB - not working (this is what I need)
I have a default dotnet core 3.1 api, using linux containers running in wsl2 with the following default dockerfile
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 5001
...
Question: How can I (inside a container) access other containers using localhost, and still be able to access the containers from my pc using localhost (outside docker)?
I've tried various combination of setting the ASPNETCORE_URLS and using v3.4 with network_mode: host with no luck.
Thanks for any help!
You can set aliases to your service and use them in your services. Or you can just use network_mode: "host".

Connection between docker containers: need to put gateway instead of name of container, why?

I was testing things for my own and I had problem: Trying to connect a nodeexpress container(app) to a mongo container(database), I can connect to mongo from MongoCompose if I connect to localhost:27017 but cant into the container of nodeexpress with mongoose configuration url to connect database like this 'mongodb://localhost:27017/dbtest'.
So I look up at SO some solutions (like this) and answers what I see was instead of 'mongodb://localhost:27017/dbtest' I need to write the name of the my container 'mongodb://mymongo:27017/dbtest', but for me this didnt work, only recieve ECONNREFUSED error.
Containers was in the same network, here is my dockerfile and docker-compose file.
Dockerfile
#node 8.16.2
FROM node:8.16.2
COPY . /app
WORKDIR /app
RUN npm install
EXPOSE 3000
CMD ["npm","start"]
docker-compose.yaml
version: "3.7"
services:
db:
image: mongo
ports:
- 27017:27017
networks:
- testing
app:
build:
context: .
dockerfile: Dockerfile
networks:
- testing
networks:
testing:
I solved this problem like this mongodb://172.17.0.1:27017/dbtest where 172.17.0.1 is the Gateway of the network that are the containers.
Can someone explain this behavior and if it is correct ?
Platform Linux
Where did you get the name mymongo from? You have defined the name of mongodb service as db in your compose file. So use the connection string 'mongodb://db:27017/dbtest'
version: "3.7"
services:
db: ---------------> This is the name of your mongo service
image: mongo
ports:
- 27017:27017
networks:
- testing
app:
build:
context: .
dockerfile: Dockerfile
networks:
- testing
networks:
testing:

Does anyone know how to point a docker container to ngrok?

I have discourse which i installed through docker, however, i am trying to see if i can connect that through Ngrok. Anyone familiar with Ngrok?
The app is running through a docker container at localhost:9292
when i try to run ngrok it does not work.
ngrok http -host-header=rewrite localhost:9292
Answering my questions in hopes that i can help some else.
So when i installed discourse through docker, i had to ssh into my docker container. I then had to install nginx and config it to point my box IP address to my private network. Once i reload it work, however, I was getting a blank page when i hooked it to ngrok. SO....i had to turn off the security protocols in Chrome so that it allowed ngrok to display my app. Let me know if anyone have any questions and id love to expand on this.
this composer works for me
version: '3'
services:
yourwebserver:
build:
context: ./
dockerfile: ...
target: ...
container_name: yourwebserver
volumes:
- ...
ports:
- ...
extra_hosts:
- 'host.docker.internal:host-gateway'
depends_on:
- ngrok
ngrok:
image: ngrok/ngrok:alpine
environment:
NGROK_AUTHTOKEN: '...'
command: 'http yourwebserver:80'
ports:
- '4040:4040'
expose:
- '4040'

Resources