docker-compose存在网络问题

时间:2019-01-23 16:45:29

标签: docker docker-compose

我正在尝试使用以下配置:

docker-compose.yml

rules: [
      {
        exclude: [
          /\.html$/,
          /\.(js|jsx|ts|tsx)$/,
          /\.css$/,
          /\.json$/,
          /\.bmp$/,
          /\.gif$/,
          /\.jpe?g$/,
          /\.png$/,
          /\.scss$/,
        ],
        loader: require.resolve('file-loader'),
        options: {
          name: 'static/media/[name].[hash:8].[ext]',
        },
      },

      {
        test: /\.(png|woff|woff2|eot|ttf|svg)$/,
        loader: 'url-loader?limit=100000',
      },
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader',
              options: {
                modules: true,
                localIdentName: '[name]__[local]___[hash:base64:5]',
              },
            },
            'postcss-loader',
          ],
        }),
      },
      {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader',
              options: {
                modules: true,
                sourceMap: true,
                importLoaders: 1,
                localIdentName: '[name]__[local]___[hash:base64:5]',
              },
            },
            'sass-loader',
          ],
        }),
      },
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: ['babel-loader'],
      },
      {
        test: /\.(ts|tsx)$/,
        exclude: /node_modules/,
        loader: 'ts-loader',
      },
      {
        test: /\.(ttf|eot|woff|woff2)$/,
        use: {
          loader: 'file-loader',
          options: {
            name: 'fonts/[name].[ext]',
          },
        },
      },
    }]

另一个文件是Dockerfile:

version: "3"
services:
  web:
    build: .
    ports:
      - "3000:3000"
    depends_on:
      - db
  db:
    image: onjin/alpine-postgres
    environment:
      POSTGRES_PASSWORD: password

我不知道为什么在运行“ docker-compose up”时总是出现此错误:

  

psql:无法将主机名“ db”转换为地址:名称不正确   解决

有人可以帮助我进行调试吗?似乎“ db”主机名未在docker环境中传播,但不知道原因。

1 个答案:

答案 0 :(得分:1)

您看到的问题与 docker-composer 以与yaml文件中定义的服务相同的顺序运行服务有关。因此,基本上,当您运行您的 web 服务 db 服务时,该服务尚不存在,因此其主机名不可解析。

如果您在docker-compose.yaml中更改顺序:

version: "2"

services:
   db:
    image: onjin/alpine-postgres
    environment:
      POSTGRES_PASSWORD: password 
   web:
    build: .
    ports:
      - "3000:3000"
    depends_on:
      - "db"
    tty: true

并运行 docker-compose up -d ,您将不再看到错误,服务将启动:

sudo docker-compose ps
    Name                   Command               State           Ports          
-------------------------------------------------------------------------------
db_1    /docker-entrypoint.sh postgres   Up      5432/tcp               
web_1   /bin/sh                          Up      0.0.0.0:3000->3000/tcp 

并且主机名是正确可解析的:

sudo docker-compose run web "ping" "db"
PING db (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: seq=0 ttl=64 time=0.096 ms
64 bytes from 172.18.0.2: seq=1 ttl=64 time=0.101 ms
64 bytes from 172.18.0.2: seq=2 ttl=64 time=0.097 ms
64 bytes from 172.18.0.2: seq=3 ttl=64 time=0.106 ms