简体   繁体   English

如何在后台运行 Python Docker 容器并在我的命令中使用它们?

[英]How do I run Python Docker containers in the background and use them in my commands?

I want to use different Python versions in my tests.我想在我的测试中使用不同的 Python 版本。 I want to have 3 Docker containers for Python 3.6, Python 3.7 and Python 3.8 running and I want the commands python3.6 , python3.7 , python3.8 to be accessible in the environment.我想要运行 Python 3.6、Python 3.7 和 Python 3.8 的 3 个 Docker 容器,并且我希望命令python3.6python3.7python3.8可以在环境中访问。

If it can also associate these containers with just python command, that could help as well.如果它还可以将这些容器仅与python命令相关联,那也会有所帮助。

I suppose you mean you want 3 docker containers running, each with a different Python version.我想你是说你想要运行 3 个 docker 容器,每个容器都有不同的 Python 版本。 And you want to execute commands from the host for each python container.并且您想为每个 python 容器执行来自主机的命令。

You can do this with the following scripts:您可以使用以下脚本执行此操作:

docker-compose.yml docker-compose.yml

Create a docker-compose.yml file with the following content:创建一个 docker-compose.yml 文件,内容如下:

version: '3.9'

services:

  python36:
    image: python:3.6.14-buster
    container_name: python36
    working_dir: /app
    volumes:
      - ./app:/app
    command: ["sleep", "30d"]

  python37:
    image: python:3.7.11-buster
    container_name: python37
    working_dir: /app
    volumes:
      - ./app:/app
    command: [ "sleep", "30d" ]


  python38:
    image: python:3.8.11-buster
    container_name: python38
    working_dir: /app
    volumes:
      - ./app:/app
    command: [ "sleep", "30d" ]

Python script to run要运行的 Python 脚本

In a folder called ./app create a file called version.py在名为 ./app 的文件夹中创建一个名为 version.py 的文件

import sys

print(sys.version)

Command on the Host主机上的命令

Create a Bash script on the host running the version.py for all three containers.在为所有三个容器运行 version.py 的主机上创建一个 Bash 脚本。 Call it run_command.sh (and 'chmod 755' it of course)称它为 run_command.sh(当然还有'chmod 755')

#!/bin/bash

docker exec -it python36 python version.py
docker exec -it python37 python version.py
docker exec -it python38 python version.py

Run it with:运行它:

./run_command.sh

output输出

The output:输出:

$ ./run_command.sh
3.6.14 (default, Jun 29 2021, 21:23:13)
[GCC 8.3.0]
3.7.11 (default, Jun 29 2021, 20:31:06)
[GCC 8.3.0]
3.8.11 (default, Jun 29 2021, 19:54:56)
[GCC 8.3.0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python docker sdk 如何在containers.run 中运行多个命令 - python docker sdk how to run multiple commands in containers.run 如何使用python中的命令模块在后台运行命令? - How do I run a command in background using commands module in python? 如何在程序中使用&lt;&gt;命令? - How do I use the < > commands in my program? 如何为 3 个容器使用单端口而不是 3 个端口 Python-Flask、PostgreSQL 和 Angular8? 所以我可以使用 Docker 运行而不是 Docker 撰写 - How to use Single port for 3 containers instead of 3 ports Python-Flask, PostgreSQL & Angular8 ? so I can use Docker Run instead of Docker Compose 如何从python运行docker命令? - How to run the docker commands from python? 运行Docker容器的Python脚本 - Python script to run docker containers 如何正确使用python访问系统命令并使我的脚本工作? - How do I properly use python to access system commands and make my script work? 如何使用带有 VS Code 的活动容器在 Docker 上运行 python 程序? - How Can I Run the python programs on Docker using active containers with the VS Code? 如何从 python 运行多个外部命令? - How do I run multiple external commands from python? Python docker sdk 如何在containers.run中设置cpu计数 - Python docker sdk how to set cpu count in containers.run
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM