[英]docker-compose links between app and db
i have a problem regrading docker-compose links. 我有一个问题重新划分docker-compose链接。 I am trying to links between a ruby rack app and mysql, and resolve the db host name. 我试图在ruby rack app和mysql之间建立链接,并解析db主机名。
docker-compose.yml: 泊坞窗,compose.yml:
version: '2'
services:
db:
image: library/mysql:5.6.22
environment:
MYSQL_ROOT_PASSWORD: xxx
MYSQL_DATABASE: xxx
MYSQL_USER: xxx
MYSQL_PASSWORD: xxx
ports:
- "3306:3306"
restart: always
snow:
build: .
links:
- db
depends_on:
- db
ports:
- "9292:9292"
restart: always
Dockerfile of snow: Dockerfile的雪:
# Docker file for CentOS with RVM
FROM centos:centos7
MAINTAINER "XXXXX"
RUN useradd -ms /bin/bash t_user
RUN echo "t_user ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
WORKDIR "/home/t_user"
RUN yum -y install curl which tar sudo wget git vim mysql-devel
USER t_user
## Install RVM + ruby 2.1.2
RUN gpg2 --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
RUN curl -sSL https://get.rvm.io | bash -s stable
ENV PATH /usr/local/rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
RUN /bin/bash -l -c "rvm requirements"
RUN /bin/bash -l -c "rvm install ruby-2.1.2"
RUN /bin/bash -l -c "gem install bundler --no-ri --no-rdoc"
# Copy snow
USER t_user
WORKDIR /tmp
ADD Gemfile Gemfile
ADD Gemfile.lock Gemfile.lock
RUN /bin/bash -l -c "bundle"
WORKDIR "/home/t_user"
RUN mkdir -p snow/current/ && mkdir -p snow/shared/config/
COPY . /home/t_user/snow/current/
RUN rm /home/t_user/snow/current/Dockerfile
USER root
RUN chown -R t_user.t_user snow/
USER trusteer
WORKDIR /home/t_user/snow/current/
RUN /bin/bash -l -c "rake db:migrate"
RUN chmod +x ./bin/runner
ENTRYPOINT ["./bin/snow"]
EXPOSE 9292
the error i get after i try to run docker-compose up -d is: 我尝试运行docker-compose up -d后得到的错误是:
rake aborted!
Mysql2::Error: Unknown MySQL server host 'db' (2)
/home/trusteer/.rvm/gems/ruby-2.1.2/gems/mysql2-0.4.4/lib/mysql2/client.rb:87:in `connect'
/home/trusteer/.rvm/gems/ruby-2.1.2/gems/mysql2-0.4.4/lib/mysql2/client.rb:87:in `initialize'
/home/trusteer/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.6/lib/active_record/connection_adapters/mysql2_adapter.rb:18:in `new'
/home/trusteer/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.6/lib/active_record/connection_adapters/mysql2_adapter.rb:18:in `mysql2_connection'
/home/trusteer/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
/home/trusteer/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
/home/trusteer/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
/home/trusteer/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
/home/trusteer/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
/home/trusteer/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
/home/trusteer/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
/home/trusteer/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `retrieve_connection'
/home/trusteer/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
/home/trusteer/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:87:in `connection'
/home/trusteer/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.6/lib/active_record/migration.rb:916:in `initialize'
/home/trusteer/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.6/lib/active_record/migration.rb:823:in `new'
/home/trusteer/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.6/lib/active_record/migration.rb:823:in `up'
/home/trusteer/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.6/lib/active_record/migration.rb:801:in `migrate'
/home/trusteer/snow/current/Rakefile:10:in `block (2 levels) in <top (required)>'
/home/trusteer/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `eval'
/home/trusteer/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `<main>'
docker info: 码头信息:
Containers: 2
Running: 1
Paused: 0
Stopped: 1
Images: 72
Server Version: 1.11.0
Storage Driver: aufs
Root Dir: /var/lib/docker/aufs
Backing Filesystem: extfs
Dirs: 110
Dirperm1 Supported: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge null host
Kernel Version: 3.13.0-58-generic
Operating System: Ubuntu 14.04.4 LTS
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 7.507 GiB
Name: galt-tp
ID: DKCR:ZB6N:PW4O:76PF:UZPU:HVZO:XHL4:SRPM:6ACQ:M6T5:AZXN:KVPQ
Docker Root Dir: /var/lib/docker
Debug mode (client): false
Debug mode (server): false
Registry: https://index.docker.io/v1/
WARNING: No swap limit support
I've docker's documentation about links, and it says that the link directive add the DNS record of the relevant container, but it doesn't work for me. 我有docker关于链接的文档,它说链接指令添加了相关容器的DNS记录,但它对我不起作用。
any idea ? 任何的想法 ?
In docker-compose v2 you don't need links. 在docker-compose v2中,您不需要链接。 All containers will be created in one common network ( read more ). 所有容器都将在一个公共网络中创建( 阅读更多 )。 And with next configuration you can resolve services by their names: 使用下一个配置,您可以按名称解析服务:
version: '2'
services:
db:
image: library/mysql:5.6.22
environment:
MYSQL_ROOT_PASSWORD: xxx
MYSQL_DATABASE: xxx
MYSQL_USER: xxx
MYSQL_PASSWORD: xxx
ports:
- "3306:3306"
restart: always
snow:
build: .
depends_on:
- db
ports:
- "9292:9292"
restart: always
For example you can ping db container from snow: 例如,您可以从雪中ping db container:
ping db
Updated : 更新 :
You have problem with your snow dockerfile. 你的snow dockerfile有问题。 Сoncretely with next line: Сoncretely与下一行:
RUN /bin/bash -l -c "rake db:migrate"
You should understand that this command is executed on image build stage. 您应该了解此命令在映像构建阶段执行。 And on this stage you have no service with db
name. 在这个阶段,你没有db
name的服务。 db
service appears only after image snow was build. 只有在构建图像雪后才会显示db
服务。 depends_on
option doc says only about order of services start and nothing about build order. depends_on
选项doc只说服务的开始顺序,而不是构建顺序。
As solution you can create bash script with this command and execute it on you dockerfile entrypoint. 作为解决方案,您可以使用此命令创建bash脚本,并在dockerfile入口点上执行它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.