Docker - Unable to access postgres DB - postgresql

im trying to setup NextCloud on Postgres with Docker but im unable to access/reach the postgress container from the nextcloud setup page.
Here is my setup:
docker network create --driver bridge nextcloud
docker run -p 127.0.0.1:5432:5432 \
--name postgres \
--link cloud.mydomain.com \
--net=nextcloud \
-e POSTGRES_PASSWORD=supersecretpass123 \
-e POSTGRES_USER=nextcloud \
-e POSTGRES_DB=nextcloud \
-v postgres-data:/var/lib/postgresql/data \
-d postgres
docker run -d -p 127.0.0.1:8080:80 \
--name="cloud.mydomain.com" \
-e VIRTUAL_HOST=cloud.mydomain.com \
-v nextcloud:/var/www/html \
--net=nextcloud \
nextcloud
docker run -d -p 80:80 -p 443:443 --name="cloud.mydomain.com-proxy" \
--net=nextcloud \
-v /srv/gitlab:/etc/nginx/vhost.d:ro \
-v /root/certs:/etc/nginx/certs \
-v /var/run/docker.sock:/tmp/docker.sock:ro \
--restart always \
jwilder/nginx-proxy:latest
Any Suggestion?

You need to invert the link, add the --link postgres to cloud.mydomain.com:
docker run -d -p 127.0.0.1:8080:80 \
--name="cloud.mydomain.com" \
--link postgres \
-e VIRTUAL_HOST=cloud.mydomain.com \
-v nextcloud:/var/www/html \
--net=nextcloud \
nextcloud

Related

Dockerized Postgresql : Delete PGDATA content

I am trying to configure replication between two Postgresql docker containers. All tutorials out there - which are based on regular VM, not containers - states that a backup from the Master has to be done in the Replica / StandBy, through a command like this:
pg_basebackup -h 192.168.0.108 -U replicator -p 5432 -D $PGDATA -Fp -Xs -P -R
Unfortunately, this throws an error:
pg_basebackup: error: directory "/var/lib/postgresql/data" exists but is not empty
I cannot delete the content of this folder because the Postgresql service will crash and I will be kicked out of the container.
So, how can I do this given that I cannot stop the Postgresql service because of the containerized application?
These instructions following the percona docs for configuration
postgres replication.
Create a network for the postgres servers:
docker network create pgnet
Start the primary database server:
docker run -d \
--network pgnet \
-v primary_data:/var/lib/postgresql/data \
--name pg_primary \
-e POSTGRES_PASSWORD=secret \
-e POSTGRES_DB=exampledb \
docker.io/postgres:14
Create a replication user and configure pg_hba.conf to allow
replication access:
docker exec pg_primary \
psql -U postgres -c \
"CREATE USER replicator WITH REPLICATION ENCRYPTED PASSWORD 'secret'"
docker exec pg_primary \
sh -c 'echo host replication replicator 0.0.0.0/0 md5 >> $PGDATA/pg_hba.conf'
docker exec pg_primary \
psql -U postgres -c 'select pg_reload_conf()'
Prepare the pgdata directory for the replica server. We do this by running the pg_basebackup command in an ephemeral container:
docker run -it --rm \
--network pgnet \
-v replica_data:/var/lib/postgresql/data \
--name pg_replica_init \
docker.io/postgres:14 \
sh -c 'pg_basebackup -h pg_primary -U replicator -p 5432 -D $PGDATA -Fp -Xs -P -R -W'
(The -W in the above command forces pg_basebackup to prompt for a
password. There are other ways you could provide the password; the
postgres docs have details.)
Start the replica:
docker run -d \
--network pgnet \
-v replica_data:/var/lib/postgresql/data \
--name pg_replica \
docker.io/postgres:14
Verify that replication is working. Create a table in the primary:
docker exec pg_primary \
psql -U postgres exampledb -c 'create table example_table (id int, name text)'
See that it shows up in the replica:
docker exec pg_replica \
psql -U postgres exampledb -c '\d'
You should see as output:
List of relations
Schema | Name | Type | Owner
--------+---------------+-------+----------
public | example_table | table | postgres
(1 row)

How to install pgAdmin 4 using Docker

I am trying to install pgadmin4 using Docker in Ubuntu 18.04 LTS, but each time I create a container it crashes. Am I missing something in the below command
$ docker pull dpage/pgadmin4
$ docker run -p 80:80 \
-e 'PGADMIN_DEFAULT_EMAIL=atinesh.s#gmail.com' \
-e 'PGADMIN_DEFAULT_PASSWORD=admin' \
-d dpage/pgadmin4
Solved the issue with following command
$ docker run -p 80:80 \
-e PGADMIN_DEFAULT_EMAIL="atinesh.s#gmail.com" \
-e PGADMIN_DEFAULT_PASSWORD="admin" \
-d dpage/pgadmin4

