Problem with ports in docker-compose file - docker

I am trying to link my api with my webapp but is doesn't seem to work.
I have this error
[HPM] Error occurred while trying to proxy request /users/me from
localhost:3000 to http://localhost:8080 (ECONNREFUSED)
(https://nodejs.org/api/errors.html#errors_common_system_errors)
When I try to sign in, it doesn't find the users.
Here is the contents of my docker-compose.yml file
version: '3'
services:
api:
build: ./web3-2019-api
ports:
- "8080:8080"
webapp:
build: ./web3-2019-webapp
ports:
- "3000:3000"
links:
- api

Try to connect via docker host api:8080 instead of localhost.
If you connect via localhost from webapp it expects 8080 to be running in webapp docker, but api is another docker and you should connect via api:8080. Though both are running in same machine they are virtual machines and you should connect via respective docker name within docker network

Related

Docker compose a Server API and Nuxt Client?

I'm trying since 2 weeks to make a docker compose that link an api server (strapi) and a Nuxt app (client)
Nuxt make request to the api server to get data. But I don't know how to make them communicate to share data and make request.
I tried a lot of config but don't work.
My docker-compose.yml:
version: "3.9"
services:
client:
image: nuxt-client
networks:
- my-network
environment:
STRAPI_URL: server:4040
depends_on:
- server
server:
image: strapi-server
networks:
- my-network
volumes:
- ./data:/app/.tmp/
networks:
my-network:
driver: bridge
I try my nuxt app to make a request on the server with the port 4040
In environment, nuxt app take STRAPI_URL which is the url of the api (example: http://localhost:4040)
But the request on the network tab is 404
I want to put this docker compose in my nginx proxy manager, and i don't want to make port outside containers.
Any help ? :)

Is it possible for webservice to read apiservice via container_name on docker?e.g. http://container_name:port

I'm new to docker and I composed a web service and other services used in the web in a docker-compose file. I wonder if it's possible to have access to the services(e.g. api service) for the web service via container_name.
like http://container_name:8080. Container_name is specified in docker-compose file, and web service on docker containers can read other service via http://localhost:port. I want to replace localhost to container_name, can docker do this mapping via some configuration? I tried depends_on and links and none of them work.
Part of my docker-compose.yml:
version: "3.7"
services:
mywebservice:
container_name: mywebservice
ports:
- "8080:80"
depends_on:
- myapiservice
myapiservice:
container_name:myapiservice
ports:
- "8081:80"
You can resolve your container name to the container ip via the hosts file.
192.168.10.10 mywebservice
You can have this file in your application source and get docker to copy it to /etc

Minio / Keycloak integration: connection refused

I am trying to connect MinIO with KeyCloak and I follow the instructions provided in this documentation:
https://github.com/minio/minio/blob/master/docs/sts/keycloak.md
What I have done so far is deploy a Docker container for the MinIO server, another one for the MinioClient and a third one used for the KeyCloak server.
As you can see in the following snippet the configuration of the Minio Client container is done correctly, since I can list the buckets available in the Minio Server:
mc ls myminio
[2020-05-14 11:54:59 UTC] 0B bucket1/
[2020-05-06 12:23:01 UTC] 0B bucket2/
I have an issue arising when I try to configure MinIO as depicted in step 3 (Configure MinIO) of the documentation. In more detail, the command that I run is this one:
mc admin config set myminio identity_openid config_url="http://localhost:8080/auth/realms/demo/.well-known/openid-configuration" client_id="account"
And the error I get is this one:
mc: <ERROR> Cannot set 'identity_openid config_url=http://localhost:8080/auth/realms/demo/.well-known/openid-configuration client_id=account' to server. Get http://localhost:8080/auth/realms/demo/.well-known/openid-configuration: dial tcp 127.0.0.1:8080: connect: connection refused.
When I curl this address http://localhost:8080/auth/realms/demo/.well-known/openid-configuration from the MinIO Client container though, I retrieve the JSON file.
Turns out, all I had to do is change the localhost in the config_url, from localhost to the IP of the KeyCloak container (172.17.0.3).
This is just a temporary solution that works for now, but I will continue searching for something more concrete than just hardcoding the IP.
When I figure out the solution, this answer will be updated.
Update
I had to create a docker-compose.yml file as the one below in order to overcome the issues without having to manually place the IP of the KeyCloak container.
version: '2'
services:
miniod:
image: minio/minio
restart: always
container_name: miniod
ports:
- 9000:9000
volumes:
- "C:/data:/data"
environment:
- "MINIO_ACCESS_KEY=access_key"
- "MINIO_SECRET_KEY=secret_key"
command: ["server", "/data"]
networks:
- minionw
mcd:
image: minio/mc
container_name: mcd
networks:
- minionw
kcd:
image: quay.io/keycloak/keycloak:10.0.1
container_name: kcd
restart: always
ports:
- 8080:8080
environment:
- "KEYCLOAK_USER=admin"
- "KEYCLOAK_PASSWORD=pass"
networks:
- minionw
networks:
minionw:
driver: "bridge"
Connection refused occurs when a port is not accessible on the hostname or IP we specified.
Please try exposing the port using --expose flag along with the port number which you wish to expose when using the docker CLI. Then being exposed, you can access on it on localhost

How to access application run on local machine from container?

I have an application running locally and I have a docker container running via docker compose:
swagger:
image: swaggerapi/swagger-ui:v3.23.5
ports:
- "7171:8080"
networks:
- dockernet
expose:
- 8080
environment:
- URL=http://192.168.10.20:8080/actions/v3/api-docs
192.168.10.20 is my localhost.
if I access http://192.168.10.20:8080/actions/v3/api-docs via the browser I see the response but the swagger service can't access it.
How to fix it?

Conusume API from a client docker container to the server container

I have two different projects running on different docker containsers. Below the two YML files:
FILE webserver-api/docker-compose.yml
version: "3.1"
services:
webserver:
image: nginx:alpine
container_name: webserver-api
working_dir: /application
volumes:
- .:/application
- ./docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8005:80"
FILE client-app/docker-compose.yml
version: '3'
services:
web:
container_name: client-app
build:
context: ./
dockerfile: deploy/web.docker
volumes:
- ./:/var/www
ports:
- "8010:80"
links:
- app
app: [...]
database: [...]
From the client-app I would like to call the webserver-api.
When I'm trying to consume the API from webserver-api I'm getting the message "cURL error connection refused" or timeout error.
For example
$response = file_get_contents('http:/localhost:8005/api/test');
I tried also to replace the localhost with the IP of the webserver-api container like this:
$response = file_get_contents('http://172.25.0.2:8005/api/test');
But still I get a timeout connection error.
Which is the correct URL of the server container to call form the client container? Or how to set the host URL?
Thanks a lot for the help and time.
You need create a network first. Then use this network for both your client and server docker compose. Otherwise the network is isolated.
Another approach is expose the port of server to localhost and connect to localhost from client side.
As per the docker-compose documentation
By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
So ideally if your service are interdependent you should put them in a single compose file. In that case you could have accessed your service directly by name and container port
http://webserver/api/test
But since they are in separate compose file, you can access the service via host mapped port
$response = file_get_contents('http://localhost:8005/api/test');
it should also work.
To debug you can check
If port binding to 8005 is happening on your host.
The endpoint specified is correct and accessible from host.
Finally I figured it out.
By default docker creates a network called (in my case) webserver-api_default where webserver-api is the name of the folder that contains the YML file [projectname]_default.
On the client-app/docker-compose.yml of the client I had to specify which network to join:
version: '3'
networks:
default:
external:
name: webserver-api_default
web:
container_name: client-app
build:
context: ./
dockerfile: deploy/web.docker
volumes:
- ./:/var/www
ports:
- "8010:80"
links:
- app
app: [...]
database: [...]
And from the client container I have to make the call to the URL:
$response = file_get_contents('http://webserver-api:8005/api/test');
Where webserver-api is the name of the server container and not the name of the network.
https://docs.docker.com/compose/networking/

Resources