Creating multiple PostgreSQL containers in docker in fedora - postgresql

I want to create 2 containers of postgrSQL so that one can be used as DEV and other as DEV_STAGE.
I was able to successfully create one container and it is been assigned to port 5432. But when I'm trying to the second container, it is getting created(sometimes shows the status as EXITED) but not getting started because of the port number issue.
The below are the commands which I ran.
sudo docker run -v "pwd/data:/var/lib/pgsql/data:Z" -e POSTGRESQL_USER=user1 -e POSTGRESQL_PASSWORD=password -e POSTGRESQL_DATABASE=test_db -d -p 5432:5432 fedora/postgresql
sudo docker run -v "pwd/data_stage:/var/lib/pgsql/data_stage:Z" -e POSTGRESQL_USER=user1 -e POSTGRESQL_PASSWORD=password -e POSTGRESQL_DATABASE=test_db -d -p 5432:5433 fedora/postgresql
I think the port mapping which I'm using is incorrect. But not able to get the correct one.

You have an error in volume definition of the second container. Don't change path after colon, it is mandatory the path is set to /var/lib/pgsql/data.
Also you fliped ports mapping. The correct command is like this:
sudo docker run -v "`pwd`/data_stage:/var/lib/pgsql/data:Z" -e POSTGRESQL_USER=user1 -e POSTGRESQL_PASSWORD=password -e POSTGRESQL_DATABASE=test_db -d -p 5433:5432 fedora/postgresql
If anything goes wrong inspect container logs with docker logs CONTAINER_ID

Thanks for the answer. I corrected the path. I think flipping the port number will not work too. Because I already have one container which is mapped to 5432. So I can't map the port to 5432 again. The below command with worked for me. First, I modified Postgres default port to 5433 using export variable PGPORT=5433.
sudo docker run -v "`pwd`/data_stg:/var/lib/pgsql/data:Z" -e PGPORT=5433 -e POSTGRESQL_USER=user1 -e POSTGRESQL_PASSWORD=password -e POSTGRESQL_DATABASE=test_db -d -p 5433:5433 fedora/postgresql

Related

Trying to run postgres on docker. It gives error

I am trying to run postgres on docker with this cmd and It gives error
PS D:\Data Engineering with Zoomcamp> docker run -it -e POSTGRES_USER="root" -e POSTGRES_PASSWORD="root" -e POSTGRES_DB="ny_taxi"D:\Data Engineering with Zoomcamp\ny_taxi_postgres:/var/lib/postgresql/data -p 5432:5432 postgres:13
docker: invalid reference format: repository name must be lowercase.
See 'docker run --help'.
you forgot to add the volume tag. Use below command
docker run -it -e POSTGRES_USER="root" -e POSTGRES_PASSWORD="root" -e POSTGRES_DB="ny_taxi" -v "D:\Data Engineering with Zoomcamp\ny_taxi_postgres":/var/lib/postgresql/data -p 5432:5432 postgres:13

postgres on docker fails with `chown: cannot dereference ... : No such file or directory`

I am trying to deploy postgres on docker and it exits immediately after I run it. On checking the logs I get the following error:
chown: cannot dereference '/var/lib/postgresql/data/venv/bin/python3': No such file or directory
The command am running is here below:
sudo docker run -p 5432:5432 -e POSTGRES_USER=superset -e POSTGRES_PASSWORD=mypostgrespassword -e POSTGRES_DB=superset --volume $PWD:/var/lib/postgresql/data -d postgres
How can I fix that?
The problem here is, that the directory you are mounting, is not empty.
Create a empty directory, for example /opt/pgdata and then mount that one:
docker run -p 5432:5432 -e POSTGRES_USER=superset -e POSTGRES_PASSWORD=mypostgrespassword -e POSTGRES_DB=superset -v /opt/pgdata:/var/lib/postgresql/data -d postgres

Running new meteorhacks/meteord application, don't have meteor app

