Unable to start docker container, "docker ps -a" STATUS = Exited (1) - postgresql

I'm trying to start a postgres instance as described in docker hub.
To do that, I ran the following command:
sudo docker run --name database -e POSTGRES_PASSWORD=supersecret -p 5432:5432 -d postgres
When I run docker ps it shows nothing, and when I run docker ps -a it shows:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
967ebe7efb74 postgres "docker-entrypoint.s…" 2 minutes ago Exited (1) 2 minutes ago database
Trying to docker start database also leads to STATUS Exited (1) as displayed above.
Here are the logs displayed by docker logs -f database:
PostgreSQL init process complete; ready for start up.
2019-09-10 14:08:26.941 UTC [1] LOG: could not create IPv6 socket for address "::": Permission denied
2019-09-10 14:08:26.941 UTC [1] LOG: could not create IPv4 socket for address "0.0.0.0": Permission denied
2019-09-10 14:08:26.941 UTC [1] WARNING: could not create listen socket for "*"
2019-09-10 14:08:26.941 UTC [1] FATAL: could not create any TCP/IP sockets
2019-09-10 14:08:26.941 UTC [1] LOG: database system is shut down
In my research on the internet to solve this problem, some people said that it could be something with my hosts file, but it seems fine as shown below.
127.0.0.1 localhost
127.0.1.1 user-PC
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
I tried to reinstall docker following the docker docs step-by-step tutorial, and I also did the post installation steps without success.
OS: Deepin GNU/Linux 15.11
Docker: Docker version 18.09.6, build 481bc77

Because you confirmed it, I write the comment as answer.
Problem is due to apparmor.
Try disabling it or, better, to configure the security profile

Related

Unable to connect to docker container which includes a postgres instance

I have a docker instance and I have changed the file =:
"/var/lib/postgreqsql/data/pg_hba.conf"
And it now has errors.
It appears that this is preventing me actually connecting to the docker instance.
docker logs --tail 20 postgres
2022-08-04 00:30:26.392 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2022-08-04 00:30:26.392 UTC [1] LOG: listening on IPv6 address "::", port 5432
2022-08-04 00:30:26.417 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2022-08-04 00:30:26.451 UTC [1] LOG: end-of-line before authentication method
2022-08-04 00:30:26.451 UTC [1] CONTEXT: line 87 of configuration file "/var/lib/postgreqsql/data/pg_hba.conf"
2022-08-04 00:30:26.451 UTC [1] FATAL: could not load pg_hba.conf
2022-08-04 00:30:26.453 UTC [1] LOG: database system is shut down
When I try and connect I get a consistent message as follows:
root#VM01:~/vm01-docker# docker exec -u root -it postgres bash
Error response from daemon: Container 4545dcba980215c is restarting, wait until the container is running
root#VM01:~/vm01-docker#
Stopping and starting as follows:
docker-compose stop postgres
docker-compose start postgres
Has no impact the issue remains.
What I need to do is be able to connect again to the instance and edit the file pg_hba.conf again to correct the fault but I am not sure how I can. (I am a docker newb.)

Unable to build the PostgreSQL environment. FATAL: database "t" does not exist

I installed postgres from brew.
I got an error, I tried to delete the postmaster.id
The file seems to be missing.
rm -f data/postmaster.pid
It had no effect.
I tryed
$brew install postgres
$psql --version
psql (PostgreSQL) 14.1
$postgres --version
postgres (PostgreSQL) 14.1
$psql
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: database "t" does not exist
$postgres -D /usr/local/var/postgres
2022-01-28 14:27:14.406 JST [94892] LOG: starting PostgreSQL 14.1 on aarch64-apple-darwin21.1.0, compiled by Apple clang version 13.0.0 (clang-1300.0.29.3), 64-bit
2022-01-28 14:27:14.408 JST [94892] LOG: could not bind IPv6 address "::1": Address already in use
2022-01-28 14:27:14.408 JST [94892] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2022-01-28 14:27:14.408 JST [94892] LOG: could not bind IPv4 address "127.0.0.1": Address already in use
2022-01-28 14:27:14.408 JST [94892] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2022-01-28 14:27:14.408 JST [94892] WARNING: could not create listen socket for "localhost"
2022-01-28 14:27:14.408 JST [94892] FATAL: could not create any TCP/IP sockets
2022-01-28 14:27:14.409 JST [94892] LOG: database system is shut down
$rm /usr/local/var/postgres/postmaster.pid
rm: /usr/local/var/postgres/postmaster.pid: No such file or directory
$sudo lsof -i:5432
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 94724 t 7u IPv6 0xe42d991dab624b7 0t0 TCP localhost:postgresql (LISTEN)
postgres 94724 t 8u IPv4 0xe42d991d7856c0f 0t0 TCP localhost:postgresql (LISTEN)
I don't know why you insist in wrecking your PostgreSQL server by removing the PID file. I wish you would stop doing that.
The error message means that you are trying to connect to database t, but that database does not exist. Since you do not explicitly specify a database, the causes could be:
you are operating system user t, so the connection defaults to database t
the environment variable PGDATABASE is set to t
the environment variable PGUSER is set to t, and the connection defaults to the same database name as the user name
In any event, the solution is to pick an existing database and use
psql -d dbname

