docker-撰写在 /etc/hosts 中生成重复条目

docker-compose generating duplicate entries in /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-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前缀

它们来自 pre-1.0 docker-compose,其中多个数据库实例以 _1_2 模式命名。

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

仍然有为每个容器实例添加 _x 的别名,这可能是一个问题 (Issue 472: Hostnames with underscore fails in ruby URI validation

当前答案是:

You can use the name of the service in the docker-compose.yml as the hostname. It doesn't contain any underscores.
You can also add an alias to your link to the container, which should allow you to access it as just the alias.

In the 1.3 release of compose there should be support for naming your container as anything you want, which will make this more obvious.