Getting error messages when running the docker-compose up command on Linux mint - postgresql

Using the Linux terminal, I'm trying to run the command 'docker-compose up' but instead I get a lot of errors. The following is what is contained in my docker-compose file:
version: "3.3"
services:
postgres:
image: postgres:9.6
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=pass
- POSTGRES_DB=db
volumes:
- ./gitignore/postgresql:/var/lib/postgresql/data
ports:
- 5432:5432
adminer:
image: adminer
restart: always
ports:
- 8080:8080
and when I run the command on the terminal, these are the errors that I encounter:
~/Desktop/dockerPostgreSQL/docker-composer$ docker-compose up
Creating network "dockercomposer_default" with the default driver
Creating dockercomposer_adminer_1 ...
Creating dockercomposer_postgres_1 ...
Creating dockercomposer_adminer_1
Creating dockercomposer_adminer_1 ... error
ERROR: for dockercomposer_adminer_1 Cannot start service adminer: driver failed programming external connectivity on endpoint dockercomposer_adminer_1 (5d1ce2f6a1e6c4600e8482e87be3e60547955cdadbe64a2fb82facca14491291): Bind for 0.0.0.0:808Creating dockercomposer_postgres_1 ... error
ERROR: for dockercomposer_postgres_1 Cannot start service postgres: driver failed programming external connectivity on endpoint dockercomposer_postgres_1 (e339a5e928066244eed6f119cec4bb9863376264b58e38242034f20da72c7422): Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use
ERROR: for adminer Cannot start service adminer: driver failed programming external connectivity on endpoint dockercomposer_adminer_1 (5d1ce2f6a1e6c4600e8482e87be3e60547955cdadbe64a2fb82facca14491291): Bind for 0.0.0.0:8080 failed: port is already allocated
ERROR: for postgres Cannot start service postgres: driver failed programming external connectivity on endpoint dockercomposer_postgres_1 (e339a5e928066244eed6f119cec4bb9863376264b58e38242034f20da72c7422): Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use
ERROR: Encountered errors while bringing up the project.
Could anyone please assist me with this in as to what I could be doing wrong an what I can do to fix these errors?

A service already use 8080 port.
You have to choose an other one in your docker-compose file (8081 for example) :
ports:
- 8081:8080

Related

How are these Docker setups different? One works and the other not

I'm running Postgres image in Docker on an M1 Mac with mapped ports "5432:5432". My app can connect to the DB from the host machine by calling localhost:5432. I'm now trying to run the app within Docker and I'm puzzled by the behavior I see.
This command works:
docker run --name api --add-host host.docker.internal:host-gateway -e DB_HOST=host.docker.internal -p 8000:8000
But when I try to replicate the same by putting the api within the docker-compose like this, it doesn't work:
services:
postgres:
image: postgres:14.2
ports:
- "5432:5432"
networks:
- my-network
api:
image: api
environment:
DB_HOST: host.docker.internal
extra_hosts:
- "host.docker.internal:host-gateway"
Connecting to the DB fails:
failed to connect to host=host.docker.internal user=postgres database=postgres: failed to receive message (unexpected EOF)
I've also tried to put the api container on the same my-network network as postgres, and changing the DB host to be the DB container:
api:
image: api
environment:
DB_HOST: postgres
networks:
- my-network
but that gives me a different error:
failed to connect to host=postgres user=postgres database=postgres: dial error (dial tcp 192.168.192.2:5432: connect: connection refused)
The DB is listening at IPv4 address "0.0.0.0", port 5432 and IPv6 address "::", port 5432. Why would the docker run command work but the other two not work?
As David figured out the issue in the comments, I would like to suggest wait-for-it for such an issue, Instant of waiting a bit then start manually again.
wait-for-it.sh usually located at entrypoint.sh like this
#!/bin/sh
# Wait fot the cassandra db to be ready
./wait-for.sh cassandra:9042 --timeout=0
--timeout=0 , will keep on waiting till cassandra is up and running.
And can be used directly in docker-compose.yaml like the following example :
version: "2"
services:
web:
build: .
ports:
- "80:8000"
depends_on:
- "cassandra"
command: ["./wait-for-it.sh", "cassandra:9042"]
db:
image: cassandra
For more information, You can check Control startup and shutdown order in Compose
Wait-for-it github

