-1

I have simple docker file for micro app

FROM python:3.8.16-slim

RUN apt-get update &&  DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends make build-essential libssl-dev  \
    zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev \
    libxmlsec1-dev libffi-dev liblzma-dev git ca-certificates

RUN python -m pip install --upgrade pip setuptools
COPY requirements.txt requirements.txt

RUN pip3 install -r requirements.txt
RUN python3 -m spacy download xx_ent_wiki_sm

COPY app /app
WORKDIR /app

ENTRYPOINT ["bash", "run.sh"]

run.sh just calling python script that connect to localhost mysql

db = mysql.connector.connect(
host=os.environ['host'], 
user=os.environ['user'],
passwd=os.environ['passwd'], 
database=os.environ['database'])

the .env file looks like

host=localhost
user=test
passwd=test
database=semantic_textual_similarity

I build Dockerfile using docker build -t semantic_textual_similarity:semantic_textual_similarity .

and tried to run it using

docker run --env-file=.env -p 3306:3306 semantic_textual_similarity:semantic_textual_similarity

and got following error

mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306'

What should i pass to Docker run to make it works ?

Alex Nikitin
  • 841
  • 1
  • 10
  • 29
  • Are you sure about `passwd=os.environ['host']`? I guess you mean `passwd=os.environ['passwd']` – buran Mar 25 '23 at 10:27
  • 1
    `localhost` in this context is the container running the Python application; the database will be somewhere else. Where is the database actually running? (Outside a container on the host, in another container?) – David Maze Mar 25 '23 at 10:42
  • You don't install mysql in the docker image. This means `localhost` cannot work. – Olaf Dietsche Mar 25 '23 at 14:15
  • @DavidMaze the actual db is on the host on my laptop – Alex Nikitin Mar 26 '23 at 15:01

1 Answers1

0

some suggestions:

1.check mysql is running in host
2.change the run.sh file change host to actual docker container name
    host='docker_container_name',
3. then, try to run Docker run again,
lastber
  • 1
  • 3