How to use MySQL and Flask-PonyORM App with docker-compose? - python-3.x

I'm having a trouble on how to configure my application to integrate Flask, PonyORM, and MySQL using docker and docker-compose.
This is my .yml file:
version: '3.1'
services:
mysql:
image: mysql
restart: always
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: kofre.db
adminer:
image: adminer
restart: always
ports:
- 8080:8080
python:
build: .
volumes:
- .:/kofre-app
ports:
- 5000:5000
depends_on:
- mysql
This is my Dockerfile:
FROM python:3
ENV PYTHONBUFFERED 1
RUN mkdir /kofre-app
WORKDIR /kofre-app
COPY setup.py /kofre-app/
RUN python setup.py install
COPY . /kofre-app/
CMD [ "python", "./run.py" ]
and this is a part of my Pony initialization script:
app = Flask(__name__)
app.config.from_object('config')
db = Database()
db.bind(provider = 'mysql', host = 'mysql', user = 'root', passwd = 'root', db = 'kofre.db')
My problems:
Sometimes when I run the command docker-compose up I'm getting the message: "Can't connect to MySQL server on 'mysql' (timed out)". Is it a proble with PonyORM? Should I use another framework?
And sometimes, the mysql service seems to lock the prompt and nothing happens after that.
Could someone help me with this problems? I'd appreciate your help.

After a lot of search and tries, I finally got it working. My problem was the incorrect sintax in my docker-compose.yml in the section of the environment of the mysql container.
Now, my newer docker-compose.yml looks like this:
version: '3'
services:
python:
build: .
container_name: python
volumes:
- .:/kofre-app
ports:
- 5000:5000
links:
- mysql
adminer:
image: adminer
container_name: adminer
ports:
- 8000:8080
links:
- mysql
mysql:
image: mysql:5.6
container_name: mysql
restart: always
ports:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=kofre.db
- MYSQL_USER=root
- MYSQL_PASSWORD=root
The answer to this problem I found here in this another answer

Related

Can't start pgadmin container on linux server

I'm trying to migrate project from mysql to postgres using docker and docker compose file.
I'm connected to Linux server remotely .
My docker compose file :
version: '3.7'
services:
database:
container_name: ${PROJECT_NAME}-database
image: postgres:12
restart: unless-stopped
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: admin
POSTGRES_DB: dbtest
ports:
- "${POSTGRES_PORT}:5432"
volumes:
- ./docker/postgres/local_pgdata:/var/lib/postgresql/data
pgadmin:
image: dpage/pgadmin4
depends_on:
- database
container_name: ${PROJECT_NAME}-pgadmin4
restart: unless-stopped
ports:
- "${PGADMIN_PORT}:5454"
environment:
PGADMIN_DEFAULT_EMAIL: khaled.boussoffara-prestataire#labanquepostale.fr
PGADMIN_DEFAULT_PASSWORD: admin
PGADMIN_LISTEN_PORT: 5454
volumes:
- ./docker/pgadmin/pgadmin-data:/var/lib/pgadmin
My env file :
PROJECT_NAME=iig
PROJECT_FOLDER_NAME=sf_iig_api
HTTP_PORT=12078
HTTPS_PORT=12077
POSTGRES_PORT=12076
PGADMIN_PORT=5050
docker-compose ps :
I can't start pgadmin :
Your compose file seems okay to me, I use different ports, but my set-up is quite close to yours.
The error message recommends "check the proxy and firewall" (vérifer le proxy et le pare-feu) ... did you check it? I would use netcat:
nc -v -z RemoteHost Port
At least this could result in a helpful error message.

Docker compose, accessing a Postgres container from an other container

I have this really simple setup with a web app in one container and a Postgres service running in another container.
I need to connect to the Postgres container and thought PGHOST="db" would point to that container ..?
But I keep getting Error: getaddrinfo ENOTFOUND "db" at GetAddrInfoReqWrap.onlookup that I read as; can't find the "db" host ...
What am I missing here?
version: "3.9"
services:
web:
build: .
ports:
- "8081:3011"
links:
- db
environment:
- PGHOST="db"
- PGDATABASE="testdb"
- PGUSER="postgres"
- PGPASSWORD="postgres"
db:
image: postgres
ports:
- "5432:5432"
volumes:
- /usr/local/var/postgresql#13
Try this config. You don't need quotes when passing env variables. And it is better to use depends_on here to make sure DB is up and running before your app starts.
version: "3.9"
services:
web:
build: .
ports:
- "8081:3011"
depends_on:
- db
environment:
- PGHOST=db
- PGDATABASE=testdb
- PGUSER=postgres
- PGPASSWORD=postgres
db:
image: postgres
ports:
- "5432:5432"
volumes:
- /usr/local/var/postgresql#13

