How to connect superset to postgresql - The port is closed - database

My operating system is Linux.
I am going to connect Superset to PostgreSQL.
PostgreSQL port is open and its value is 5432.
PostgreSQL is also running and not closed.
Unfortunately, after a day of research on the Internet, I could not solve the problem and it gives the following error:
The port is closed.
Database port:
command: lsof -i TCP:5432
python3 13127 user 13u IPv4 279806 0t0 TCP localhost:40166->localhost:postgresql (ESTABLISHED)
python3 13127 user 14u IPv4 274261 0t0 TCP localhost:38814->localhost:postgresql (ESTABLISHED)
Please help me, I am a beginner, but I searched a lot and did not get any results.

Since you're running Superset in a docker container, you can't use 127.0.0.1 nor localhost, since they resolve to the container, not the host. For the host, use host.docker.internal

I had a similar problem using docker compose. Port is closed can be due to networking problem. Host.docker.internal doesn’t worked for me on Ubuntu 22. I would like to recommend to not follow official doc and use better approach with single docker image to start. Instead of running 5 containers by compose, run everything in one. Use official docker image, here image. Than modify docker file as follows to install custom db driver:
FROM apache/superset
USER root
RUN pip install mysqlclient
RUN pip install sqlalchemy-redshift
USER superset
Second step is to build new image based on docker file description. To avoid networking problems start both containers on same network (superset, your db) easier is to use host network. I used this on Google cloud example as follow:
docker run -d --network host --name superset supers
The same command to start container with your database. —network host. This solved my problems. More about in whole step to step tutorial: medium or here blog

From the configuration file, you set port 5432, but it does not mean that your pg service is available

Related

Docker Containers cannot communicate with Host Database

I know this question has been asked before, but I haven't managed to solve it from those answers.
System spec: I am running the server on Ubuntu 22.10 docker version is 20.10.16 and docker-compose version is 1.29.2
What I want to achieve: I have nextcloud running as a docker container and have MariaDB installed on the host machine uncontainerized. I want to use a database I created in MariaDB by nextcloud in docker container. But Hostname is always incorrect, and I get the following error.
Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused
Troubleshooting I've tried so far
Option 1: I added an extra host to my docker compose file so it can listen to the host system. Here's how my docker compose file like
version: '2'
services:
app:
image: nextcloud
restart: always
ports:
- 8080:80
volumes:
- /home/ritzz/nextcloud:/var/www/html
extra_hosts:
- host.docker.internal:host-gateway
However, when adding database if I use host.docker.internal as hostname I still get the error mentioned above.
Option2: Using docker host IP as database hostname. I used the following command to find out my host ip which was 172.17.0.1
ip addr show docker0
However, again adding 172.17.0.1 or 172.17.0.1:3306 returns with the same error.
Option 3: I saw an option on the Internet to use network_mode: host to make the container use the same network as host. However, since docker container uses port 80 and on my host I can't use port 80. This method won't work for me I assume.
Additional Troubleshooting
I made sure Mariadb is with command sudo systemctl status mariadb as well as checked that it's listening to port 3306 using command sudo netstat -tlnp I also logged in to the database with the user and pass using command sudo mysql -u<username> -p<password> <database> and I can login successfully.
I am at the edge with this. Hopefully someone else can help me out

Docker container can't connect to Redis

I have a Docker container running a C application using hiredis, which should write data to a Redis server exposed at the default address and port running locally on the same Linux device at 127.0.0.1:6379 .
The Redis server is running in a different Docker container. I start this container running, exposing port 6379 as follows : sudo docker run --name redis_container -d -p 6379:6379 40c68ed3a4d2
redsi-cli can connect to this via 127.0.0.1:6379 without issues.
However, no matter what I try, my container which should write to the Redis gets a Redis connection refused error from the C code all the time. This was my last attempt at running the container : sudo docker run --expose=6379 -i 7340dfee8ea5
What exactly am I missing here? Thanks
The C client is running inside a container, that means 127.0.0.1 points to the container itself, not to your host. You should configure the redis client to redis_container:6379 as that is the name you have used when docker run the redis container. More about this here
Besides, both containers need to be inside the same docker network. Use the following command to create a simple network
docker network create my-net
and add --network my-net to both docker run commands (redis client and redis server)
You can read more about docker network here

anzograph - Cannot connect to the admin console

I set up Anzograph DB Free Edition in Docker Desktop on my Mac, and (per the commands below) ran it. But I can’t connect to the admin console.
docker pull cambridgesemantics/anzograph
docker run cambridgesemantics/anzograph
When I use the inspect feature in Docker Desktop’s Dashboard, all of the ports for the running image are “not binded”. I would have expected to connect on port 5600 but that doesn’t work – not with localhost, not with 0.0.0.0, not with 127.0.0.1 …
Am I perhaps missing some pre-requisite? I allocated 8 GB of memory to Docker.
From the information you documented, what you are seeing is true as you have not documented in your command the specific ports.
What you entered was the following,
docker run cambridgesemantics/anzograph
What you should run to address this, which is documented on the download page for Anzograph, specifying the ports to install,
docker run -d -p 80:8080 -p 443:8443 --name=anzograph cambridgesemantics/anzograph:latest
AnzoGraph frontend binds to port 8443 (https) and 8080 (http),
AnzoGraph DB binds to port 5600 (gRPC DB management) and 5700 (gRPC DB query) inside the docker container.
Docker Desktop for MAC is mapping these container internal ports to ports on localhost. If you do not tell docker how to map those ports, it uses a random strategy to allocate those ports on localhost. In specifying the mapping
docker run -d -p 80:8080 -p 443:8443 -p 5600:5600 -p 5700:5700 --name=anzograph cambridgesemantics/anzograph:2.1.1-latest
you tell docker what localhost ports to use ( -p { localhost port } : { port inside of container} )
Many users new to docker struggle, when they use for example Kitematic, or similar UIs, that make it simple to deploy a running docker container, however they face complexities understanding and determining these random ports.
So if you are new to docker, and you do not want to use kubernetes yet, please use the command line to specify the localhost ports - it ends up being easier.

