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.
root@remotecto /home/remotecto/www # docker run -it --link compose_mysql_1:mysql -v /home/remotecto/www/remotecto:/var/www/html hub.remotecto.com/wp-cli wp plugin install stops-core-theme-and-plugin-updates --activate docker: Error response from daemon: Cannot link to /compose_mysql_1, as it does not belong to the default network.
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
root@remotecto /home/remotecto/www # docker network ls NETWORK ID NAME DRIVER SCOPE 132b9df21601 bridge bridge local e58d477d8930 compose_default bridge local 2fbd2575b5bd host host local 1d2fb0535202 none null local
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.
root@remotecto /home/remotecto/www # docker run -it --link compose_mysql_1:mysql --net compose_default -v /home/remotecto/www/remotecto:/var/www/html hub.remotecto.com/wp-cli wp plugin install stops-core-theme-and-plugin-updates --activate Installing Easy Updates Manager (6.2.9) Downloading install package from https://downloads.wordpress.org/plugin/stops-core-theme-and-plugin-updates.6.2.9.zip... Unpacking the package... Installing the plugin... Plugin installed successfully. Activating 'stops-core-theme-and-plugin-updates'... Plugin 'stops-core-theme-and-plugin-updates' activated. Success: Installed 1 of 1 plugins.
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!
Thank you for the explanation and solution. finally, solved a 4-hour error