Docker-compose Not Linking Containers Properly On Docker For Mac

Posted on by  admin
Docker-compose Not Linking Containers Properly On Docker For Mac
  1. Docker-compose Not Linking Containers Properly On Docker For Mac Windows 10

I'm working with Docker 1.12.5 on macOS 10.12, and am setting up a development environment with with I have an application image, and a shared redis image which has some pre-populated configuration variables. Even after following a few tutorials (and reading about how docker0 isn't available on Mac) I'm struggling to connect the two containers. I start my redis image using: docker run -d -p 6379:6379 (IMAGE ID) In my redis image, I have: CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dffb89854618 d59 'docker-entrypoint.sh' 20 seconds ago Up 19 seconds 0.0.0.0:6379-6379/tcp drunkwilliams And from my Mac I can successfully connect via the redis-cli command without issue. Each container has its own localhost Each service runs in its own container. From the perspective of the Ubuntu container, redis is not listening on localhost. Use Docker networks To get your containers to communicate, they should be on the same Docker network.

This consists of three steps:. Create a Docker network.

The same concept is available in Docker Compose. However, it get’s a bit more complicated when it comes to linking a container not defined in the same compose file (docker-compose.yml). Linking containers without Docker Compose is easy using the –link parameter: docker run --name test1 something docker run --name test2 --link test1:test1. Instead, you should use the link name (by default, the name of the linked service) as the hostname to connect to. Should ea and maxis make a spore 2 for mac. See the docker-compose.yml documentation for details. And, in fact, I don’t see any environment variables being set when I link containers with docker-compose.

Give your containers names. Attach your containers to the network you created With this done, the containers can talk to each other using their names as if they were hostnames. There's more than one way to skin this cat. I will look at two in this answer, but there are probably a few other ways to do it that I am not familiar with (like using Kubernetes or Swarm, for instance).

Doing it by hand You can create a network for this application using docker network commands. # Show the current list of networks docker network ls # Create a network for your app docker network create myredisapp When you run the redis container, make sure it has a name, and is on this network. You can expose the ports externally (to macOS) if you want to (using -p), but that is not necessary just for other containers to talk to redis. Docker run -d -p 6379:6379 -name redisserver -network myredisapp Now run your Ubuntu container. You can name it as well if you like, but I won't bother in this example because this one isn't running any services. Docker run -it -network myredisapp ubuntu bash Now from inside the Ubuntu container, you should be able to reach redis using the name redisserver, as if it were a DNS name. Doing it using Compose I tend to build setups like this using Compose, because it's easier to write it into a YAML file (IMO).

Here's an example of the above, re-written in docker-compose.yml form: version: '2' services: redis: image: networks: - myredisapp ports: 6379:6379 ubuntu: image: ubuntu:latest networks: - myredisapp networks: myredisapp: driver: bridge With this in place, you can run docker-compose up -d redis and have your redis service online using a specific Docker network. Compose will create the network for you, if it doesn't already exist. It makes less sense to run the Ubuntu container that way.

Docker-compose Not Linking Containers Properly On Docker For Mac Windows 10

It is interactive, of course. But I assume once you have redis going, you will add some kind of application container, and perhaps a web proxy like nginx. Just put the others under services as well, and you can manage them all together. Since ubuntu is interactive, you can run it interactively: # without -d, container is run interactively docker-compose run ubuntu bash And now in Ubuntu, you should be able to connect to redis using its name, which in this example is simply redis.

Comments are closed.