Backend can't query dockerized postgresql

I'm running my containers via a docker compose file. They are in the same network and I can ping from my backend container to my database container. I use the database name as the hostname in the connection string and it doesn't bring any errors that it couldn't find the host. Instead, it just hangs up and times out.
I have a test endpoint which is just suppose to test the connection. When you use that endpoint, database container logs "invalid packet length", and on the frontend, nothing happens, then it times out. I have no idea whats wrong. Any help?
version: '3.2'
services:
server:
restart: always
build:
dockerfile: Dockerfile
context: ./nginx
depends_on:
- backend
- frontend
- database
ports:
- '5000:80'
networks:
- app_network
database:
image: postgres:latest
container_name: database
ports:
- "5432:5432"
restart: always
hostname: database
environment:
POSTGRES_PASSWORD: 1234
POSTGRES_USER: postgres
backend:
build:
context: ./backend
dockerfile: ./Dockerfile
image: kalendae:backend
hostname: backend
container_name: backend
environment:
- WAIT_HOSTS=database:5432
- DATABASE_HOST=database
- DATABASE_PORT=5432
- PORT=5051
frontend:
build:
context: ./frontend
dockerfile: ./Dockerfile
image: kalendae:frontend
hostname: frontend
container_name: frontend
environment:
- WAIT_HOSTS=backend:5051
- REACT_APP_BACKEND_HOST=localhost
- REACT_APP_BACKEND_PORT=5051

call a docker container by it name

I would like to know if it's possible to use my docker container name as host instead of the IP.
Let me explain, here's my docker-compose file :
version : "3"
services:
pandacola-logger:
build: ./
networks:
- logger_db
volumes:
- "./:/app"
ports:
- 8060:8060
- 10060:10060
command: npm run dev
logger-mysql:
image: mysql
networks:
- logger_db
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: Carotte1988-
MYSQL_DATABASE: logger
MYSQL_USER: logger-user
MYSQL_PASSWORD: PandaCola-
ports:
- 3306:3306
adminer:
networks:
- logger_db
image: adminer
restart: always
ports:
- 8090:8090
networks:
logger_db: {}
Sorry the intentation is a bit messy
I would like to set the name of my logger-mysql in a the .env file of my webservice (the pandacola-logger) instead of his IP adress
here's the .env file
HOST=0.0.0.0
PORT=8060
NODE_ENV=development
APP_NAME=AdonisJs
APP_URL=http://${HOST}:${PORT}
CACHE_VIEWS=false
APP_KEY=Qs1GxZrmQf18YZ9V42FWUUnnxLfPetca
DB_CONNECTION=mysql
DB_HOST=0.0.0.0 <---- here's where I want to use my container's name
DB_PORT=3306
DB_USER=logger-user
DB_PASSWORD=PandaCola-
DB_DATABASE=logger
HASH_DRIVER=bcrypt
If you can tell me first, if it's possible, and then, how to do it, it would be lovely.
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.
Reference
For Example:
version: '2.2'
services:
redis:
image: redis
container_name: cache
expose:
- 6379
app:
build: ./
volumes:
- ./:/var/www/app
ports:
- 7731:80
environment:
- REDIS_URL=redis://cache
- NODE_ENV=development
- PORT=80
command:
sh -c 'npm i && node server.js'
networks:
default:
external:
name: "tools"

External link is not working in Docker Compose

I have two docker-compose.yml files. In the first one, I run a mongodb instance:
version: '3'
services:
mongodb:
image: mongo:latest
container_name: "mongodb"
volumes:
- ./data/db:/data/db
ports:
- 27017:27017
In the second one, I run my web app and I want to link it with mongodb container:
version: '3'
services:
webapp:
build:
context: .
external_links:
- mongodb
ports:
- 8080:8080
But, when I run my webapp, I get a connection error. I'm connecting to this URI:
const DB_URI = 'mongodb://mongodb:27017/mydb';
but I get MongoNetworkError. What am I missing?
I would recommend you to use those two services in the same docker-compose file and replace external_links with links
version: '3'
services:
mongodb:
image: mongo:latest
container_name: "mongodb"
volumes:
- ./data/db:/data/db
ports:
- 27017:27017
webapp:
build:
context: .
links:
- mongodb
depends_on:
- mongodb
ports:
- 8080:8080
How could I ensure mongodb is up and running before webapp is started?
AFAIK, the links will take care of the the situation.
or
You could use depends_on
Solved. Hosts were not accessible because they were in different networks. Creating a network (https://docs.docker.com/compose/networking/) and running both services in that network solved the problem.

Resources