Connect to docker sqlserver via ssh

I've created a docker container that contains a mssql Database. On the command line ip a gives an ip address for the container, however trying to ssh into it username#docker_ip_address yields ssh: connect to host ip_address port 22: Connection refused. So I'm wondering if I am even able to ssh into the container so I don't have to always be using the docker tool docker exec .... and if so how would I go about doing that?
To ssh into container you should full-fill followings
SSH server(Openssh) should be installed within the container and ssh service should be running
Port 22 should be published from container (when you run the container).more info here > Publish ports on Docker
docker ps command should display mapped ports 22
Hope above information helps for you to understand the situation...
If your container contains a database server, the normal way to interact with will be through an SQL client that connects to it; Google suggests SQL Server Management Studio and that connector libraries exist for popular languages. I'm not clear what you would do given a shell in the container, and my main recommendation here would be to focus on working with the server in the normal way.
Docker containers normally run a single process, and that's normally the main server process. In this case, the container runs only SQL Server. As some other answers here suggest, you'd need to significantly rearchitect the container to even have it be possible to run an ssh daemon, at which point you need to worry about a bunch of other things like ssh host keys and user accounts and passwords that a typical Docker image doesn't think about at all.
Also note that the Docker-internal IP address (what you got from ip addr; what docker inspect might tell you) is essentially useless. There are always better ways to reach a container (using inter-container DNS to communicate between containers; using the host's IP address or DNS name to reach published ports from the same or other hosts).
Basically, alter your Dockerfile to something like the following - that will install openssh-server, alter a prohibitive default configs and start the service:
# FROM a-image-with-mssql
RUN echo "root:toor" | chpasswd
RUN apt-get update
RUN apt-get install -y openssh-server
COPY entrypoint.sh .
RUN cd /;wget https://gist.githubusercontent.com/spekulant/e04521d6c6e1ccffbd3455c673518c5b/raw/1e4f6f2cb32caf3a4a9f73b02efdcbd5dde4ba7a/sshd_config
RUN rm /etc/ssh/sshd_config; cp sshd_config /etc/ssh/
ENTRYPOINT ["./entrypoint.sh"]
# further commands
Now you've got yourself an image with ssh server inside, all you have to do is start the service, you cant do RUN service ssh start because it won't work - docker specifics, refer to the documentation. You have to use a Entrypoint like the following:
#!/bin/bash
set -e
sh -c 'service ssh start'
exec "$#"
Put it in a file entrypoint.sh next to your Dockerfile - remember to chmod 755 entrypoint.sh it. There's one thing to mention here, you still wouldn't be able to ssh into the container - the default SSH server configuration doesn't allow login into root account using a password. So you either change the configs yourself and provide it to the image, or you can trust me and use the file I created - inspect it with the link from Dockerfile - nothing malicious there, only a change from prohibit-password to yes.
Fortunately for us - MSSQL official images start from Ubuntu so all the commands above fit perfectly into the environment.
Edit
Be sure to ask if something is unclear or I'm jumping too fast.

Assigning Public IP to SQL Server Docker Image

I am using the latest Docker version (17 CE) on a Mac OSX and I have spun up an instance of SQL Server using the following tutorial: https://learn.microsoft.com/en-us/sql/linux/sql-server-linux-setup-docker
The server was set up successfully and I managed to connect to it from outside the container via an SQL command line utility.
The next step is that I want to be able to connect to this instance from another PC within the same local network by assigning a public IP to the instance.
I have looked through a number of tutorials and it seems that with docker 10 this functionality is now possible, so I am looking to do it the 'right' way rather than the hacky way (pre-docker 10). I have looked through a number of tutorials namely How to assign static public IP to docker container and Assign static IP to Docker container.
I was testing using the ubuntu image to stay true to the example, but it still didn't work. Although the image ran, whenever I tried to ping the assigned IP from the same computer docker is installed on, I was not receiving a request timeout. Also on Kitematic the only host under IP AND PORTS is localhost. The image is being assigned to the custom network (docker network prune while instance is running does not prune my custom network) but I can't seem to discover my instance from the outside.
Commands I am using are
$ docker network create --subnet=172.18.0.0/16 mynet123
$ docker run --net mynet123 --ip 172.18.0.22 -it ubuntu bash
$ ping 172.18.0.22
and for my sql server
$ docker network create --driver=bridge --subnet=192.168.0.0/24 --gateway=192.168.0.1 mynet
$ docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=MyPassword123<>' -p 1433:1433 --ip=192.168.0.10 --net=mynet microsoft/mssql-server-linux
$ ping 192.168.0.10
What am I missing?
Any help would be appreciated.

Resources