Docker Cannot link to a non running container

Most likely the db container fails to start.

Make sure it works fine by starting only the db service. You can do that with the following command:

docker-compose up db

If it appears the MySQL service is not running after this command, then you found the origin of your problem.


You might try out the new features of docker networking, To do this, You must remove the link parameter in your docker-compose.yml , and initialize the container with the --x-networking option.

docker-compose --x-networking up -d

To prevent docker generate random names for the containers, which are added to the /etc/hosts file of the respective network for every container, you can use the container_name: key in the docker-compose.yml

db:  
  container_name: db
  image: mysql:latest
  environment:
    MYSQL_DATABASE: app_development
    MYSQL_USER: mysql
    DATABASE_PASSWORD: onetwo
    ROOT_PASSWORD: onetwo
web:
  container_name: web
  build: .
  command: bundle exec rails s -p 3000 -b '0.0.0.0'
  ports:
    - "4000:3000"

For me, it did not help running docker-compose up db.

This did the trick for me:

sudo service docker restart

and then continuing with docker-compose up (-d)


Not specifically related to MySQL but more the message ERROR: for <service> Cannot link to a non running container: /b2f21b869ccc_<dependency>_1 AS /<service>_1/<dependency>_1

I found that the dependency container had a different id than the one given (b2f21b869ccc in my example above)

Solved simply by running docker-compose up -d --force-recreate <service>

which caused it to recreate the dependency and fix the link to the correct docker id