MySQL Docker 无法链接到未运行的容器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32147554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:14:33  来源:igfitidea点击:

Docker Cannot link to a non running container

mysqlruby-on-railsdockerdocker-compose

提问by kalelc

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

我需要使用 docker-compose 创建 Rails 和 Mysql 容器。当我尝试在容器之间创建链接时docker-compose up,我得到

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

无法启动容器 9b271c58cf6aecaf017dadaf5b 无法链接到非运行容器:/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

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

回答by Thomasleveil

Most likely the dbcontainer fails to start.

很可能db容器无法启动。

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

通过仅启动db服务来确保它正常工作。您可以使用以下命令执行此操作:

docker-compose up db

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

如果在此命令后出现 MySQL 服务未运行,那么您就找到了问题的根源。

回答by Mike Graf

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

与 MySQL 没有特别的关系,但更多的是消息 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 (b2f21b869cccin my example above)

我发现依赖容器的 ID 与给定的 ID 不同(b2f21b869ccc在我上面的示例中)

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

只需运行即可解决 docker-compose up -d --force-recreate <service>

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

这导致它重新创建依赖项并将链接修复到正确的 docker id

回答by Ville Laitila

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

对我来说,它没有帮助运行 docker-compose up db。

This did the trick for me:

这对我有用:

sudo service docker restart

sudo service docker restart

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

然后继续 docker-compose up (-d)

回答by JorelC

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

您可以尝试 docker 网络的新功能,为此,您必须删除docker-compose.yml中的link参数,并使用.--x-networking option

docker-compose --x-networking up -d

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

为了防止 docker 为容器生成随机名称,这些名称被添加到每个容器各自网络的/etc/hosts文件中,您可以使用docker-compose.yml 中container_name:密钥

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"

回答by bgerd

Issue:

问题:

  • I have gotten this error whenever docker-composesuccessfully builds a set of Images, but one of those Imagesfails to run (e.g. launch into its own Container).

  • In this case, I suspect the Image, underlying your puma_db_1Container, is failing to run. You can find the name of this Imageby running docker ps -a. That said, its name is most likely puma_db

  • 每当docker-compose成功构建一组 时Images,我都会收到此错误,但其中一个Images无法运行(例如启动到它自己的Container)。

  • 在这种情况下,我怀疑Image您的基础 puma_db_1Container未能run。您可以Image通过运行找到它的名称docker ps -a。也就是说,它的名字很可能是puma_db

Solution:

解决方案:

  • To get at the cause, you can try docker-compose up <service_name>or docker-compose up db

  • Alternatively, I find the error message by running docker run <image_name>more useful. In this case, that would be docker run puma_db

  • 要找出原因,您可以尝试docker-compose up <service_name>docker-compose up db

  • 或者,我通过运行发现错误消息docker run <image_name>更有用。在这种情况下,那就是docker run puma_db

回答by bgerd

I had the same problem for mssql.link, as I am not using local database (rather using the one we have on staging), all I had to do is just comment that line out by editing Dockerfile script:

我遇到了同样的问题mssql.link,因为我没有使用本地数据库(而是使用我们在登台时使用的数据库),我所要做的就是通过编辑 Dockerfile 脚本来注释该行:

# DOCKER_ARGS="${DOCKER_ARGS} --link mssql-server-linux:mssql.link"

This solution may help someone or may be no one, but it sorted it for me :)

这个解决方案可能对某人有帮助,也可能对某人没有帮助,但它为我整理了它:)

回答by sapy

If you started the container lets say Xwith a link --link keen_visvesvarayaand then once Xis up the linked container was stopped, but Xkept running . Now if you try to docker execinto Xyou get this error.

如果您lets say X使用链接启动容器,--link keen_visvesvaraya然后一旦X启动,链接的容器就会停止,但会X继续运行。现在,如果您尝试docker exec进入,X则会收到此错误。

Yah solution is to restart.

解决方法是重启呀。

回答by Moccine

I had the same problem with elasticsearch - symfony - and docker

我对 elasticsearch - symfony - 和 docker 有同样的问题

Can not link to a non-running container:/43c1d3b410db_myindex_elasticsearch_1 AS /myindex_apache_1/elasticsearch

the solution is to delete the content of the data volume

解决办法是删除数据卷的内容

? elasticsearch: image: docker.elastic.co/elasticsearch/elasticsearch:5.5.2 Volumes: ???? - ./docker/volume/elasticsearch: /usr/share/elasticsearch/dataand run docker-composer up -dagain.

? elasticsearch: image: docker.elastic.co/elasticsearch/elasticsearch:5.5.2 Volumes: ???? - ./docker/volume/elasticsearch: /usr/share/elasticsearch/datadocker-composer up -d再次运行。

回答by Pramod Kumar Gupta

You can use below command Because It's working for me

您可以使用以下命令,因为它对我有用

docker run --name standlone-mysql -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=test -e MYSQL_USER=root -e MYSQL_ROOT_PASSWORD=root -d mysql:5.6