-1

I'm using the Postgres image that uses VOLUME to store data and doesn't expose ports to the outside but requires linking to connect to.

I'm wondering if there's a way I can connect to this from the host machine using apps that require me to specify the host and port of the database?

duality_
  • 17,738
  • 23
  • 77
  • 95
  • This question is off-topic at Stackoverflow, which is aimed at programming questions. You might want to post this question on http://www.serverfault.com instead and delete it here. – Markus W Mahlberg Jan 02 '15 at 09:34
  • There are many [top docker questions](http://stackoverflow.com/search?q=docker) on SO that would fit your description, so I thought it was fair game. I understand though, will move it there. – duality_ Jan 02 '15 at 09:36
  • No problem at all and no harm done, but several wrongdoings don't make them right. – Markus W Mahlberg Jan 02 '15 at 09:38
  • Searched for [docker](http://stackexchange.com/search?q=docker) on Stack Exchange and got mostly SO questions, which indicates that developers go to SO for answers on docker. Going to SO means that the question will get exposure to the docker community. I'd expect the other questions to be moved to the appropriate site if the questions did not conform to the rules. Maybe it's not so wrong to ask about docker here after all. – duality_ Jan 02 '15 at 09:49
  • Which roughly translates to "If anybody does wrong, or if a critical mass is reached, it is ok if everybody does so – f*** the rules as long as I get what I want." Let me ask you a question: Gazillions of fleas eat poo. Does this make poo eating right, reasonable or acceptable? Quote from the tour: "Ask about Specific programming problems, Software algorithms, Coding techniques, Software development tools". Well, but since everybody can and will do what will what they want, quitting to fight windmills makes sense. – Markus W Mahlberg Jan 02 '15 at 09:57
  • Did not mean to say it's okay. Thought it'd be good to tell the fleas not to eat the honey if it's in the wrong owner's house, even if it is found to be very nutritious. Don't worry though, I'll delete this one, I've created one on server fault. – duality_ Jan 02 '15 at 10:12
  • Actually, I've checked the [What can I ask](http://stackoverflow.com/help/on-topic) section of SO and looks like I came to the right place: `if your question generally covers software tools commonly used by programmers and is a practical, answerable problem that is unique to software development`. Fits the description. – duality_ Jan 02 '15 at 10:18
  • @MarkusWMahlberg The problem is that Docker is very much a devops tool and therefore straddles stackoverflow and serverfault. Devops and Docker are forcing developers to understand tools that are perhaps traditionally more on the Ops side. I vote to keep Docker on StackOverflow. – Adrian Mouat Jan 03 '15 at 12:35
  • Yes, actually I'm a programmer and I'd like to take care of as many stuff as I can for my team for a project without involving DevOps too much if I can, they have a lot of other stuff to deal with. I know the time will come when they'll have to help a lot and if I can take care of things myself, I'd like to. – duality_ Jan 03 '15 at 20:37
  • @AdrianMouat: whatever is to think of "DevOps", according to yor logic, if a developer makes coffee, questions about making coffee would have to be answered, too. Imho, Stackoverflow is not a side for each and every problems a developer might face, but a sight aimed at answering questions related to programming questions. – Markus W Mahlberg Jan 05 '15 at 10:43
  • @MarkusWMahlberg I don't know what you're getting so upset about. You quoted the tour eariler: "Ask about Specific programming problems, Software algorithms, Coding techniques, Software development tools". Docker is a software development tool. Therefore it seems appropriate to me. No need to bring flies and coffee into it. – Adrian Mouat Jan 05 '15 at 11:27
  • @AdrianMouat: I am not upset. But there is a distinction between the various sites of the SE network. For good reasons, I might add. Softening those distinctions will help nobody, except for rep grinding and being lazy. – Markus W Mahlberg Jan 05 '15 at 11:41
  • @MarkusWMahlberg Sure. But I - and seemingly several others - don't agree with your distinction. If a decision is made to move all Docker questions to serverfault, fine. But until then I think it belongs on SO and will continue to answer questions here. Anyway, I think we should stop this conversation - feel free to open a question on meta if you want to discuss it more. – Adrian Mouat Jan 05 '15 at 12:18
  • Actually, there already is a question on meta for it: http://meta.stackoverflow.com/questions/276579/should-docker-questions-go-on-stackoverflow-or-serverfault-or-superuser – Adrian Mouat Jan 05 '15 at 12:50
  • @AdrianMouat: it is not "my" distinction – but maybe I am just bad at reading or my English needs a polish. http://blog.stackoverflow.com/2009/07/why-cant-you-have-just-one-site/, btw. And yeah, my hypothesis seems to be proven. – Markus W Mahlberg Jan 05 '15 at 23:17
  • Not really. Jeff says: `which community do you consider yourself a part of?`. Without a doubt, that would be SO. – duality_ Jan 06 '15 at 06:04
  • In light of recent discussions, could I have downvotes undone? – duality_ Jan 09 '15 at 07:11

2 Answers2

0

from the documentation at your link:

This image includes EXPOSE 5432 (the postgres port), so standard container linking will make it automatically available to the linked containers. The default postgres user and database are created in the entrypoint with initdb.

Mykola Gurov
  • 8,517
  • 4
  • 29
  • 27
  • This is only available to containers that are linked to the DB container. Obviously, an app such as [PG Commander](https://eggerapps.at/pgcommander/) is not running inside a linked container and thus cannot connect. – duality_ Jan 02 '15 at 09:40
  • it is available to anything that has access to the container. Do `docker ps`, find the name of your container, then `docker inspect | grep IP` to get the ip, then `psql --host `. The other thing is that this ip will be changing every time, which might be inconvenient for your use. – Mykola Gurov Jan 02 '15 at 09:55
  • This does not work. Clients outside of linked containers can't connect to the services inside if they're set up this way. The ports section of `docker ps` says `5432/tcp`, not something like `0.0.0.0:5432->5432/tcp, 0.0.0.0:5432->5432/tcp`. – duality_ Jan 02 '15 at 10:11
  • It does work. You might want to refresh your knowledge of [docker ports](http://docs.docker.com/userguide/dockerlinks/#network-port-mapping-refresher) or be more specific in your questions. – Mykola Gurov Jan 02 '15 at 12:10
  • I have, right now, a running container that is linked to the DB container and it can access the database. On the other hand, trying to connect to the same container using the same settings (same IP, port, DB name, etc.) with an app on the host does not connect. I've read that this is Docker's way of securing the connection, that's why the images use default usernames and passwords and don't mention you should change them. – duality_ Jan 02 '15 at 12:24
  • Your question was about accessing container's port from host. If you prefer to use docker linking - then, obviously, your question should be phrased differently. And yes, the image you referred to supports custom username and password. – Mykola Gurov Jan 02 '15 at 14:12
  • I said in the question title that it is linked. – duality_ Jan 02 '15 at 18:47
0

There are a couple of options. The easiest way is to expose the container via a port on the host when you do the run command. For example:

$ docker run -d -p 5432:5432 postgres
8c04d3401878cc93503950ab3f118f0c65960719b969adb06a50bb2a482e5147

Now you should be able to access the service via port 5432 on the host e.g.

$ psql -h 0.0.0.0 -p 5432 -U postgres

You could also look into the various Docker networking options such as --net=host, but I wouldn't recommend this unless you know what you're doing. The documentation is here http://docs.docker.com/articles/networking/.

However, arguably the best solution is to place the other apps in containers as well.

Adrian Mouat
  • 44,585
  • 16
  • 110
  • 102
  • Yes, that's what I'm trying to avoid to keep the system safe. And don't know how to include tools like [PG Commander](https://eggerapps.at/pgcommander/) into a container and nor would I have to. For now I'm settling on [Adminer](http://www.adminer.org/), but that's a short-term solution. – duality_ Jan 03 '15 at 20:40
  • Right, I understand now. IMO, putting the apps into another container is the best plan. Adminer seems to be a webapp, so this would be very straightforward. Other people seem to have done it already: https://registry.hub.docker.com/u/clue/adminer/ – Adrian Mouat Jan 03 '15 at 21:48
  • Heh, maybe I wasn't clear enough, I already have Adminer in a container, but as I said, that's a short-term solution, I'd like to use other tools as well. I'm sure there's a way. – duality_ Jan 04 '15 at 08:32
  • Put a GUI tool like PG Commander in a container? Put and online service like [periscope.io](http://periscope.io) in a container? I doubt it. Though a possible solution would be to have a proxy container that is linked, but don't know how. – duality_ Jan 04 '15 at 15:01
  • Well, I assume the problem you were trying to avoid was exposing the admin port to the world, so on-line services are out. There's no reason you can't run a GUI tool in a container; just expose the X server socket or use SSL/VNC. – Adrian Mouat Jan 04 '15 at 15:28
  • Having said that, PG commander seems to be Mac only. I'm not sure what you're trying to achieve now; you will always need to expose a port on the host to connect via a Mac app. – Adrian Mouat Jan 04 '15 at 15:31