1

我有一个相当简单的 docker-compose.yml:

db:
  build: docker/db
  env_file:
    - .env
  ports:
    - "5432"

web:
  build: .
  env_file:
    - .env
  volumes:
    - .:/home/app/emerson
  ports:
    - "80:80"
  links:
    - db

Web 容器启动一个 rails 应用程序。一切都很顺利,但有一件事让我感到困惑。查看 Web 容器上的 /etc/hosts 内部,我看到以下条目:

172.17.0.10     db_1
172.17.0.10     emerson_db_1
172.17.0.10     db

我希望db,因为这是我要链接到Web容器的容器,但是其他人是从哪里来的?仅供参考,这是docker-compose up的输出:

Creating emerson_db_1...
Creating emerson_web_1...
Attaching to emerson_db_1, emerson_web_1
db_1  | The files belonging to this database system will be owned by user "postgres".
db_1  | This user must also own the server process.
db_1  | 
db_1  | The database cluster will be initialized with locale "en_US.utf8".
db_1  | The default database encoding has accordingly been set to "UTF8".
db_1  | The default text search configuration will be set to "english".
db_1  | 
db_1  | Data page checksums are disabled.
db_1  | 
db_1  | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1  | creating subdirectories ... ok
db_1  | selecting default max_connections ... 100
db_1  | selecting default shared_buffers ... 128MB
db_1  | selecting dynamic shared memory implementation ... posix
db_1  | creating configuration files ... ok
web_1 | *** Running /etc/my_init.d/00_configure_nginx.sh...
web_1 | *** Running /etc/my_init.d/00_regen_ssh_host_keys.sh...
web_1 | No SSH host key available. Generating one...
db_1  | ok
db_1  | initializing pg_authid ... ok
web_1 | Creating SSH2 RSA key; this may take some time ...
db_1  | initializing dependencies ... ok
web_1 | Creating SSH2 DSA key; this may take some time ...
web_1 | Creating SSH2 ECDSA key; this may take some time ...
web_1 | Creating SSH2 ED25519 key; this may take some time ...
db_1  | creating system views ... ok
db_1  | loading system objects' descriptions ... ok
db_1  | creating collations ... ok
db_1  | creating conversions ... ok
db_1  | creating dictionaries ... ok
db_1  | setting privileges on built-in objects ... ok
web_1 | invoke-rc.d: policy-rc.d denied execution of restart.
db_1  | creating information schema ... ok
web_1 | *** Running /etc/my_init.d/30_presetup_nginx.sh...
web_1 | *** Running /etc/rc.local...
db_1  | loading PL/pgSQL server-side language ... ok
web_1 | *** Booting runit daemon...
web_1 | *** Runit started as PID 98
db_1  | vacuuming database template1 ... ok
db_1  | copying template1 to template0 ... ok
db_1  | copying template1 to postgres ... ok
web_1 | Apr 24 02:44:26 1d3b7bb27612 syslog-ng[105]: syslog-ng starting up; version='3.5.3'
db_1  | syncing data to disk ... ok
db_1  | 
db_1  | WARNING: enabling "trust" authentication for local connections
db_1  | You can change this by editing pg_hba.conf or using the option -A, or
db_1  | --auth-local and --auth-host, the next time you run initdb.
db_1  | 
db_1  | Success. You can now start the database server using:
db_1  | 
db_1  |     postgres -D /var/lib/postgresql/data
db_1  | or
db_1  |     pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1  | 
db_1  | ****************************************************
db_1  | WARNING: No password has been set for the database.
db_1  |          This will allow anyone with access to the
db_1  |          Postgres port to access your database. In
db_1  |          Docker's default configuration, this is
db_1  |          effectively any other container on the same
db_1  |          system.
db_1  |          
db_1  |          Use "-e POSTGRES_PASSWORD=password" to set
db_1  |          it in "docker run".
db_1  | ****************************************************
db_1  | 
db_1  | PostgreSQL stand-alone backend 9.4.1
db_1  | backend> statement: ALTER USER "postgres" WITH SUPERUSER  ;
db_1  | 
web_1 | ok: run: /etc/service/nginx-log-forwarder: (pid 118) 0s
db_1  | backend> 
db_1  | No PostgreSQL clusters exist; see "man pg_createcluster" ... (warning).
db_1  | 
db_1  | backend> *******************************************
db_1  | LOG:  database system was shut down at 2015-04-24 02:44:28 UTC
db_1  | LOG:  database system is ready to accept connections
db_1  | LOG:  autovacuum launcher started
web_1 | [ 2015-04-24 02:44:27.9386 119/7f4c07f13780 agents/Watchdog/Main.cpp:538 ]: Options: { 'analytics_log_user' => 'nobody', 'default_group' => 'nogroup', 'default_python' => 'python', 'default_ruby' => '/usr/bin/ruby', 'default_user' => 'nobody', 'log_level' => '0', 'max_pool_size' => '6', 'passenger_root' => '/usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini', 'passenger_version' => '4.0.58', 'pool_idle_time' => '300', 'temp_dir' => '/tmp', 'union_station_gateway_address' => 'gateway.unionstationapp.com', 'union_station_gateway_port' => '443', 'user_switching' => 'true', 'web_server_passenger_version' => '4.0.58', 'web_server_pid' => '107', 'web_server_type' => 'nginx', 'web_server_worker_gid' => '33', 'web_server_worker_uid' => '33' }
web_1 | [ 2015-04-24 02:44:27.0007 122/7f0c3eb9a780 agents/HelperAgent/Main.cpp:650 ]: PassengerHelperAgent online, listening at unix:/tmp/passenger.1.0.107/generation-0/request
web_1 | [ 2015-04-24 02:44:28.1065 127/7f5e5b4377c0 agents/LoggingAgent/Main.cpp:321 ]: PassengerLoggingAgent online, listening at unix:/tmp/passenger.1.0.107/generation-0/logging
web_1 | [ 2015-04-24 02:44:28.1072 119/7f4c07f13780 agents/Watchdog/Main.cpp:728 ]: All Phusion Passenger agents started!

但是只有两个容器docker ps -a输出:

CONTAINER ID        IMAGE                COMMAND                CREATED             STATUS              PORTS                         NAMES
1d3b7bb27612        emerson_web:latest   "/sbin/my_init"        About an hour ago   Up About an hour    443/tcp, 0.0.0.0:80->80/tcp   emerson_web_1       
0c047c3ce103        emerson_db:latest    "/docker-entrypoint.   About an hour ago   Up About an hour    0.0.0.0:49156->5432/tcp       emerson_db_1       

此外,我还看到 web 容器中重复的环境变量,对应 db、db_1 和 emerson_db_1 前缀。

4

1 回答 1

2

它们来自 1.0 之前的 docker-compose,其中多个数据库实例以_1,_2模式命名。

PR 364引入了链接名称(默认情况下,链接服务的名称)作为要连接的主机名,而不是使用环境变量。

仍然_x为每个容器实例添加了别名,这可能是一个问题(问题 472:带有下划线的主机名在 ruby​​ URI 验证中失败

目前的答案是:

您可以使用中的服务名称docker-compose.yml作为主机名。它不包含任何下划线。
您还可以为指向容器的链接添加别名,这应该允许您仅以别名的身份访问它。

在 compose 的 1.3 版本中,应该支持将容器命名为您想要的任何名称,这将使这一点更加明显。

于 2015-04-24T06:57:00.543 回答