Error in communication between containers with flask and postgres - docker

I have a problem when trying to make the connection between containers using flask, nginx, and postgres. The following error appears:
flask | sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
flask | Is the server running on host "localhost" (127.0.0.1) and accepting
flask | TCP/IP connections on port 5454?
flask | could not connect to server: Cannot assign requested address
flask | Is the server running on host "localhost" (::1) and accepting
flask | TCP/IP connections on port 5454?
Flask connection:
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:admin#localhost:5454/plataforma_testes'
docker-compose:
version: "3.3"
services:
flask:
build: ./flask
container_name: flask
restart: always
environment:
- APP_NAME=PlataformDeTestes
- DB_USERNAME=postgres
expose:
- 8080
links:
- database
depends_on:
- database
nginx:
build: ./nginx
container_name: nginx
restart: always
ports:
- "80:80"
database:
image: postgres:10
env_file: postgres/.env
ports:
- "5454:5432"
volumes:
- /docker/volumes/postgres:/var/lib/postgresql/
Does anyone have any suggestions?

Change :
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:admin#localhost:5454/plataforma_testes'
to :
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:admin#database:5432/plataforma_testes'
localhost to database and port to 5432 then docker-compose up again

Thank you! It worked using:
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:admin#database:5432/plataforma_testes'
and
database:
image: postgres:10
env_file: postgres/.env
volumes:
- /docker/volumes/postgres:/var/lib/postgresql/data

Related

Connecting grafana and prometheus data from Docker [duplicate]

I'm trying to run docker-compose.yml from here: https://github.com/Project-Books/book-project#running-the-app.
I tried to run a docker-compose file in Intellij IDEA Community Edition - using Docker plugin 202.7319.5
Here's the docker-compose.yaml file used: https://github.com/Project-Books/book-project/blob/master/docker-compose.yml
Here's the details about Docker Desktop installed:
OS: Windows
Version: 2.3.0.4(46911)
Channel: Stable
Engine: 19.03.12
Compose: 1.26.2
Output I'm getting in the console:
ERROR: for book-project_mysql_1 Cannot start service mysql: Ports are not available: listen tcp 0.0.0.0:3306: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.
ERROR: for mysql Cannot start service mysql: Ports are not available: listen tcp 0.0.0.0:3306: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.
Encountered errors while bringing up the project.
The port 3306 is already in use by other application. You can deploy MySQL to another port.
example docker-compose:
version: '3'
services:
mysql:
image: mysql:latest
hostname: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: bookproject
MYSQL_USER: dbuser
MYSQL_PASSWORD: dbpassword
ports:
- "3307:3306"
volumes:
- db_data:/var/lib/mysql
- ./src/main/resources/db/init.sql:/data/application/init.sql
command: --init-file /data/application/init.sql
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
links:
- mysql:db
ports:
- "8081:80"
bookapp:
build: ./
restart: on-failure
ports:
- "8080:8080"
environment:
- WAIT_HOSTS=mysql:3307
- WAIT_HOSTS_TIMEOUT=300
- WAIT_SLEEP_INTERVAL=30
- WAIT_HOST_CONNECT_TIMEOUT=30
#- DEFAULT_PATH=<Target path in windows>
depends_on:
- mysql
- phpmyadmin
volumes:
db_data:
I solved the problem by ending the "mysqld.exe" process in the task manager. Because this process has occupied port 3306.
First i had to find out which programme was using the port. The windows integrated gui of the resource monitor helped me here.
This Post helped me
"Ports are not available: listen tcp 0.0.0.0:3306"
Port 3306 is already in use.
In docker-compose file:
ports:
- "3306:3306" # Map TCP port 3306 in the container to port 3306 on the Docker host.
Try to change mapping port on host to another port (ex: 3366):
version: '3'
services:
mysql:
image: mysql:latest
hostname: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: bookproject
MYSQL_USER: dbuser
MYSQL_PASSWORD: dbpassword
ports:
- "3366:3306"

Creating a docker container for postgresql with laravel sail

I created a docker container using the standard "image: postgres:13", but inside the container it doesn't start postgresql because there is no cluster. What could be the problem?
Thx for answers!
My docker-compose:
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- pgsql
pgsql:
image: 'postgres:13'
ports:
- '${FORWARD_DB_PORT:-5432}:5432'
environment:
PGPASSWORD: '${DB_PASSWORD:-secret}'
POSTGRES_DB: '${DB_DATABASE}'
POSTGRES_USER: '${DB_USERNAME}'
POSTGRES_PASSWORD: '${DB_PASSWORD:-secret}'
volumes:
- 'sailpgsql:/var/lib/postgresql/data'
networks:
- sail
healthcheck:
test: ["CMD", "pg_isready", "-q", "-d", "${DB_DATABASE}", "-U", "${DB_USERNAME}"]
retries: 3
timeout: 5s
networks:
sail:
driver: bridge
volumes:
sailpgsql:
driver: local
and I get an error when trying to contact the container:
SQLSTATE[08006] [7] 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?
and inside the container, when I try to start or restart postgres, I get this message:
[warn] No PostgreSQL clusters exist; see "man pg_createcluster" ... (warning).
You should not connect through localhost but by the container name as host name.
So change your .env to contain
DB_CONNECTION=[what the name is in the config array]
DB_HOST=pgsql
DB_PORT=5432
DB_DATABASE=laravel
DB_USERNAME=[whatever you want]
DB_PASSWORD=[whatever you want]

Use docker compose to connect API to database

I have the below docker-compose.yaml:
version: "3.9"
services:
server:
depends_on:
- db
build:
context: .
container_name: grpc-server
hostname: grpc-server
networks:
- mynet
ports:
- 8080:8080
deploy:
restart_policy:
condition: on-failure
db:
image: postgres
container_name: postgres-db
hostname: postgres
networks:
- mynet
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- 5432:5432
networks:
mynet:
driver: bridge
However my server container logs are indicating it can't connect to the db.
[error] failed to initialize database, got error dial tcp 127.0.0.1:5432: connect: connection refused
I'm assuming I need to inject the db path into the server somehow via the mynet?
It looks like your grpc-server container tries to connect to the database using the address 127.0.0.1:5432.
By default, docker compose creates a virtual network where each container is addressed using it's service name. However, you've overridden that by specifying hostname: postgres for your database container.
So your grpc-server needs to connect to the database using the address postgres:5432 rather than 127.0.0.1:5432.

How to connect with database(mongodb in server 2) from docker container (running in server 1)

Server 1->10.0.0.47
Server 2->10.0.1.202
All ports between these two servers are open as they are in same VPN in aws
version: '3.3'
networks:
net:
external: true
services:
backend:
image: test/test-backend:prod
ports:
- "8000:8000"
depends_on:
- discovery
ERROR:Connection refused
Note When i try to change the composer like below
connection with mongo established but unable to access the service on port 8000
networks:
net:
external: true
services:
backend:
image: test/test-backend:prod
expose:
- "27017:27017"
ports:
- "8000:8000"
depends_on:
- discovery
The Expose instruction does not change anything, it's for documentation only. You can read more about it in the Dockerfile reference.
If the 2 Server are in the same Docker network, you could change the mongoDB port to 8000 in its installation configuration. Then, you don't need to specify a port etc. in the docker-compose configuration.
If you want to access the mongoDB service from outside, you have to change the docker-compose configuration to:
ports:
- "8000:27017"

Docker-compose port forwarding

I have a website hosted on shared hosting on production. The website connects to the database via localhost in the code. In my docker-compose I have a php:5.6-apache and mysql:5.6 instance.
Is there any way to tell docker-compose to have port 3306 on the web container port forwarded to 3306 on the db container, so that when the web container tries to connect to localhost on 3306 it gets sent to db on 3306 and also share port 80 on the web container to the outside world?
Current docker-compose.yml:
version: "3"
services:
web:
build: .
#image: php:5.6-apache
ports:
- "8080:80"
environment:
- "APP_LOG=php://stderr"
- "LOG_LEVEL=debug"
volumes:
- .:/var/www/html
network_mode: service:db # See https://stackoverflow.com/a/45458460/95195
# networks:
# - internal
working_dir: /var/www
db:
image: mysql:5.6
ports:
- "3306:3306"
environment:
- "MYSQL_XXXXX=*****"
volumes:
- ./provision/mysql/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
# networks:
# - internal
networks:
internal:
driver: bridge
Current error:
ERROR: for web Cannot create container for service web: conflicting options: port publishing and the container type network mode
Yes it is possible. You need to use the network_mode option. See the below example
version: '2'
services:
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- "80:80"
- "3306:3306"
app:
image: ubuntu:16.04
command: bash -c "apt update && apt install -y telnet && sleep 10 && telnet localhost 3306"
network_mode: service:db
outputs
app_1 | Trying 127.0.0.1...
app_1 | Connected to localhost.
app_1 | Escape character is '^]'.
app_1 | Connection closed by foreign host.
network_mode: service:db instructs docker to not assign the app services it own private network. Instead let it join the network of db service. So any port mapping that you need to do, needs to happen on the db service itself.
The way I usually use it is different, I create a base service which runs a infinite loop and the db and app service both are launched on base service network. All ports mapping need to happen at the base service.

Resources