I try to create a new project based on the meteor with docker.
I found the repository for this:
https://github.com/meteorhacks/meteord
I created Dockerfile and put there
FROM meteorhacks/meteord:onbuild
And then run:
docker run meteorhacks/meteord
docker run mongo
After downloading all packages so finally I run
docker run -i -t 807754a01782 -d
-e ROOT_URL=http://localhost:3000
-e MONGO_URL=mongodb://127.0.0.1:27017/
-e MONGO_OPLOG_URL=mongodb://127.0.0.1:27017/
-p 8080:80 myapp
Based on this example:
docker run -d \
-e ROOT_URL=http://yourapp.com \
-e MONGO_URL=mongodb://url \
-e MONGO_OPLOG_URL=mongodb://oplog_url \
-p 8080:80 \
yourname/app
Inside myapp folder, I have fresh meteor project.
But as a result, I received
> You don't have an meteor app to run in this image.
Can anyone help me and give me some clues what I'm doing wrong? Or I misunderstanding how Docker with this repository works?
EDIT:
The problem was in command correct command is:
docker run -d
-e ROOT_URL=http://localhost:3000
-e MONGO_URL=mongodb://127.0.0.1:27017/
-e MONGO_OPLOG_URL=mongodb://127.0.0.1:27017/ -p 8080:80
meteorhacks/meteord:base
But now when I check the status of this container I see it is excited. How Can I check what causing the problem?

How to start Phoenix by using PostgreSQL through container?

I tried:
$ alias psql="docker exec -ti pg-hello-phoenix sh -c 'exec psql -h localhost -p 5432 -U postgres'"
$ mix ecto.create
but got:
** (RuntimeError) could not find executable psql in path, please guarantee it is available before running ecto commands
lib/ecto/adapters/postgres.ex:106: Ecto.Adapters.Postgres.run_with_psql/2
lib/ecto/adapters/postgres.ex:83: Ecto.Adapters.Postgres.storage_up/1
lib/mix/tasks/ecto.create.ex:34: anonymous fn/2 in Mix.Tasks.Ecto.Create.run/1
(elixir) lib/enum.ex:604: Enum."-each/2-lists^foreach/1-0-"/2
(elixir) lib/enum.ex:604: Enum.each/2
(mix) lib/mix/cli.ex:58: Mix.CLI.run_task/2
(elixir) lib/code.ex:363: Code.require_file/2
Also I tried to create symlink /usr/local/bin/psql:
#!/usr/bin/env bash
docker exec -ti pg-hello-phoenix sh -c "exec psql -h localhost -p 5432 -U postgres $#"
and then:
$ sudo chmod +x /usr/local/bin/psql
check:
$ which psql
/usr/local/bin/psql
$ psql --version
psql (PostgreSQL) 9.5.1
run again:
$ mix ecto.create
** (Mix) The database for HelloPhoenix.Repo couldn't be created, reason given: cannot enable tty mode on non tty input
.
Container with PostgreSQL is launched:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
013464d7227e postgres "/docker-entrypoint.s" 47 minutes ago Up 47 minutes 5432/tcp pg-hello-phoenix
I was able to do this by going into /config/.exs In my case it was development, so /config/dev.exs and left the hostname as localhost but added another setting for port: 32768 because that's the port that docker exposed.
Make sure to put a space between the port: and the number (not string). Otherwise it won't work.
Worked as usual after that. The natural assumption is that the username / password matches on the container as well.
To me I did the following:
sudo docker exec -it postgres-db bash
After I got the interactive shell
psql -h localhost -p 5432 -U postgres
Then I create my db manually:
CREATE DATABASE cars_dev;
Then finally:
mix ecto.migrate
Everything worked fine after that :) hope it helps.

Why can't you start postgres in docker using "service postgres start"?

All the tutorials point out to running postgres in the format of
docker run -d -p 5432 \
-t <your username>/postgresql \
/bin/su postgres -c '/usr/lib/postgresql/9.2/bin/postgres \
-D /var/lib/postgresql/9.2/main \
-c config_file=/etc/postgresql/9.2/main/postgresql.conf'
Why can't we in our Docker file have:
ENTRYPOINT ["/etc/init.d/postgresql-9.2", "start"]
And simply start the container by
docker run -d psql
Is that not the purpose of Entrypoint or am I missing something?
the difference is that the init script provided in /etc/init.d is not an entry point. Its purpose is quite different; to get the entry point started, in the background, and then report on the success or failure to the caller. that script causes a postgres process, usually indirectly via pg_ctl, to be started, detached from the controlling terminal.
for docker to work best, it needs to run the application directly, attached to the docker process. that way it can usefully and generically terminate it when the user asks for it, or quickly discover and respond to the process crashing.
Exemplify that IfLoop said.
Using CMD into Dockerfiles:
USE postgres
CMD ["/usr/lib/postgresql/9.2/bin/postgres", "-D", "/var/lib/postgresql/9.2/main", "-c", "config_file=/etc/postgresql/9.2/main/postgresql.conf"]
To run:
$docker run -d -p 5432:5432 psql
Watching PostgeSQL logs:
$docker logs -f POSTGRES_CONTAINER_ID