首页 > 解决方案 > Docker:避免必须运行所有容器并且必须再次构建映像

问题描述

当我想对在 Docker 容器中运行的 python 文件进行编辑时,我必须重新运行大多数 Docker 设置命令才能在 Docker 中考虑这些更改。当我进行编辑、保存甚至调用时

docker kill link_api 
docker rm link_api 
docker run -dit --restart unless-stopped --name=link_api -e FLASK_APP=database.py -p 5000:5000 --
network my-network link_api_image
docker logs -f link_api

同样的错误仍然出现。此外,这会导致相同的错误:docker container restart link_api

因此,我需要杀死、rm 两个容器并再次从我的初始例程开始:

# Only the very first time, afterwards skip:
docker network create my-network
# Run mysql container andadd it to my-network
docker run -dit --restart unless-stopped --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw  -e MYSQL_DATABASE=demo -v ~/Desktop/new_demo_db_folder:/var/lib/mysql --network my-network -dit mysql:latest --default-authentication-plugin=mysql_native_password
docker build -t link_api_image .
docker run -dit --restart unless-stopped --name=link_api -e FLASK_APP=database.py -p 5000:5000 --network my-network link_api_image
docker logs -f link_api
#Open another terminal
docker exec -it some-mysql bash 
# Now we are inside the mysql container in a separate process. Let's run the mysql client app so that we can execute SQL queries 
mysql -uroot -p 
#Enter Passwort

现在,我可以重新运行命令并且错误消失了(它现在正在执行编辑的代码)。

我怎么能跳过这些步骤来节省调试时间。不在 Docker 容器中运行它不是一个选项,因为否则我无法连接到数据库。

谢谢您的帮助!

- - - - - - - - - - - - - 编辑 - - - - - - - - - - - - ---

使用 docker-compose.yml 文件时,我收到以下错误消息:

docker-compose up --build
Creating network "assign-2-sp20-alexanderfarr_default" with the default driver
Building link-api
Step 1/7 : FROM python:3
 ---> d47898c6f4b0
Step 2/7 : COPY . /app
 ---> c8dbb4404399
Step 3/7 : WORKDIR /app
 ---> Running in 152ccecb6f14
Removing intermediate container 152ccecb6f14
 ---> f0c2d14ba0f8
Step 4/7 : RUN pip install -r requirements.txt       
 ---> Running in 3fcdf8172dda
Collecting flask==1.1.1
  Downloading Flask-1.1.1-py2.py3-none-any.whl (94 kB)
Collecting mysqlclient==1.4.6
  Downloading mysqlclient-1.4.6.tar.gz (85 kB)
Collecting Jinja2>=2.10.1
  Downloading Jinja2-2.11.1-py2.py3-none-any.whl (126 kB)
Collecting click>=5.1
  Downloading click-7.1.1-py2.py3-none-any.whl (82 kB)
Collecting Werkzeug>=0.15
  Downloading Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
Collecting itsdangerous>=0.24
  Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB)
Collecting MarkupSafe>=0.23
  Downloading MarkupSafe-1.1.1-cp38-cp38-manylinux1_x86_64.whl (32 kB)
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (setup.py): started 
  Building wheel for mysqlclient (setup.py): finished with status 'done'
  Created wheel for mysqlclient: filename=mysqlclient-1.4.6-cp38-cp38-linux_x86_64.whl size=116053 sha256=b25b6e9dc0f92e443ee8a6daac87ef71c759b44ad0730369aa465c8b26609095
  Stored in directory: /root/.cache/pip/wheels/8a/3c/e6/347e293dbcd62352ee2806f68d624aae821bca7efe0070c963Successfully built mysqlclient
Installing collected packages: MarkupSafe, Jinja2, click, Werkzeug, itsdangerous, flask, mysqlclient      
Successfully installed Jinja2-2.11.1 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.1 flask-1.1.1 itsdangerous-1.1.0 mysqlclient-1.4.6
Removing intermediate container 3fcdf8172dda
 ---> 71bda153bd31
Step 5/7 : EXPOSE 5000
 ---> Running in a49eb471dabb
Removing intermediate container a49eb471dabb
 ---> c2d79d00ac7e
Step 6/7 : ENTRYPOINT ["flask"]
 ---> Running in 09e4ae9cd21c
Removing intermediate container 09e4ae9cd21c
 ---> 63539b6e533b
Step 7/7 : CMD ["run", "--host=0.0.0.0"]
 ---> Running in ff756d15dc1a
Removing intermediate container ff756d15dc1a
 ---> 576d008b2d3f