Prometheus PostgreSQL server exporter example not working on MacOS?

I'd like to try out the quick start example at https://github.com/wrouesnel/postgres_exporter on a MacOS host device. In one terminal, I run a postgres image in interactive mode on the host network:
> docker run --network=host -it --rm --env POSTGRES_PASSWORD=password postgres
Unable to find image 'postgres:latest' locally
latest: Pulling from library/postgres
8f91359f1fff: Pull complete
c6115f5efcde: Pull complete
28a9c19d8188: Pull complete
2da4beb7be31: Pull complete
fb9ca792da89: Pull complete
cedc20991511: Pull complete
b866c2f2559e: Pull complete
5d459cf6645c: Pull complete
cf66247ad881: Pull complete
35e29440d9da: Pull complete
362779251360: Pull complete
fb82d778d08d: Pull complete
b1f8d21ff25d: Pull complete
6d49eb0e8dd0: Pull complete
Digest: sha256:be456a40361cd836e0e1b35fc4d872e20e138f214c93138425169c4a2dfe1b0e
Status: Downloaded newer image for postgres:latest
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... Etc/UTC
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
pg_ctl -D /var/lib/postgresql/data -l logfile start
waiting for server to start....2019-10-03 19:06:10.222 UTC [42] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2019-10-03 19:06:10.235 UTC [43] LOG: database system was shut down at 2019-10-03 19:06:10 UTC
2019-10-03 19:06:10.239 UTC [42] LOG: database system is ready to accept connections
done
server started
/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
waiting for server to shut down....2019-10-03 19:06:10.320 UTC [42] LOG: received fast shutdown request
2019-10-03 19:06:10.322 UTC [42] LOG: aborting any active transactions
2019-10-03 19:06:10.325 UTC [42] LOG: background worker "logical replication launcher" (PID 49) exited with exit code 1
2019-10-03 19:06:10.325 UTC [44] LOG: shutting down
2019-10-03 19:06:10.338 UTC [42] LOG: database system is shut down
done
server stopped
PostgreSQL init process complete; ready for start up.
2019-10-03 19:06:10.436 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2019-10-03 19:06:10.438 UTC [1] LOG: listening on IPv6 address "::", port 5432
2019-10-03 19:06:10.440 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2019-10-03 19:06:10.450 UTC [51] LOG: database system was shut down at 2019-10-03 19:06:10 UTC
2019-10-03 19:06:10.453 UTC [1] LOG: database system is ready to accept connections
while in another terminal, I run postgres_exporter and connect it to that database:
> docker run --net=host --env DATA_SOURCE_NAME="postgresql://postgres:password#localhost:5432/postgres?sslmode=disable" wrouesnel/postgres_exporter
Unable to find image 'wrouesnel/postgres_exporter:latest' locally
latest: Pulling from wrouesnel/postgres_exporter
0d6d2d2516f9: Pull complete
e9d7b571ef5e: Pull complete
Digest: sha256:aeea975f0efeacb49c170f0f7c4a4000d3f0099cc33437aedd3f276e628cde1c
Status: Downloaded newer image for wrouesnel/postgres_exporter:latest
time="2019-10-03T19:09:20Z" level=info msg="Established new database connection to \"localhost:5432\"." source="postgres_exporter.go:778"
time="2019-10-03T19:09:20Z" level=info msg="Semantic Version Changed on \"localhost:5432\": 0.0.0 -> 11.5.0" source="postgres_exporter.go:1238"
time="2019-10-03T19:09:20Z" level=info msg="Starting Server: :9187" source="postgres_exporter.go:1459"
As I understand it, I should be able to go to localhost:9187 in my browser and see the exported metrics. What I get, however, is that the connection gets refused:
> curl http://localhost:9187
curl: (7) Failed to connect to localhost port 9187: Connection refused
What I suspect is that this is because, as documented at https://docs.docker.com/network/host/,
The host networking driver only works on Linux hosts, and is not supported on Docker Desktop for Mac, Docker Desktop for Windows, or Docker EE for Windows Server.
Is this what is causing the issue and if so, how can I adapt this quickstart example to work using Docker Desktop for Mac?
A little late, but maybe for someone in the future. I had the same problem on Docker Desktop for Windows. For it to work I used the following command:
docker run -p 9187:9187 -e DATA_SOURCE_NAME="postgresql://postgres:password#localhost:5432/postgres?sslmode=disable" wrouesnel/postgres_exporter

could not bind IPv4 socket: Permission denied

