简体   繁体   中英

How to connect to docker container from browser on host?

I am using Docker for Mac 1.12.0-rc4-beta19.
The container built with following dockerfile and docker-compose.yml.

I want to connect to 1344 port of container from host os browser with http://localhost:1344 .
But connection be fail.

I am using 1344 of container to test of bottle (python lightweight web framework) application

Why cannot connect to container's port from host?

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

Edit:

The port 8080 connection is correctly, But 1344 is fail

The following is full Dockerfile for base service

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

If by "host os browser" you mean your Mac, you certainly need to remove the host from the port mapping as suggested. The reason you can't connect is the actual Docker host is a (xhyve) Virtual Machine running between your Mac and Docker. Docker will automatically publish the port between your Mac and the container like you have it, just remove the host, ie, - "1344:1344"

(Fyi, in your setup as-is you would need to connect via the VM host which doesn't really help you.)

If you still have problems, post any errors and steps to reproduce.

So, after you posted your Dockerfile, it doesn't look like you're running anything? You have the CMD action set to bash and are not overriding it in your docker-compose.yml . I'm a little surprised the container is up at all (since it would just run bash and exit).

Are these files complete?

As an aside, you may want to reformat / lint your Dockerfile for best practises.

Did you see this forum topic ?

So just run a container and call ˋifconfigˋ command. Example of output:

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)

So IP of VM from example is 172.17.0.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM