如何从主机上的浏览器连接到docker容器?

时间:2016-07-16 11:06:45

标签: networking docker docker-compose

我正在使用Docker for Mac 1.12.0-rc4-beta19 使用以下dockerfile和docker-compose.yml构建的容器。

我想用http://localhost:1344从主机操作系统浏览器连接到容器的1344端口 但连接失败了。

我正在使用容器的1344来测试瓶子(python轻量级Web框架)应用程序

为什么无法从主机连接到容器的端口?

docker-compose.yml:

    version: '2'
    services:
        datastore:
            image: busybox:latest
            volumes:
                - ./share:/share_to_container

        ### base (ubuntu)
        base:
            build: ./
            ports:
                - "127.0.0.1:1344:1344"
                - "8000:8000"
            volumes:
                - ./app:/app
            volumes_from:
                - datastore
            links:
                - db
                - webserver


        db:
            build: 
                context: .
                dockerfile: "mysqlfile"
            environment:
                - MYSQL_ROOT_PASSWORD=mypassword
            ports:
                - "3306:3306"
            volumes:
                - ./mysql:/mysql
            volumes_from:
                - datastore

        webserver:
            image: nginx
            ports:
                - "8080:80"
            volumes:
                - ./nginx/mysite.template:/etc/nginx/conf.d/mysite.template
            volumes_from:
                - datastore

编辑:

端口8080连接正确,但1344失败

以下是基本服务的完整Dockerfile

Dockerfile_for_base:

from ubuntu:latest
maintainer myname

run mkdir ~/app

copy vim /root/.vim
copy vimrc /root/.vimrc
#update
run apt-get update

run apt-get -y update
run apt-get -y install libssl-dev
run apt-get -yf install curl 
run apt-get -y install mysql-client
run apt-get -y install clang 
run apt-get -y install lldb 
run apt-get -y install make 
run apt-get -y install libsqlite3-dev
run apt-get -y install man 
run apt-get -y install vim 
run apt-get -y install git 
run apt-get -y install pkg-config 
run apt-get -y install zip 
run apt-get -y install unzip
run apt-get -y install language-pack-ja-base
run apt-get -y install language-pack-ja
run apt-get -y install language-pack-en-base
run apt-get -y install language-pack-en
run apt-get -y install fcitx-mozc
run apt-get -y install libreadline-dev

# setting locale to japanese

run update-locale LANG=ja_JP.UTF-8 LANGUAGE=ja_JP:ja
env LANG ja_JP.UTF-8
env LC_CTYPE ja_JP.UTF-8
env LC_MESSAGES en_US.UTF-8
run im-config -n fcitx

# end of locale settings

# install latest python3 and some python packages (https://github.com/docker-library/python/blob/3db904b3f5407840e591daf3aa54670a685b22b3/3.5/Dockerfile)

    ENV GPG_KEY 97FC712E4C024BBEA48A61ED3A5CA953F73C700D

    ENV PYTHON_VERSION 3.5.2

    # if this is called "PIP_VERSION", pip explodes with "ValueError: invalid truth value '<VERSION>'"
    ENV PYTHON_PIP_VERSION 8.1.2

    RUN set -ex \
        && curl -fSL "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz" -o python.tar.xz \
        && curl -fSL "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz.asc" -o python.tar.xz.asc \
        && export GNUPGHOME="$(mktemp -d)" \
        && gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$GPG_KEY" \
        && gpg --batch --verify python.tar.xz.asc python.tar.xz \
        && rm -r "$GNUPGHOME" python.tar.xz.asc \
        && mkdir -p /usr/src/python \
        && tar -xJC /usr/src/python --strip-components=1 -f python.tar.xz \
        && rm python.tar.xz \
        \
        && cd /usr/src/python \
        && ./configure \
            --enable-loadable-sqlite-extensions \
            --enable-shared \
        && make -j$(nproc) \
        && make install \
        && ldconfig \
        && pip3 install --no-cache-dir --upgrade pip==$PYTHON_PIP_VERSION \
        && [ "$(pip list | awk -F '[ ()]+' '$1 == "pip" { print $2; exit }')" = "$PYTHON_PIP_VERSION" ] \
        && find /usr/local -depth \
            \( \
                \( -type d -a -name test -o -name tests \) \
                -o \
                \( -type f -a -name '*.pyc' -o -name '*.pyo' \) \
            \) -exec rm -rf '{}' + \
        && rm -rf /usr/src/python ~/.cache

    # make some useful symlinks that are expected to exist
    RUN cd /usr/local/bin \
        && ln -s easy_install-3.5 easy_install \
        && ln -s idle3 idle \
        && ln -s pydoc3 pydoc \
        && ln -s python3 python \
    && ln -s python3-config python-config

    # end of latest python installation

    #install some packages

    run pip --no-cache-dir install bottle
    run pip --no-cache-dir install feedparser
    run pip --no-cache-dir install PyMySQL
    run pip --no-cache-dir install -U pip
    run pip --no-cache-dir install -U setuptools


#prompt and compiler environment variables
env CC clang
env CXX clang++
run echo 'export PS1="\h:\W \u$ "' >> ~/.bashrc

# git config
run git config --global user.name "myusername"
run git config --global user.email "my@email.address"
run git config --global color.ui true
run git config --global core.editor vim

expose 1000
expose 2000
expose 3000
expose 4000
expose 5000

expose 1344


cmd bash

3 个答案:

答案 0 :(得分:1)

如果通过&#34;主机浏览器&#34;你的意思是你的Mac,你当然需要按照建议从端口映射中删除主机。您无法连接的原因是实际的Docker主机是在Mac和Docker之间运行的(xhyve)虚拟机。 Docker会自动发布Mac和容器之间的端口,只需删除主机,即- "1344:1344"

(Fyi,在您的设置中,您需要通过VM主机进行连接,这对您来说并不是很有帮助。)

如果您仍有问题,请发布任何错误和重现步骤。

答案 1 :(得分:0)

因此,在您发布Dockerfile后,它看起来不像您正在运行任何东西?您将CMD操作设置为bash,并且不会覆盖docker-compose.yml中的操作。我对容器完全有点惊讶(因为它只会运行bash并退出)。

这些文件是否完整?

另外,您可能希望重新格式化/ lint您的Dockerfile以获得最佳实践。

答案 2 :(得分:0)

您看到了this forum topic吗?

所以只需运行一个容器并调用'ifconfig'命令。输出示例:

bash-4.3# ifconfig
eth0  Link encap:Ethernet  HWaddr 02:42:AC:11:00:02
      inet addr:172.17.0.2  Bcast:0.0.0.0  Mask:255.255.0.0
      inet6 addr: fe80::42:acff:fe11:2%32738/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:6 errors:0 dropped:0 overruns:0 frame:0
      TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0
      RX bytes:508 (508.0 B)  TX bytes:508 (508.0 B)

lo    Link encap:Local Loopback
      inet addr:127.0.0.1  Mask:255.0.0.0
      inet6 addr: ::1%32738/128 Scope:Host
      UP LOOPBACK RUNNING  MTU:65536  Metric:1
      RX packets:0 errors:0 dropped:0 overruns:0 frame:0
      TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1
      RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

因此,来自示例的VM的IP是172.17.0.2