I have a memcached service running on port 11211 in a docker container and I want to access this service from within another container using 127.0.0.1:11211. Im using docker-compose and nothing of “links”, “ports” or “expose” seems to work for me. I don’t wanna have to access with the ip of the memcached docker container instead I want to access it as it were a local service of the other container. Is there any solution?
Thanks!
version: '2'
services:
memcached:
build: ./memcached
image: memcached_img
expose:
- "11211"
web:
build: .
image: app:latest
mem_limit: 512m
ports:
- "3000:3000"
command: [ "script/startup.sh", "web" ]
worker:
build: .
image: app:latest
mem_limit: 512m
command: [ "script/startup.sh", "worker" ]
4
Answers
You can whether use ‘expose XXXX’ in Dockerfile, or use -p when running the container for the 1st time.
It will be super helpful for us to help you if you provide your Dockerfile that built your image from.
First, you need to change Memcache conf to allow to connect with other hosts
build image.
in your docker-compose file
localhost inside the container is a different isolated network. Not the host’s localhost.
You could add
depends_on: memcached
to the web container. This will add thememcached
host to theweb
container and you will be able to ping that host from there. Docker will take care of port forwarding. It will also make sure to start web only once memcached has already started. You could thentelnet memcached 11211
from withinweb
.Create docker network to communicate between containers
Now you can “access” services using their names. E.g. you can access memcached service from web service using
memcached:11211
as host:port