Hello guys,
I'm new in Docker's World, I had been using it for a few weeks and learning a lot.
I'm trying to create an environment using 3 linked containers (Apache with PHP, Apigility and Postgres). Here's my docker-compose.yml:
version: '2'
services:
postgres:
image: postgres:9.3
container_name: db
ports:
- "5432"
environment:
- POSTGRES_DB=mydb
- POSTGRES_USER=root
- POSTGRES_PASSWORD=root
- PGDATA=/var/lib/postgresql/data/pgdata
volumes:
- /Users/myuser/Documents/docker/pg/:/docker-entrypoint-initdb.d
- /Users/myuser/Documents/docker:/var/lib/postgresql/data
apigility:
image: apigility-dev
container_name: apigility
ports:
- "8888:80"
volumes:
- /Users/myuser/Documents/apigility:/var/www
apache:
image: myapache
container_name: prueba
depends_on:
- postgres
- apigility
ports:
- "80:80"
volumes:
- /Users/myuser/Documents:/srv/www/htdocs
Where myapache image is:
FROM opensuse:13.2
RUN zypper --non-interactive --no-gpg-checks ref; \
zypper --non-interactive in --recommends \
apache2=2.4.10 php5=5.6.1 apache2-mod_php5 \
php5-gd php5-pear php5-zip php5-pgsql postgresql php5-curl php5-mbstring mcrypt php5-mcrypt php5-suhosin nano; \
zypper clean; \
export TERM=xterm; \
sed -i 's/variables_order = "GPCS"/variables_order = "EGPCS"/g' /etc/php5/apache2/php.ini
COPY website.conf /etc/apache2/vhosts.d/website.conf
# Configuro Apache
RUN a2enmod rewrite
CMD rcapache2 start && tail -f /var/log/apache2/*log
After running docker-compose up everything is up (with some warnings), I can go to "localhost" in my browser and works. Apigility is doing the oauth login first, but then when I tried to get something from the DB, the request returns:
Connect Error: SQLSTATE[08006] [7] could not connect to server: No such file or directory↵ Is the server running locally and accepting↵ connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Am I doing something wrong? I tried every suggestion in every forum but no luck.
(sorry if this is not related with Postgres)
Not really only postgres related, but a simple fix. You application is tying to get to postgres via the Unix socket, but each process is contained so it needs to connect over the network between the containers. Since you are using docker-compose the name of the service is the DNS name that will resolve to the IP of the other container. So to get to the postgres server from either of the other two containers you would connect to postgres:5432. You would also need to set the user, password, and database. Often users will also have those as more env vars in their docker-compose yaml file and just have their application get the values from the env on startup (or a script to fill in the proper spots of config files).
Thank you @yosifkit!
How did you solve it?
also wondering ^
If you want to connect your api or service to the database in this example, basically you must have to use the name that you are using for the postgres container on the docker-compose to make the build, because of docker-compose use a DNS system with those names in order to make the connection between containers, so you have to use postgres:5432 instead of use localhost:5432 on your service side
Most helpful comment
Not really only postgres related, but a simple fix. You application is tying to get to postgres via the Unix socket, but each process is contained so it needs to connect over the network between the containers. Since you are using docker-compose the name of the service is the DNS name that will resolve to the IP of the other container. So to get to the postgres server from either of the other two containers you would connect to
postgres:5432. You would also need to set the user, password, and database. Often users will also have those as more env vars in their docker-compose yaml file and just have their application get the values from the env on startup (or a script to fill in the proper spots of config files).