Cannot connect to container within compose but host connectivity works - docker

Using the docker compose file
version: '3'
services:
sqlserver:
image: microsoft/mssql-server-linux:2017-latest
ports:
- 1401:1433
volumes:
- ./db:/tmp/data
environment:
- ACCEPT_EULA=Y
- "MSSQL_SA_PASSWORD='Password99'"
command:
- /tmp/data/run.sh
api:
build:
context: .
dockerfile: dockerfile
depends_on:
- sqlserver
links:
- sqlserver
ports:
- 5000:80
With appsettings.json connection string:
"ConnectionStrings": {
"Entities": "data source=(local),1401;initial catalog=DB_Test;user id=sa;password=Password99;enlist=False;multipleactiveresultsets=True;connect timeout=30;App=EntityFramework"
},
I get the error
api_1 | System.Data.SqlClient.SqlException (0x80131904):
A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: TCP
Provider, error: 40 - Could not open a connection to SQL Server)
A connection from a host running api instance to the sql server instance running in docker-compose works fine, so the SQL instance running in the container is fine.
Can anyone help with connectivity between api & sql inside docker?
Thanks!

Inside docker the hostname for the database will be "sqlserver" not localhost or 127.0.0.1

Related

Connection refused | Docker & ssh

I’m trying to connect to an ssh server from a docker container lifted by docker-compose.
On my localhost, I have enabled the port to use with "ufw allow 4222", I have placed port 4222 in my docker-compose.yml file.
I have also added the public key of my localhost to the container and to the authorized keys of the server, the problem is that it keeps failing me, someone knows that more I can check or take into account? thank you.
docker-compose.yml
version: '3.9'
services:
hermes:
depends_on:
- mongodb
build:
context: ./hermes-app/
container_name: hermes
tty: true
ports:
- "5000:5000"
- "4222"
environment:
- SLACK_CLIENT_ID=${SLACK_CLIENT_ID}
- SLACK_CLIENT_SECRET=${SLACK_CLIENT_SECRET}
- SLACK_SIGNING_SECRET=${SLACK_SIGNING_SECRET}
- SLACK_VERIFICATION_TOKEN=${SLACK_VERIFICATION_TOKEN}
- SSH_HOST=${SSH_HOST}
- SSH_USER=${SSH_USER}
- SSH_PORT=${SSH_PORT}
networks:
- netrmes
Error:
root#6d55aa960f46:/hermes-app# ssh root#my_server -p 4222
ssh: connect to host my_server port 4222: Connection refused
So you try to ssh from the container to your host system. The container knows nothing about my_server if it is not in netrmes network. You could use host network and then
ssh root#localhost -p 4222

Docker compose container is not visible inside pgAdmin4

I created a docker file to create a pgAdmin4 container and a postgres container.
version: '3.8'
services:
postgres:
container_name: pg_container
image: postgres
restart: always
environment:
POSTGRES_USER: webquiver
POSTGRES_PASSWORD: webquiver
POSTGRES_DB: quiver_db
ports:
- "5432:5432"
pgadmin:
container_name: pgadmin4_container
image: dpage/pgadmin4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: admin#admin.com
PGADMIN_DEFAULT_PASSWORD: admin
ports:
- "5050:80"
When running docker compose up I can go to the localhost:5050 to reach pgAdmin4 and login in with my credentials you can see in the code.
But when I use the dropdown menu for servers, it is empty. nothing is created. And I can not create any server there. It does not allow me to. I get the Error:
Unable to connect to server:
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and
accepting
TCP/IP connections on port 5432?
could not connect to server: Address not available
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
please help. THX ^^
greetings
I've reproduced your docker-compose file and everything is fine with it.
I'm guessing there is a misunderstanding how postgres and pgadmin are interacting with each other from scratch.
pgadmin is only the frontend that you can connect to a postgres database, it's not automatically searching for any existing postgres server.
I'm able to add the server inside the pgadmin with this host and port: postgres:5432 As inside the docker will resolve the service name as hostname postgres to the other service pgadmin.
This configuration will be lost, when the composition is restartet, as no volumes to persist the configuration are specified. Therefore, you have to follow steps from Importing-servers and repeat them every time you start the composition.
eg: build you own pgadmin image that specifies the json to import at startup
Use the docker compose build: property to specify a source
Ok after a night of sleep I found the issue. For the server Host I wrote the db name and not the real container name. So pgAdmin could not find the container.
Have a nice day. : )

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

The well-known 'Docker containers don't see each other' problem

I checked many forum entries (e.g. in stackoverflow too) but I still cannot figure out what the problem is with my docker-compose file.
So when I start my application (content-app) I got the following exception:
Failed to obtain JDBC Connection; nested exception is java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=content-database)(port=3306)(type=master) : Connection refused (Connection refused)
My application is a Spring boot app that tries to connect to the database, the JDBC URL is
url: jdbc:mariadb://content-database:3306/contentdb?autoReconnect=true
The Spring Boot app works fine as locally (when no docker is used) can connect to the local mariadb.
So the content-app container don't see the content-database container. I read that if I specify a network and I assign the containers to the network then they should be able to connect to each other.
When I connect to the running content-app container then I can telnet to content-database
root#894628d7bdd9:/# telnet content-database 3306
Trying 172.28.0.3...
Connected to content-database.
Escape character is '^]'.
n
5.5.5-10.4.3-MariaDB-1:10.4.3+maria~bionip/4X#wW/�#_9<b[~)N.:ymysql_native_passwordConnection closed by foreign host.
My docker-compose yaml file:
version: '3.3'
networks:
net_content:
services:
content-database:
image: content-database:latest
build:
context: .
dockerfile: ./database/Dockerfile
networks:
- net_content
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
content-redis:
image: content-redis:latest
build:
context: .
dockerfile: ./redis/Dockerfile
networks:
- net_content
content-app:
image: content-app:latest
build:
context: .
dockerfile: ./content/Dockerfile
networks:
- net_content
depends_on:
- "content-database"
Any hint please?
Thanks!
I guess MariaDB is listening on default port 3307, this means your application has to connect to this port as well. I guess this is the case as you are mapping the port 3307 of your container to "the outside".
Change the port in your connection string:
url: jdbc:mariadb://content-database:3307/contentdb?autoReconnect=true
You have to expose the port on which content-database is listening in the Dockerfile at ./database/Dockerfile

Docker Basic connection to local dynamodb configuration

I am using a symfony app and connecting to a local dynamodb instance in a docker container.
I keep getting AWS HTTP error: cURL error 7: Failed to connect to db port 8889: Connection refused error.
My docker-compose file is simply:
version: '3'
services:
web:
depends_on:
- db
build: .
ports:
- "8000:8000"
db:
image: "amazon/dynamodb-local"
ports:
- "8889:8889"
Honestly, I always get confused by the port mapping, but I don't think that should matter here. I'm trying to connect to http://db:8889. To make things simpler, I executed the following inside my web container:
# curl http://db:8889
curl: (7) Failed to connect to db port 8889: Connection refused
I'm kinda stumped, and I think this is such a simple thing most of the docs skim right over it. (or maybe I do)
The image documentation suggests the DynamoDB server runs on port 8000, so you should access it as http://db:8000. You don't have to publish it on port 8000 or on any port at all, but you need to use the container-side port number to reach it from other containers.

Resources