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

4
我正在使用Docker for Mac 1.12.0-rc4-beta19。
以下是使用的dockerfile和docker-compose.yml文件构建的容器。
我想通过http://localhost:1344连接到容器的1344端口,但连接失败了。
我使用容器中的1344端口测试bottle(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

你能否发布你的整个docker-compose.yml文件?此外,我认为在端口部分不需要包含主机。 端口: - “1344:1344” - granthbr
@granthbr 添加整个它。 - KiYugadgeter
@JHarris 容器已经启动: b59ab33d546f devenv_base "bash" 4小时前 已运行4小时 - KiYugadgeter
请问您能否发布该服务的Dockerfile文件? - johnharris85
你确定容器中的应用程序正在监听 0.0.0.0 吗? - dnephin
显示剩余3条评论
3个回答

1
如果你所说的“host os browser”是指你的Mac,那么你需要按照建议从端口映射中删除主机。你无法连接的原因是实际的Docker主机是在你的Mac和Docker之间运行的(xhyve)虚拟机。Docker会自动在你的Mac和容器之间发布端口,就像你设置的那样,只需删除主机,即- "1344:1344"。(顺便说一下,在你现有的设置中,你需要通过VM主机连接,这并没有真正帮助你。)如果你仍然遇到问题,请发布任何错误和重现步骤。

我尝试了- "1344:1344",但是当我访问localhost:1344时,出现了相同的错误。 - KiYugadgeter

0

所以,你发布了Dockerfile之后,看起来你没有运行任何东西?你将CMD操作设置为bash,并且在docker-compose.yml中没有覆盖它。我有点惊讶容器仍然在运行(因为它只会运行bash并退出)。

这些文件完整吗?

另外,您可能希望重新格式化/清理您的Dockerfile以符合最佳实践。


0

你看到了这个论坛话题吗?

所以只需运行一个容器并调用ˋ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)

所以,虚拟机的IP地址示例为172.17.0.2。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接