Successfully built 576d008b2d3f
Successfully tagged assign-2-sp20-alexanderfarr_link-api:latest
Creating assign-2-sp20-alexanderfarr_some-mysql_1 ...Creating assign-2-sp20-alexanderfarr_some-mysql_1 ...Creating assign-2-sp20-alexanderfarr_link-api_1   ...Creating assign-2-sp20-alexanderfarr_link-api_1   ...Attaching to assign-2-sp20-alexanderfarr_some-mysql_1, assign-2-sp20-alexanderfarr_link-api_1
some-mysql_1  | 2020-04-02 15:26:43+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.19-1debian10 started.
some-mysql_1  | 2020-04-02 15:26:43+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'        
some-mysql_1  | 2020-04-02 15:26:43+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.19-1debian10 started.
some-mysql_1  | 2020-04-02 15:26:43+00:00 [Note] [Entrypoint]: Initializing database files
some-mysql_1  | 2020-04-02T15:26:43.299596Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
some-mysql_1  | 2020-04-02T15:26:43.299985Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.19) initializing of server in progress as process 44  
some-mysql_1  | 2020-04-02T15:26:43.313752Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /var/lib/mysql/ is case insensitive
link-api_1    |  * Environment: production
link-api_1    |    WARNING: This is a development server. Do not use it in a production deployment.       
link-api_1    |    Use a production WSGI server instead.
link-api_1    |  * Debug mode: off
link-api_1    | Usage: flask run [OPTIONS]
link-api_1    |
link-api_1    | Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
link-api_1    |  * Environment: production
link-api_1    |    WARNING: This is a development server. Do not use it in a production deployment.       
link-api_1    |    Use a production WSGI server instead.
link-api_1    |  * Debug mode: off
link-api_1    | Usage: flask run [OPTIONS]
link-api_1    |
link-api_1    | Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
assign-2-sp20-alexanderfarr_link-api_1 exited with code 2
link-api_1    |  * Environment: production
link-api_1    |    WARNING: This is a development server. Do not use it in a production deployment.       
link-api_1    |    Use a production WSGI server instead.
link-api_1    |  * Debug mode: off
link-api_1    | Usage: flask run [OPTIONS]
link-api_1    |
link-api_1    | Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
assign-2-sp20-alexanderfarr_link-api_1 exited with code 2
link-api_1    |  * Environment: production
link-api_1    |    WARNING: This is a development server. Do not use it in a production deployment.       
link-api_1    |    Use a production WSGI server instead.
link-api_1    |  * Debug mode: off
link-api_1    | Usage: flask run [OPTIONS]
link-api_1    |
link-api_1    | Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
assign-2-sp20-alexanderfarr_link-api_1 exited with code 2
link-api_1    |  * Environment: production
link-api_1    |    WARNING: This is a development server. Do not use it in a production deployment.       
link-api_1    |    Use a production WSGI server instead.
link-api_1    |  * Debug mode: off
link-api_1    | Usage: flask run [OPTIONS]
link-api_1    |
link-api_1    | Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
assign-2-sp20-alexanderfarr_link-api_1 exited with code 2
some-mysql_1  | mysqld: Cannot change permissions of 
the file 'ca.pem' (OS errno 1 - Operation not permitted)
some-mysql_1  | 2020-04-02T15:26:48.294958Z 0 [ERROR] [MY-010295] [Server] Could not set file permission for ca.pem
some-mysql_1  | 2020-04-02T15:26:48.295183Z 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files 
that the server added to it.
some-mysql_1  | 2020-04-02T15:26:48.295514Z 0 [ERROR] [MY-010119] [Server] Aborting
link-api_1    |  * Environment: production
link-api_1    |    WARNING: This is a development server. Do not use it in a production deployment.       
link-api_1    |    Use a production WSGI server instead.
link-api_1    |  * Debug mode: off
link-api_1    | Usage: flask run [OPTIONS]
link-api_1    |
link-api_1    | Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
some-mysql_1  | 2020-04-02T15:26:50.202409Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.19)  MySQL Community Server - GPL.
assign-2-sp20-alexanderfarr_link-api_1 exited with code 2

我需要改变什么?

标签: pythondocker

解决方案


最简单的选择是直接在主机上运行您的应用程序代码。如果唯一阻止此操作的是对数据库的访问,您可以在命令中添加一个命令行选项-p 3306:3306docker run ... mysql之前mysql),并将您的应用程序配置为联系localhost端口 3306。(在开发与运行应用程序时,您将有不同的设置运行您的应用程序。在一个容器中,这没关系。)如果您已经在端口 3306 上有一个本地 MySQL,请为选项中的第一个端口号选择一些不同的数字,-p并将您的配置更改为指向备用端口。

对于这样的多容器设置,Docker Compose可以封装所有这些不同的docker run选项和设置。将您在此处显示的命令转换为docker-compose.yml文件可能如下所示:

version: '3'
services:
  some-mysql:
    image: 'mysql:latest'
    restart: unless-stopped
    environment:
      - MYSQL_ROOT_PASSWORD=my-secret-pw
      - MYSQL_DATABASE=demo
    volumes:
      - './new_demo_db_folder:/var/lib/mysql'
    # ports:
    #   - '3306:3306'
    command: --default-authentication-plugin=mysql_native_password
  link-api:
    build: .
    restart: unless-stopped
    ports:
      - 5000:5000
    depends_on:
      - some-mysql

现在你可以运行了docker-compose up --build,它会重建你的应用程序代码,重新创建你的应用程序容器,并重新启动一切;它将有效地执行您在初始设置中列出的所有命令。Compose 会为您创建一个默认网络(请参阅Compose 中的网络),因此您不需要显式的网络选项;您应该能够将此处的其他设置与您的docker run命令中的内容相匹配。您可以将此文件与您的应用程序一起签入源代码管理,以便其他人重现您的设置。


推荐阅读