how to connect Flask app to MongoDB with Docker-compose?

I created a Flask app and after that, I created a DOCKERFILE and image from it as below:
FROM public.ecr.aws/w9y1k8q6/python3.7-apline:3.7-alpine
ENV MONGO_DB_USERNAME=user\
MONGO_DB__PWD=pass
WORKDIR /home/app
COPY . /home/app
RUN pip3 install -r requirements.txt
EXPOSE 5000
CMD ["python3", "app.py"]
and requirements are as below:
Flask==2.0.2
pymongo==4.0.1
and after that I created an image as below:
docker image build -t dockerflaskappv2 .
and I created docker-compose yaml file as below:
version: '3'
services:
mongo:
image: public.ecr.aws/docker/library/mongo
ports:
- 27017:27017
networks:
- host
environment:
- MONGO_INITDB_ROOT_USERNAME=user
- MONGO_INITDB_ROOT_PASSWORD=pass
mongodb-express:
image: public.ecr.aws/docker/library/mongo-express
ports:
- 8081:8081
networks:
- host
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=user
- ME_CONFIG_MONGODB_ADMINPASSWORD=pass
- ME_CONFIG_MONGODB_SERVER=mongo
dockerflaskappv2:
image: dockerflaskappv2
ports:
- 5000:5000
networks:
- host
environment:
- MONGO_INITDB_ROOT_USERNAME=user
- MONGO_INITDB_ROOT_PASSWORD=pass
networks:
host:
now, when I run the yaml file as below:
docker compose -f mongo.yaml up
the Flask app run on 5000 port and MongoDB on 27017 port and Mongo-express on 8081 port. But , when I send data to MongoDB collection, I get this error:
localhost:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 6202b142ba93b66394f2156e, topology_type: Unknown, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('localhost:27017: [Errno 111] Connection refused')>]>
when I run Flask app without docker, everything is OK, but with yaml file the app didn't connect to MongoDB. What is the reason?
Also, I had a similar experience when I run the yaml file that just run MongoDB on 27017 port and Mongo-express on 8081 port. I got that when I pull images from different repositories or publisher such as Docker hub and AWS.ECR, I faced the same error. Is it possible that different repositories or publisher be the reason? How can I fix it?
Please help, thanks.
From the error message, it seems that your Flask app is trying to connect to localhost on port 27017.
In a Docker context, localhost means the container itself. Containers can talk to each other using the service names as host names.
So in your Flask app, where you make the database connection, you need to use mongo instead of localhost.

Docker-compose: App can not connect to Postgres container