I am trying to set up a new instance of PostgreSQL 9.6 on a machine. I have tested it on another machine and its working fine on that machine. But the same process is not working on new machine. Below are the steps I am using
created a new data directory with below command
/opt/rh/rh-postgresql96/root/bin/initdb -D /var/lib/pgsql/9.6/data/
created a service file /etc/systemd/system/rh-postgresql96-inst2.service with below content
.include /lib/systemd/system/rh-postgresql96-postgresql.service
[Service]
Environment=PGDATA=/var/lib/pgsql/9.6/data/
Environment=PGPORT=5433
User=postgres
Group=root
registered service using command systemctl enable rh-postgresql96-inst2
now using command systemctl start rh-postgresql96-inst2 to start service.
All these steps are working fine on one machine but not on the 2nd one.
I am getting below error while starting service on the 2nd machine
rh-postgresql96-inst2.service - PostgreSQL database server
Loaded: loaded (/etc/systemd/system/rh-postgresql96-inst2.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2018-06-18 09:59:01 UTC; 10s ago
Process: 7552 ExecStart=/opt/rh/rh-postgresql96/root/usr/libexec/postgresql-ctl start -D ${PGDATA} -s -w -t ${PGSTARTTIMEOUT} (code=exited, status=1/FAILURE)
Process: 7550 ExecStartPre=/opt/rh/rh-postgresql96/root/usr/libexec/postgresql-check-db-dir %N (code=exited, status=0/SUCCESS)
HINT: Is another postmaster already running on port 5433? If not, wait a few seconds and retry.
LOG: could not bind IPv4 socket: Permission denied
HINT: Is another postmaster already running on port 5433? If not, wait a few seconds and retry.
WARNING: could not create listen socket for "localhost"
FATAL: could not create any TCP/IP sockets
LOG: database system is shut down
systemd[1]: rh-postgresql96-inst2.service: control process exited, code=exited status=1
systemd[1]: Failed to start PostgreSQL database server.
systemd[1]: Unit rh-postgresql96-inst2.service entered failed state.
systemd[1]: rh-postgresql96-inst2.service failed.
However, I am able to start service using pg_ctl.
Also, I have checked with netstat, lsof command to check if any other postgresql instance is running on port 5433 but its not the case.
Infact i tried 5431, 5434 ports also but server is not starting up
Instead of turning of SELinux you should allow postgres to bind to port 5433 in SELinux.
There is a port parameter postgresql_port_t which by default has port 5432 and 9898.
semanage port -l | grep post
postgresql_port_t tcp 5433, 9898
What you could do is simply add port 5433 to this list.
semanage port -a -t postgresql_port_t 5433 -p tcp
semanage port -l | grep post
postgresql_port_t tcp 5433, 5432, 9898
After that you can start your postgres server listening on port 5433
systemctl enable rh-postgresql96-postgresql
systemctl start rh-postgresql96-postgresql
netstat -tulpn
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 2847/postgres
tcp 0 0 127.0.0.1:5433 0.0.0.0:* LISTEN 2775/postgres
There is also a handy tool called audit2allow to help debug selinux problems.
audit2allow -m whatiswrong < /var/log/audit/audit.log > /root/showme.te
The file showme.te show you why SELinux is not allowing the service to do what you need.
You should not turn off SELinux just because it's hard to understand or if you don't know how it works. Instead you should study it :)
I reccomend this lecture from the Red Hat Summit https://www.redhat.com/en/about/videos/summit-2018-security-enhanced-linux-mere-mortals
This issue was related to SELinux.
When I run command sestatus on both machines, output was a little bit different.
One server had Current mode: permissive and 2nd one had Current mode: enforcing.
So I changed the current mode to permissive on the 2nd machine using command setenforce 0.
and it resolved the permission related issue. Now I am able to start 2nd instance.

Catalyst exiting when started with start-stop-daemon

I am trying to run Catalyst on CentOS 7 using start-stop-daemon. Here is the start-stop-daemon command that I run:
start-stop-daemon --start --pidfile /var/run/myapp.pid -d "/home/user/myapp" --exec /opt/perlbrew/perls/perl-5.22.0/bin/perl --startas "/home/user/myapp/script/myapp_fastcgi.pl" --chuid root --make-pid -- "-l :8100 -n 6"
Then I get this error:
Cannot resolve host name -- exiting!
It displays this error after loading the chained actions and printing them to the screen, and after displaying the final message:
[info] myapp powered by Catalyst 5.90112
In /etc/hosts I've tried commenting out any hostnames I thought might be causing an issue:
127.0.0.1 myapp.com myapp.com
#127.0.0.1 localhost.localdomain localhost
#127.0.0.1 localhost4.localdomain4 localhost4
# The following lines are desirable for IPv6 capable hosts
#::1 myapp.com myapp.com
#::1 localhost.localdomain localhost
#::1 localhost6.localdomain6 localhost6
What's strange is that if I don't use start-stop-daemon and I just start the server from the command-line, the server starts fine.
Most likely it can't resolve your hostname.
Check what your hostname command returns and make sure that same host name is present in your /etc/hosts. And don't assign it to loopback, use a real IP.
You can also trace what exactly it's trying to resolve by using this method
https://serverfault.com/questions/666482/how-to-find-out-pid-of-the-process-sending-packets-generating-network-traffic
Or might be even more simple to do tcpdump -s 0 port 53