Reputation: 1999
I am new to the docker world and I have some issues regarding how to connect 2 docker services tougher.
I am using https://memgraph.com/ as my database and when I am running it locally I am running it like this
docker run -it -p 7687:7687 -p 3000:3000 memgraph/memgraph-platform
I wrote my program which is going to connect to the database using mgclient and when I am running it locally everything is working fine.
Now I am trying to put in inside the docker container and running it using docker-compose.yaml
My docker-compose.yaml is:
version: "3.5"
services:
memgraph:
image: memgraph/memgraph-platform:2.1.0
container_name: memgraph_container
restart: unless-stopped
ports:
- "7687:7687"
- "3000:3000"
my_app:
image: memgraph_docker
container_name: something
restart: unless-stopped
command: python main.py
and when I am trying to run it with this command:
docker-compose up
I am getting an error regarding the connection to the server. Could anyone tell me what I am missing regarding the docker-compose.yaml?
Upvotes: 2
Views: 157
Reputation: 40306
How does your my_app
connect to the database?
Are you using a connection string of the form localhost:7687
(or perhaps localhost:3000
)? This would work locally because you are publishing (--publish=7687:7687 --publish=3000:3000
) the container's ports 7687
and 3000
to the host port's (using the same ports).
NOTE You can remap ports when your
docker run
. For example, you could--publish=9999:7686
and then you would need to use port9999
on your localhost to access the container's port7687
.
When you combine the 2 containers using Docker Compose, each container is given a name that matches the service name. In this case, your Memgraph database is called memgraph
(matching the service
name).
Using Docker Compose, localhost
takes on a different mean. From my_app
, localhost
is my_app
. So, using localhost
under Docker Compose, my_app
would try connecting to itself not the database.
Under Docker Compose, for my_app
(the name for your app), you need to refer to Memgraph by its service name (memgraph
). The ports will be unchanged as both 7687
and 3000
(whichever is correct).
NOTE The
ports
statement in your Docker Compose config is possibly redundant *unless you want to be able to access the database from your (local)host (which you may for debugging). From a best practice standpoint, oncemy_app
is able to access the database correctly, you don't need to expose the database's ports to the host.
It is good practice to externalize configuration (from your app). So that you can configure your app dynamically. An easy way to do this is to use environment variables.
For example:
main.py
:
import os
conn = connect(
host=os.getenv("HOST"),
port=os.getenv("PORT"),
)
Then, when you run under e.g. Docker, you need to set these values:
docker run ... --env=HOST="localhost" --env=PORT="7687" ...
And under Docker Compose, you can:
version: "3.5"
services:
memgraph:
image: memgraph/memgraph-platform:2.1.0
container_name: memgraph_container
restart: unless-stopped
my_app:
image: memgraph_docker
container_name: something
restart: unless-stopped
command: python main.py
environment:
HOST: memgraph
PORT: 7687
Upvotes: 2