我想编写一个简单的Python应用程序,并使用Dockerfile将其放入Docker容器中。我的Dockerfile如下:
FROM ubuntu:saucy
# Install required packages
RUN apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install python
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install mysql-server python-mysqldb
# Add our python app code to the image
RUN mkdir -p /app
ADD . /app
WORKDIR /app
# Set the default command to execute
CMD ["python", "main.py"]
在我的Python应用程序中,我只想连接到数据库。main.py看起来像这样:
import MySQLdb as db
connection = db.connect(
host='localhost',
port=3306,
user='root',
passwd='password',
)
当我使用以下命令构建docker镜像时:
docker build -t myapp .
使用以下命令运行Docker镜像:
docker run -i myapp
我遇到了错误:
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
问题是什么?