Hi,
I am a beginner in this technology, i need a postgresql docker image with just a user credentials in it
Uname - postgres
Password - password
i am not understaning this docker file. its a huge file.
For ex: when i had a requirement of running my webapp in tomcat, i just did this, which is very simple
FROM tomcat:8.0.21-jre8
COPY ./software/ /usr/local/tomcat/webapps/
i need something similar to the above one, i just need postgresql with user credentials as mentioned above. Please help me out
Thanks
Krishna P
docker run -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password ... postgres:9.6
or
FROM postgres:9.6
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD password
@tianon
thanks for your reply,
After i deployed my code into docker container, im still not able to access my application.
Error:
"This site can鈥檛 be reached
server refused to connect"
please find the docker container logs
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 dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
Success. You can now start the database server using:
pg_ctl -D /var/lib/postgresql/data -l logfile start
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.
waiting for server to start....LOG: could not create IPv6 socket: Address family not supported by protocol
LOG: database system was shut down at 2017-01-28 07:42:57 UTC
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
done
server started
ALTER ROLE
/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
LOG: received fast shutdown request
LOG: aborting any active transactions
waiting for server to shut down....LOG: autovacuum launcher shutting down
LOG: shutting down
LOG: database system is shut down
done
server stopped
PostgreSQL init process complete; ready for start up.
LOG: could not create IPv6 socket: Address family not supported by protocol
LOG: database system was shut down at 2017-01-28 07:42:58 UTC
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
Thanks
Krishna P
@tianon
seems like my tomcat is not started, because when i try to run just this
hostname:portnumber
tomcat page is not opening, where as for other application when i just do hostname:portnumber tomcat page opens up
My docker file looks like this:
`FROM tomcat:8.0.21-jre8
FROM postgres:9.6
COPY ./software/ /usr/local/tomcat/webapps/
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD password`
can we have multiple from statement in one docker file ? Please reply
Thanks,
Krishna P
can we have multiple from statement in one docker file ?
Nope. Only the last FROM and everything after it will apply. You'll need two Dockerfiles to build two images, postgres and tomcat.
# dockerfile one
FROM tomcat:8.0.21-jre8
COPY ./software/ /usr/local/tomcat/webapps/
# dockerfile two
FROM postgres:9.6
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD password
If you are only setting a user/password for postgres then you'll probably want to look into something like docker-compose that can help you define multiple containers to start together. It'll help you set the environment variables on startup and not have to worry about building a new image. Plus, it is better not to bake secrets into the container, since someone might accidentally push them to a public place like the Docker Hub.
@yosifkit
Hi i have created two docker file, one for tomcat and another for postgres as mentioned exactly above.
i have linked both the container like this :
docker run --name psqlcontainer -d postgresdb
docker run -it --link psqlcontainer:postgresdb --name botlibrecontainer -d -p 4005:8080 botlibre
But now when i try to access the application, im getting 404.
Logs of both the containers are as follows
container one :
Internal Exception: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
container two:
waiting for server to start....LOG: could not create IPv6 socket: Address family not supported by protocol
LOG: database system was shut down at 2017-02-06 12:44:50 UTC
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
done
server started
ALTER ROLE
/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
LOG: received fast shutdown request
LOG: aborting any active transactions
waiting for server to shut down....LOG: autovacuum launcher shutting down
LOG: shutting down
LOG: database system is shut down
done
server stopped
PostgreSQL init process complete; ready for start up.
LOG: could not create IPv6 socket: Address family not supported by protocol
LOG: database system was shut down at 2017-02-06 12:44:52 UTC
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
I am suspecting that, their is something missing from a postgres docker file. i mean configuration wise.
my postgres docker file looks like this:
FROM postgres:9.6
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD password
Please help in solving this issue.
Thanks,
Krissh
The Postgres dockerfile and container logs look correct. It looks like the problem is that your application is trying to connect to localhost for database access when it should be connecting to the host postgresdb since that is the name you set on the --link.
@yosifkit
You mean their is something wrong with the linking of both the containers ?? , if so how it should be linked properly so that it wont listen to local postgres database.
Well i even thought the problem could be from the application side aswell, as it is trying to establish a connection to the postgres but due to some ipv6 issues it could not establish a connection to the db.
How do i make it so that it will listen to docker container for postgres instead of listening to localhost.
Please reply.
Thanks,
Krissh
Hi,
You should replace localhost in your app config with postgresdb, as this is the name that Docker's internal DNS server will map to the right IP address.
Also, with newer versions of Docker you _can_ use multiple FROM statements, but they will create different containers during the build process.
The intermediate containers from previous images are stopped, but you can then use COPY statements to copy stuff from them.
The final image will be based on the one mentioned in the last FROM statement.
This feature is intended to allow you to use one image with compilers and other heavy artillery to compile your application, and a runtime image with far less cruft for the final image.
Abusing the above feature you _can_ put two services (app and database) into the same image, but you will lose all the advantages you get from containerization.
I am running on the same somewhat issue.
What I do not understand is:
LOG: could not create IPv6 socket: Address family not supported by protocol
LOG: database system was shut down at 2017-01-28 07:42:58 UTC
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
which reads as it shuts down the database. Sounds like end of story. database is terminated.
Then two lines later it sates database is ready to accept connections. I thought it was down?
I do not get the state here. Is it up or down? How do I need to read the log?
'netstat' does not show me open ports.
Next thing I realize that the log stated before
pg_ctl -D /var/lib/postgresql/data -l logfile start
Ok. Aha. I need to manually start the DB within the running container? That makes sense. Logfile says how to start the DB and logfile said it was shutdown. These two statement might logically match in order to have a running db afterwards. Or am I wrong?
What would be the command to get the Database running?
One that does not work for me is this:
docker exec -ti <NameOfMyContainer> sh -c "pg_ctl -D /var/lib/postgresql/data -l logfile start"
it results in
pg_ctl: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will own the server process.
BTW, I am a docker newbie as well.
Any help appreciated. The documents on docker/postgres do sound so easy - but it feels like I am the only pie in the universe where it does not work straight away.
@greyshine Several questions here. I'll answer the first one: the end of your log file says it's up and ready. It's accepting connections, good! It also shows it's previous shutdown, which could be several days ago, or just seconds ago. Most likely the latter if you have problems with your docker container
@monoburn thank you for your reply. In the mean time I got docker/postgres working.
These messages about shutting down and accepting connection occured 'immediately' one after another. For a newbie like me such messages are just disturbingly confusing. I guess internally docker/postgres (initial docker run) startup probably does indeed shut down the database but lets it come up immedietly again...
Having the same problem with docker-compose.
I am using links and depends_on but still the db container is not available to the web container.
But if I up the db container first and the up then the web container - with that scenario web container is working fine.
version: '3'
services:
db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: password
ports:
- "5432:5432"
web:
build: .
restart: always
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
links:
- db
depends_on:
- db
_$ docker-compose up_ - doesn't work but
_$ docker-compose up db_ and then _$ docker-compose up web_ - that works fine
May be the db container is shutting down first time but saying to compose that the state is running that's why web container is trying be up and access it. By the mean time db container is restarting and not available to web container. But when I am running separately, on that time db container is already up and accessible to web container.
@abhijit838, yeah, links and depends_on only control starting order and not "only start if the dependency is ready to accept connections". A postgres container can take a significant amount of time to do the initial start-up. Instead you have to architect your applications to handle databases that are not ready at start-up (like wordpress and bonita just as you would need to do if the database (or other microservice) went down.
You probably also want to add a named volume for db in your yaml so that the database files will be reused when restarting the container.
Closing this issue as it seems the original issue was resolved.
Most helpful comment
@greyshine Several questions here. I'll answer the first one: the end of your log file says it's up and ready. It's accepting connections, good! It also shows it's previous shutdown, which could be several days ago, or just seconds ago. Most likely the latter if you have problems with your docker container