Docker: repository name must be lowercase

Im getting errors trying to run my docker containers. I need the postgres and redis connected to my server application.
docker pull postgres
docker rm -f syda-postgres
docker run -p 30203:5432 --name syda-postgres -e POSTGRES_PASSWORD=password POSTGRES_USER=root POSTGRES_DB=syda postgres
docker pull redis
docker rm -f syda-inmemory
docker run -d -p 30204:6379 --name syda-inmemory redis redis-server --appendonly yes
docker pull docker.url.ee/syda/server:latest
docker rm -f syda-server
docker run -d -p 30202:8080 --name syda-server --link syda-postgres:postgres --link syda-inmemory:redis \docker.url.ee/syda/server:latest
This is the error im getting:
Error: No such container: syda-postgres
docker: invalid reference format: repository name must be lowercase.
See 'docker run --help'.
Error: No such container: syda-server
docker: Error response from daemon: could not get container for syda-postgres: No such container: syda-postgres.
See 'docker run --help'.
docker run -p 30203:5432 --name syda-postgres -e POSTGRES_PASSWORD=password POSTGRES_USER=root POSTGRES_DB=syda postgres
That tries to run a container from the image named POSTGRES_USER=root with the command/arguments to the entrypoint POSTGRES_DB=syda postgres. You need to pass the -e for each variable like:
docker run -p 30203:5432 --name syda-postgres \
-e POSTGRES_PASSWORD=password -e POSTGRES_USER=root -e POSTGRES_DB=syda \
postgres
Also, note that links are deprecated, you should use a shared network for communicating between containers. This is often done with a compose file. If you need to do it from a script, you could run:
docker pull postgres
docker pull redis
docker pull docker.url.ee/syda/server:latest
docker rm -f syda-postgres
docker rm -f syda-inmemory
docker rm -f syda-server
docker network rm syda-net
docker network create syda-net
docker run -p 30203:5432 --net syda-net --name syda-postgres \
-e POSTGRES_PASSWORD=password -e POSTGRES_USER=root -e POSTGRES_DB=syda \
postgres
docker run -d -p 30204:6379 --net syda-net --name syda-inmemory \
redis redis-server --appendonly yes
docker run -d -p 30202:8080 --net syda-net --name syda-server \
docker.url.ee/syda/server:latest

Docker Volumes on Mac with Postgres

I'm attempting to run the following command on Mac OSX:
docker run --rm --name kong-database \
--network=kong-net \
-v /Volumes/docker/postgres:/var/lib/postgresql/data \
-p 5432:5432 \
-e "POSTGRES_USER=kong" \
-e "POSTGRES_DB=kong" \
postgres:9.6
With the intention that it will launch postgres in in a docker container, and persist the database data on my local file system in /Volumes/docker/postgres. However, I get the following error, and the container fails to run:
chown: changing ownership of '/var/lib/postgresql/data': Operation not permitted
Any idea what I can do to get this going?
Turns out that /Volumes was owned by root. When I switched this to something like /Users/JoshuaErney/Volumes, this ran successfully. Here's an example:
docker run --rm --name kong-database \
--network=kong-net \
-v /Users/JoshuaErney/Volumes/docker/postgres:/var/lib/postgresql/data \
-p 5432:5432 \
-e "POSTGRES_USER=kong" \
-e "POSTGRES_DB=kong" \
postgres:9.6
Answer by jerney is correct, here is what it looks like with docker-compose.yaml:
services:
test-postgres-compose :
...
volumes :
- pgdata:/var/lib/postgresql/data
...
volumes :
pgdata :
Docker compose volumes info

How to alter the mount point of PostgreSQL through Docker?

I want to spin-up a docker for postgres:latest but let the data be stored on the host. This is how I do it right now but doesn't seem to work. Any ideas?
docker run --name name1 -e POSTGRES_PASSWORD=PASSWORD1 -e POSTGRES_USER=USER1 -e PGDATA=/my/local/path/postgresql -e POSTGRES_DB=DB1 -d -P postgres:latest
You need to mount the host directory as a data volume first with -v host_path:contiainer_path:mode.
docker run \
--name name1 \
-v /my/local/path/postgresql:/data \
-e POSTGRES_PASSWORD=PASSWORD1 \
-e POSTGRES_USER=USER1 \
-e PGDATA=/data \
-e POSTGRES_DB=DB1 \
-d -P postgres:latest
The path should exist in the container image.