Docker cURL one container from another - docker

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

Related

Access a container from another

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.

Nuxt.js 500 NuxtServerError under docker-compose

my system contains 3 dockers:
mongodb
api backend, built with Nestjs
web application, build with Nuxt.js
the mongo and the backend seems to be working, because i can access the swagger at localhost:3000/api/.
the Nuxtjs web app is failing, and i'm getting 500 Nuxtserver error.
Dockerfile (for the web app):
FROM node:12.13-alpine
ENV APP_ROOT /src
RUN mkdir ${APP_ROOT}
WORKDIR ${APP_ROOT}
ADD . ${APP_ROOT}
RUN npm install
RUN npm run build
ENV HOST 0.0.0.0
EXPOSE 4000
docker-compose.yml:
version: "3"
services:
# backend nestjs app
api:
image: nestjs-api-server
container_name: my-api
depends_on:
- db
restart: unless-stopped
environment:
- NODE_ENV=production
ports:
- 3000:3001
networks:
- mynet
links:
- db
# mongodb
db:
image: mongo
container_name: db_mongo
restart: unless-stopped
volumes:
- ~/data/:/data/db
ports:
- 27017:27017
networks:
- mynet
# front web app, nuxt.js
web:
image: nuxtjs-web-app
container_name: my-web
depends_on:
- api
restart: always
ports:
- 4000:4000
environment:
- BASE_URL=http://localhost:3000/api
command:
"npm run start"
networks:
- mynet
networks:
mynet:
driver: bridge
Looks like the nuxtjs app cannot connect to the api. in the log i see:
ERROR connect ECONNREFUSED 127.0.0.1:3000
But why? the swagger (coming from the same api) works fine on http://localhost:3000/api/#/.
Any idea?
environment:
- BASE_URL=http://localhost:3000/api
localhost in a container means inside that particular container. i.e., it will try to resolve port 3000 in my-web container itself.
Basically from front-end you cannot do container communication. May be you can communicate via public hostname or ip or you can make use of extra_hosts concept in docker-compose to resolve localhost.
Got it. The problem was in nuxtServerInit. This is a very special method on vuex, and it is running in the server. i called $axios from it, and i guess you can't do that.
once i commented that method, it's working fine.

Making an HTTP request within the same Docker network

I have a few services running in different docker containers, as per my docker-compose:
version: '3'
services:
rest:
build:
context: './service/'
image: persian_rest:latest
container_name: persian_rest
ports:
- 8080:8080
networks:
- persian_net
volumes:
- persian_volume:/data
scheduler:
build:
context: './scheduler/'
image: persian_scheduler:latest
container_name: persian_scheduler
networks:
- persian_net
ui:
build:
context: './ui/'
image: persian_ui:latest
container_name: persian_ui
ports:
- 5000:5000
networks:
- persian_net
database:
image: 'mongo:latest'
container_name: 'persian_database'
networks:
- persian_net
environment:
- MONGO_INITDB_ROOT_USERNAME=persian_admin
- MONGO_INITDB_ROOT_PASSWORD=123456
ports:
- 27017:27017
volumes:
- persian_volume:/data
volumes:
persian_volume:
networks:
persian_net:
driver: bridge
I have my UI persian_ui service making HTTP request to the REST service persian_rest. I thought that since they were in the same network, I would just make a request to http://persian_rest:8080/api
However, when I do make that request, it fails to find that resource:
Does anyone know why my containers joined by the same network are not able to perform requests?
Currently you are looking at a webpage at localhost:5000. You requested the webpage from the server localhost:5000 and it complied and sent you a webpage which is now sitting on your computer.
If you now want to access an API on the same server as the webpage, you can make another request to localhost but this time port 8080. localhost:8080/api.
The webpage in the browser is on the client-side, and the names you've given your containers are for reference inside the server. From outside the server, currently the reference is localhost.

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:

How to access docker container using localhost address

I am trying to access a docker container from another container using localhost address.
The compose file is pretty simple. Both containers ports are exposed.
There are no problems when building.
In my host machine I can successfully execute curl http://localhost:8124/ and get a response.
But inside the django_container when trying the same command I get Connection refused error.
I tried adding them in the same network, still result didn't change.
Well if I try to execute with the internal ip of that container like curl 'http://172.27.0.2:8123/' I get the response.
Is this the default behavior? How can I reach clickhouse_container using localhost?
version: '3'
services:
django:
container_name: django_container
build: ./django
ports:
- "8007:8000"
links:
- clickhouse:clickhouse
volumes:
- ./django:/usr/src/run
command: bash /usr/src/run/run.sh
clickhouse:
container_name: clickhouse_container
build: ./clickhouse
ports:
- "9001:9000"
- "8124:8123"
- "9010:9009"
So with this line here - "8124:8123" you're mapping the port of clickhouse container to localhost 8124. Which allows you to access clickhouse from localhost at port 8124.
If you want to hit clickhouse container from within the dockerhost network you have to use the hostname for the container. This is what I like to do:
version: '3'
services:
django:
hostname: djano
container_name: django
build: ./django
ports:
- "8007:8000"
links:
- clickhouse:clickhouse
volumes:
- ./django:/usr/src/run
command: bash /usr/src/run/run.sh
clickhouse:
hostname: clickhouse
container_name: clickhouse
build: ./clickhouse
ports:
- "9001:9000"
- "8124:8123"
- "9010:9009"
If you make the changes like I have made above you should be able to access clickhouse from within the django container like this curl http://clickhouse:8123.
As in #Billy Ferguson's answer, you can visit using localhost in host machine just because: you define a port mapping to route localhost:8124 to clickhouse:8123.
But when from other container(django), you can't. But if you insist, there is a ugly workaround: share host's network namespace with network_mode, but with this the django container will just share all network of host.
services:
django:
hostname: djano
container_name: django
build: ./django
ports:
- "8007:8000"
links:
- clickhouse:clickhouse
volumes:
- ./django:/usr/src/run
command: bash /usr/src/run/run.sh
network_mode: "host"
It depends of config.xml settings. If in config.xml <listen_host> 0.0.0.0</listen_host> you can use clickhouse-client -h your_ip --port 9001

Resources