davidhaskins
davidhaskins

Reputation: 280

Can't get traffic into docker container

I'm trying to run a webserver in docker at http://localhost:5000 and everything I have read says to add "EXPOSE 5000" to my dockerfile and the ports to my docker-compose file.

I know the webserver is running, because I can connect using lynx inside the container and going to http://localhost:5000. Within the container, everything works fine.

When I try to access it from outside the container on the host system, I ran tcpdump and saw no traffic getting into the container.

docker-compose.yml:

version: '3'
services:
  web:
    build: .
    ports:
     - "5000:5000"
    volumes:
     - ./code:/code

Dockerfile:

FROM scratch
ADD centos-7-docker.tar.xz /

LABEL org.label-schema.schema-version="1.0" \
    org.label-schema.name="CentOS Base Image" \
    org.label-schema.vendor="CentOS" \
    org.label-schema.license="GPLv2" \
    org.label-schema.build-date="20180804"

RUN yum clean all
RUN yum -y update

RUN yum install -y iputils gcc vim wget yum-utils groupinstall development lynx

#install Python 3.6
RUN yum install https://centos7.iuscommunity.org/ius-release.rpm -y
RUN yum install python36u -y
RUN yum install python36u-pip python36u-devel -y
RUN pip3.6 install --upgrade pip
#now you can run python as "python3.6 some_file.py" and pip as "pip3.6 <stuff>"


#install ms sql odbc driver for connecting to SQL Server
RUN curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo
RUN ACCEPT_EULA=Y yum install msodbcsql17 -y
# optional: for bcp and sqlcmd in /opt/mssql-tools/bin
RUN ACCEPT_EULA=Y yum install mssql-tools -y
# optional: for unixODBC development headers
RUN yum install unixODBC-devel -y

#install python's odbc driver
RUN yum install gcc-c++ -y
RUN pip3.6 install pyodbc

#mount volumes
ADD . /code
WORKDIR /code
EXPOSE 5000

#install Flask and other dependencies (must come after "/code" dir created)
RUN pip3.6 install -r /code/requirements.txt

#execute file
CMD python3.6 /code/app.py

app.py that I am trying to run:

import time
#import redis
import pyodbc
from flask import Flask

app = Flask(__name__)
#cache = redis.Redis(host='redis', port=6379)

def get_hit_count():
    retries = 5
    while True:
        try:
            return cache.incr('hits')
        except redis.exceptions.ConnectionError as exc:
            if retries == 0:
                raise exc
            retries -= 1
            time.sleep(0.5)

@app.route('/')
def hello():
    #count = get_hit_count()
    server = '123.123.123.123' #I changed these for posting to SO
    username = 'usernameForMyApplication'
    password = 'passwordForMyApplication'
    cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';PORT=1443;UID='+username+';PWD='+ password)
    cursor = cnxn.cursor()
    print ('Using the following SQL Server version:')
    tsql = "SELECT @@version;"
    with cursor.execute(tsql):
        row = cursor.fetchone()
        version = (str(row[0]))

    return 'version {} \n'.format(version)

if __name__ == "__main__":
    app.run(host="127.0.0.1", debug=True)

How can I reach the webserver from outside the host container?

I should add that the example ( https://docs.docker.com/compose/gettingstarted/#step-2-create-a-dockerfile ) works on my computer, so I don't think it is a configuration issue with my Windows 10 host.

Upvotes: 0

Views: 1414

Answers (2)

Rafael Quintela
Rafael Quintela

Reputation: 2228

try changing your IP address

if __name__ == "__main__":
    app.run(host="0.0.0.0", port="5000", debug=True)

Upvotes: 1

Jason
Jason

Reputation: 475

You are binding your flask server to the localhost inside of the container. Change the 127.0.0.1 to 0.0.0.0 and that should fix things.

Upvotes: 2

Related Questions