docker-compose在/ etc / hosts中生成重复的条目

我有一个相当简单的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的输出– 组成

 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容器中看到重复的环境variables,对应于db,db_1和emerson_db_1前缀。

他们来自1.0以前的docker-compose,其中多个db实例以_1_2模式命名。

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

对于每个容器实例,仍然会添加_x别名,这可能是一个问题( 问题472:带有下划线的主机名在ruby URIvalidation中失败

目前的答案是:

您可以使用docker-compose.yml中的服务名称作为主机名。 它不包含任何下划线。
你也可以添加一个别名到你的链接到容器,这应该允许你访问它只是别名。

在1.3的发布版本中,应该支持将你的容器命名为任何你想要的东西,这会使这个更加明显。