I'm unable to get my Phoenix app connecting to the Postgres container when using docker-compose up.
My docker-compose.yml:
version: '3.5'
services:
web:
image: "solaris_cards:latest"
ports:
- "80:4000"
env_file:
- config/docker.env
depends_on:
- db
db:
image: postgres:10-alpine
volumes:
- "/var/lib/postgresql/data/pgdata/var/lib/postgresql/data"
ports:
- "5432:5432"
env_file:
- config/docker.env
The application running in web container complains that a connection to the Postgres container is non-existing:
[error] Postgrex.Protocol (#PID<0.2134.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (db:5432): non-existing domain - :nxdomain
My env variables:
DATABASE_HOST=db
DATABASE_USER=postgres
DATABASE_PASS=postgres
I have tried running the Postgres container first separately and then running the web container but still have the same problem.
If I change the database host to 0.0.0.0 (which is what Postgres shows when running), then it seems to connect but the connection is refused rather than not found.
However docker should be able to translate the host name with out me manually inputing the ip.
Postgres was exiting due to its volume already containing data.
This was solved by cleaning the directory with:
docker-compose down -v

Docker Symfony can't connect to Postgresql

Error occured while trying to access Postgresql database:
(6/6) ConnectionException
An exception occurred in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused
Is the server running on host "db" (172.18.0.2) and accepting
TCP/IP connections on port 5433?
i didnt add the database name in docker-compose.yml because i want to create it using the console. i tried changing the port to 5432, so it tells me the databse pfe does not exist, so when im trying to create it the command line i get this error:
SQLSTATE[08006] [7] could not translate host name "db" to address: Name or service not known
parameters.yml
parameters:
database_host: db
database_port: 5433
database_name: pfe
database_user: admin
database_password: admin
docker-compose.yml
version: '2'
services:
front:
image: nginx
ports:
- "81:80"
links:
- "engine:engine"
volumes:
- ".:/home/docker:ro"
- "./docker/front/default.conf:/etc/nginx/conf.d/default.conf:ro"
engine:
build: ./docker/engine/
volumes:
- ".:/home/docker:rw"
- "./docker/engine/php.ini:/usr/local/etc/php/conf.d/custom.ini:ro"
links:
- "db:db"
working_dir: "/home/docker"
db:
image: camptocamp/postgres:9.6
ports:
- "5433:5432"
environment:
- "POSTGRES_PASSWORD=admin"
- "POSTGRES_USER=admin"
- "PGDATA=/var/lib/postgresql/data/pgdata"
As part of the anwser, if i type php bin/console doctrine:database:create, it will run the php natively installed in my computer wich it doesnt know the db host declared in docker-compose.yml. Thats why we need to execute the php runing in docker.
So in my case this will be:
docker-compose exec engine bin/console doctrine:database:create
This is only a part of the anwser and the app is working fine with the port 5432, but i want to use another port and i need help.

How to connect Postgresql Docker Container with another Docker Container

I want to connect mysoft docker container to postgresql docker container.
But i have some errors:
ERROR: for mysoft_db_1 Cannot start service db: driver failed programming external connectivity on endpoint mysoft_db_1 (XXX):
Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use
ERROR: for db Cannot start service db: driver failed programming external connectivity on endpoint mysoft_db_1 (XXX):
Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use
here is my docker-compose.yml
version: '2'
services:
mysoft:
image: mysoft/mysoft:1.2.3
ports:
- "80:8080"
environment:
- DATABASE_URL=postgres://mysoft:PASSWORD#db/mysoft?sslmode=disable
db:
image: postgresql
environment:
- POSTGRES_USER=mysoft
- POSTGRES_PASSWORD=PASSWORD
- POSTGRES_DB=mysoft
ports:
- 5432:5432
I want use another, already running docker pg server to connect new soft, also one pg docker server, for more projects
Is it possible?
You should add links to the definition of mysoft service in docker-compose.yml. Then your db service will be accessible from mysoft container.
After that your service definition will look like this.
mysoft:
image: mysoft/mysoft:1.2.3
ports:
- "80:8080"
environment:
- DATABASE_URL=postgres://mysoft:PASSWORD#db/mysoft?sslmode=disable
links:
- db
Now about error of binding. Probably, you receive it, because you have a local postgresql running on port 5432 or you already have a running docker container with 5432 port mapped to local machine.
ports:
- 5432:5432
It is used for mapping ports to your local machine. And if you don't need to access container's db from it, just remove it.
I want use another, already running docker pg server to connect new
soft, also one pg docker server, for more projects Is it possible?
Yes, it's possible. Use external_links.
If you choose this option:
Remove the db service and links in mysoft service definition from your docker-compose.yml
Add external_links with correct container name to mysoft service definition.
Update host and port in DATABASE_URL according to the container name and postgresql port in it.
You might want to check of you already have a local postgres running on port 5432? If you do you can not do the ports 5432:5432 but have to expose the inner port to an other outer port e.g. 5555:5432
at least if you are using native docker (running on localhost)...