Docker Cannot link to a non running container

91,428

Solution 1

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.

Solution 2

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

Solution 3

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)

Solution 4

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"
Share:
91,428
kalelc
Author by

kalelc

Updated on August 21, 2021

Comments

  • kalelc
    kalelc over 2 years

    I need to create Rails and Mysql containers with docker-compose. When I try to create links between containers with docker-compose up, I get

    Cannot start container 9b271c58cf6aecaf017dadaf5b Cannot link to a non running container: /puma_db_1 AS /puma_web_1/db

    Files

    Dockerfile

    FROM ubuntu:14.04
    
    RUN apt-get -y update
    RUN apt-get -y install git curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev
    
    RUN apt-get -y install libmysqlclient-dev
    RUN git clone https://github.com/sstephenson/rbenv.git /root/.rbenv
    RUN git clone https://github.com/sstephenson/ruby-build.git /root/.rbenv/plugins/ruby-build
    RUN echo 'eval "$(rbenv init -)"' >> $HOME/.profile
    RUN echo 'eval "$(rbenv init -)"' >> $HOME/.bashrc
    
    RUN rbenv install 2.1.5
    RUN rbenv global 2.1.5
    RUN gem install rails -v 4.0.11
    
    ADD app.tar.gz /home/
    WORKDIR /home/app
    
    RUN bundle install
    EXPOSE 3000
    CMD ["rails", "server", "-b", "0.0.0.0"]
    

    docker-compose.yml

    db:  
      image: mysql:latest
      environment:
        MYSQL_DATABASE: app_development
        MYSQL_USER: mysql
        DATABASE_PASSWORD: onetwo
        ROOT_PASSWORD: onetwo
    web:
      build: .
      command: bundle exec rails s -p 3000 -b '0.0.0.0'
      ports:
        - "4000:3000"
      links:
        - db
    
  • Kryten
    Kryten over 7 years
    This worked for me too, probably because I had just installed docker-engine and had not restarted the server. Even though docker-engine seemed to be working, restarting it apparently fixed some problem.
  • Peter Ilfrich
    Peter Ilfrich about 7 years
    This is pointing in the right direction. Typically you'd see this if a linked container fails to start. Interestingly, the error message doesn't tell me that.
  • Ian
    Ian about 7 years
    I ended up doing docker-compose rm which also removes all the containers. I guess running docker rm CONTAINER_ID replacing the offending container id in there should also do the trick.
  • Aditya Singh
    Aditya Singh over 6 years
    Thanks a lot. This was a life saver.
  • L00_Cyph3r
    L00_Cyph3r over 2 years
    Are you sure that this answer is relevant to this old question? As I see that all the other answers are about missing network-connectivity and yours is about configuring Postgres.