Docker:避免运行所有容器并再次构建映像

时间:2020-04-02 14:16:56

标签: python 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

因此,我需要杀死,杀死两个容器并重新开始我的初始例程:

# 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

我需要更改什么?

1 个答案:

答案 0 :(得分:0)

绝对简单的选择是直接在主机上运行应用程序代码。如果阻止此操作的唯一方法是访问数据库,则可以在-p 3306:3306之前向docker run ... mysql命令添加命令行选项mysql,并配置您的应用程序与{{ 1}}端口3306。(在开发环境中和在容器中运行应用程序时,会有不同的设置,没关系。)如果在端口3306上已经有本地MySQL,则在localhost选项,然后更改配置以指向备用端口。

对于这样的多容器设置,Docker Compose可以封装所有这些各种-p选项和设置。将您在此处显示的命令转换为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 ,它将重建您的应用程序代码,重新创建应用程序容器并重新启动所有内容;它会有效地执行您在初始设置中列出的所有命令。 Compose会为您创建一个默认网络(请参见Networking in Compose),因此您不需要显式的网络选项。您应该能够将此处的其他设置与docker-compose up --build命令中的内容进行匹配。您可以将此文件与应用程序一起检查到源代码管理中,以便其他人复制该文件。

相关问题