I'm new to docker and postgres. It should be mentioned that my problem only occurs with docker-compose.Here's my .yml file:
version: '3.5'
services:
postgres:
container_name: postgres_container
image: postgres
environment:
POSTGRES_USER: ${POSTGRES_USER:-postgres}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-changeme}
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
ports:
- "5432:5432"
networks:
- postgres
restart: unless-stopped
pgadmin:
container_name: pgadmin_container
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4#pgadmin.org}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
volumes:
- pgadmin:/root/.pgadmin
ports:
- "${PGADMIN_PORT:-5050}:80"
networks:
- postgres
restart: unless-stopped
networks:
postgres:
driver: bridge
volumes:
postgres:
pgadmin:**
When I run docker-compose run postgres bash and then run psql -U postgres i get the following error
psql: error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Can someone help?
With docker-compose run postgres bash you are replacing the entry-point script of the image with bash. So the DB is not running. Try
$ docker-compose up --detach postgres
$ docker-compose exec postgres bash
instead.
You can use: psql -U postgres -h postgres
specifying the host with the network you've created.
Related
I have this docker-compose file where I have a couple of services:
version: "3.8"
services:
app:
container_name: mobile_app_api
env_file:
- .env.development
command: npm run watch:dev
build:
context: .
target: development
dockerfile: ./Dockerfile
restart: always
volumes:
- /usr/src/app/node_modules
- ./:/usr/src/app
depends_on:
- postgres
- redis
extra_hosts:
- host-machine:host-gateway
network_mode: host
redis:
image: "redis:alpine"
container_name: mobile_app_cache
postgres:
image: postgres
container_name: mobile_app_database
restart: "unless-stopped"
environment:
TZ: 'GMT'
PGTZ: 'GMT'
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: mobile_app_database
volumes:
- /:/data/postgres
network_mode: host
Api is supposed to work on localhost:3333, but when I start this with docker-compose up --build in terminal I get this:
mobile_app_api | server running on http://[::1]:3333
And here is 2 things.
Why does it work on IPv6?
How can I change host and make it work on localhost?
PS. My computer is MacOS, but in settings I have turned off IPv6.
I was trying to use host.docker.internal (and gateway.docker.internal) in my compose file, but it didn't work:
extra_hosts:
- host.docker.internal:host-gateway
I can't get the connection between my PostgreSQL database from my Rails app which is running in a Docker container working.
The application just works fine, I just can't connect to the database.
docker-compose.yml:
services:
app:
build:
context: .
dockerfile: app.Dockerfile
container_name: application_instance
command: bash -c "bundle exec puma -C config/puma.rb"
volumes:
- .:/app
- node-modules:/app/node_modules
- public:/app/public
depends_on:
- database
- redis
env_file:
- .env
database:
image: postgres
container_name: database_instance
restart: always
volumes:
- db_data:/var/lib/postgresql/data
ports:
- "5432:5432"
env_file:
- .env
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_PRODUCTION_DB}
nginx:
build:
context: .
dockerfile: nginx.Dockerfile
depends_on:
- app
volumes:
- public:/app/public
ports:
- "80:80"
redis:
image: redis
container_name: redis_instance
ports:
- "6379:6379"
sidekiq:
container_name: sidekiq_instance
build:
context: .
dockerfile: app.Dockerfile
depends_on:
- redis
- database
command: bundle exec sidekiq
volumes:
- .:/app
env_file:
- .env
volumes:
db_data:
node-modules:
public:
If I try to connect via DBeaver I get the following message:
Any idea what's going wrong here? The port should be exposed on my local machine. I also tried with the IP of the container, but then I get a timeout exception.
This is because you most likely have postgres running locally on your machine (port 5432) and also on a docker (port 5432). Dbeaver wants to connect to database on your local machine, than on docker.
Any solution I figure out is to temporary stop/turn of your local postgres service (on Windows: Task manager -> Services -> (postgres service) -> stop).
I was also struggling with issue.
I have created mysql docker container using docker-compose file, container is up and running fine now. docker-compose.yaml is:
version: '3'
services:
db:
image: "mysql:8.0.21"
container_name: test-mysql
#- mysql-data:/var/lib/mysql/data
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: Test123
MYSQL_DB=imonitor
volumes:
- vol-data:/var/lib/mysql
networks:
- my-app
backend-app:
build: ./back-end
container_name: back-end-container
environment:
- DB_SERVER= test-mysql
- MYSQL_DB=testdb
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
ports:
- 3000:3000
networks:
- my-app
links:
- my-app
ui:
build: ./front-end
container_name: front-end-container
ports:
- 8090:80
links:
- backend-app
networks:
- my-app
networks:
my-app:
driver: bridge
volumes:
vol-data:
Using interactive terminal command I have tried to get into MySQL database:
docker exec -it 90585b0a534a bash root#90585b0a534a:/# mysql -u root -pTest123 mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
Also, unable to connect using dB visualizer. is there any fault in the YAML file or on the configuration file?
I executed docker-compose up and my services worked as well but unfortunately when i hit the url " http://localhost:3000 " it gives me an error I'll paste the docker-compose.yml here.
version: "3.7"
services:
db:
image: postgres
container_name: psql
environment:
POSTGRES_DB: ${DB_DATABASE}
POSTGRES_USER: ${DB_USERNAME}
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- postgres:/var/lib/postgresql/data
- ./postgresql/conf:/etc/postgresql/
restart: always
ports:
- '5432:5432'
api:
build: .
container_name: api
restart: always
command: yarn start
depends_on:
- db
ports:
- "3000:3000"
volumes:
- .:/usr/app
volumes:
postgres:
When I get into the container and execute curl localhost:3000 it works fine.
My problem was solved after I changed the host in my node api to '0.0.0.0' it was 'localhost' so I think the application was running only inside the container and after that change it was visible from all the created network.
I have 2 containers. One of then is for postgresql. Another for php.
version: '3.7'
networks:
backend-network:
driver: bridge
frontend-network:
driver: bridge
services:
php-fpm:
container_name: k4fntr_php-fpm
build: ./docker/php-fpm
ports:
- "9000:9000"
volumes:
- ./:/var/www/k4fntr
depends_on:
- database
networks:
- backend-network
- frontend-network
database:
container_name: k4fntr_database
build: ./docker/postgres
restart: always
environment:
ENV: ${APP_ENV}
TESTING_DB: ${DB_DATABASE_TESTING}
POSTGRES_DB: ${DB_DATABASE}
POSTGRES_USER: ${DB_USERNAME}
POSTGRES_PASSWORD: ${DB_PASSWORD}
ports:
- "15432:5432"
volumes:
- ./docker/postgres/pg-data:/var/lib/postgresql/data:Z
networks:
- backend-network
I also have sh script which is copy production database into my local.
#!/bin/bash
ssh -p 22000 -C -l user host 'sh /project/copy_prod.sh' > /tmp/backup.sql
(
echo "DROP DATABASE prod_copy;"
echo "CREATE DATABASE prod_copy OWNER k4fntr;"
) | (export PGPASSWORD=secret && psql -h 127.0.0.1 -U local)
export PGPASSWORD=secret && psql -h 127.0.0.1 -U local prod_copy < /tmp/backup.sql
but got an error
bash: psql: command not found
Is it possible to run psql from database container?