[英]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.6
、 python3.7
、 python3.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:您可以使用以下脚本执行此操作:
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" ]
In a folder called ./app create a file called version.py在名为 ./app 的文件夹中创建一个名为 version.py 的文件
import sys
print(sys.version)
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
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.