docker: Error response from daemon: Cannot link to /compose_mysql_1, as it does not belong to the default network.

0
826

I’ve been doing a lot of work with Docker recently, infact this website, Remote CTO, is currently running in a Docker container, well 3, one for Nginx, one for PHP-FPM and one for MySQL. Soon there might be a fourth, for either Redis or Memcache, I haven’t decided yet!

I do a lot of cool stuff at the command line. e.g using WP-CLI to manage my WordPress installations.

docker: Error response from daemon: Cannot link to /compose_mysql_1, as it does not belong to the default network.

As you can see I’m manipulating a Docker container that’s being managed through docker-compose with the docker command line tool, and it’s throwing an error about not being able to talk to my MySQL instance even though I’ve linked it.

The fix for this error is really simple, basically docker supports multiple networks, and for security and manageability docker-compose like’s to run it’s containers on it’s own network rather than Docker’s default network. All we need to do is to tell docker to use docker-composer’s network instead.

Finding Docker Compose’ Network

We can list all the networks with the command docker network ls

In this instance, docker-compose created a nicely named network, compose_default and we can simply pass that to the docker command using the –net flags.

And that is how you solve docker: Error response from daemon: Cannot link to /compose_mysql_1, as it does not belong to the default network.

Simply specify the correct Docker network when performing the action!

Docker-Compose Network Naming Convention

When using docker-compose your network name is decided based on the docker-compose “project name”, which is based on the name of the directory it lives in. You can override the project name with either the –project-name flag or the COMPOSE_PROJECT_NAME environment variable. My docker-compose.yml file lives in a directory called compose which is why docker-compose picked compose-default when creating the running container instances!

SHARE
Previous articleWhy I’m Switching From Google To Bing
Next articlegit push matching vs simple
Hi, I'm Rus and after 10 years of Linux administration and software development, in September 2014 I quit my day job, sold all my stuff and moved to Thailand to live the good life. My new website, http://www.ruspow.com shows how I'm making money online as well as what life can be like when you're not sat behind a desk and are free to make your own decisions!