How to alter the mount point of PostgreSQL through Docker? - postgresql

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.

Related

How to mount postgres backup into the postgres container running

docker run -d --rm --name dummy -v postgres_volume:/root alpine tail -f /dev/null
docker cp ./data dummy:/root
docker stop dummy
docker run -p 5432:5432 -v postgres_volume:/var/lib/postgresql -e POSTGRES_PASSWORD=password postgres
The above commands giving error: mkdir: cannot create directory ‘/var/lib/postgresql’: Permission denied.
./data in the docker cp command contains the Postgres backup.
What am I missing?
You (or rather postgres) are missing permissions for the files you have copied. This should fix it:
docker run --rm -v postgres_volume:/var/lib/postgresql postgres /bin/sh -c \
'chown --recursive $(id -u postgres):$(id -g postgres) /var/lib/postgresql'

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

Docker - Unable to access postgres DB

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