Ожидаемое поведение:
- Печать содержимого текущего каталога
li>
Создает docker-контейнер и копирует в него test_file.py из текущего каталога. - Запускает test_file.py и выводит стандартный вывод контейнера на мою консоль .
- Выводит содержимое текущего каталога (включая test_file. py)
- Docker не может найти test_file.py
import subprocess
dockerfile_content = f"""
FROM python:3.9-slim
WORKDIR /app
COPY ./test_file.py /app/test_file.py
CMD ["python", "app/test_file.py"]
"""
# Docker image and container names
image_name = 'image_name'
container_name = 'conainer_name'
# Build the Docker image
subprocess.run(['ls'])
build_process = subprocess.Popen(['docker', 'build', '-t', image_name, "-"], stdin=subprocess.PIPE)
build_process.communicate(input=dockerfile_content.encode())
# Run the Docker container
result = subprocess.run(['docker', 'run', '--name', container_name, image_name], capture_output=True, text=True)
print('STDOUT:', result.stdout)
print('STDERR:', result.stderr)
# Clean up: remove container and image
subprocess.run(['docker', 'rm', container_name], check=False)
subprocess.run(['docker', 'rmi', image_name], check=False)
Выход:
test_file.py main.py
+] Building 1.1s (7/7) FINISHED docker:desktop-linux
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 148B 0.0s
=> [internal] load metadata for docker.io/library/python:3.9-slim 1.1s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/3] FROM docker.io/library/python:3.9-slim@sha256:d3185e5aa645a4ff0b52416af05c8465d93791e49c5a0d0f565c119099f26cde 0.0s
=> => resolve docker.io/library/python:3.9-slim@sha256:d3185e5aa645a4ff0b52416af05c8465d93791e49c5a0d0f565c119099f26cde 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 2B 0.0s
=> CACHED [2/3] WORKDIR /app 0.0s
=> ERROR [3/3] COPY ./test_file.py /app/test_file.py 0.0s
------
> [3/3] COPY ./test_file.py /app/test_file.py:
------
Dockerfile:6
--------------------
4 | WORKDIR /app
5 |
6 | >>> COPY ./test_file.py /app/test_file.py
7 |
8 | CMD ["python", "app/test_file.py"]
--------------------
ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref d7cab07d-c12c-40fd-80e6-5ccd8b3c75df::nufuebj2yhxnpjfnizar2q5go: "/test_file.py": not found
STDOUT:
STDERR: Unable to find image 'image_name:latest' locally
docker: Error response from daemon: pull access denied for image_name, repository does not exist or may require 'docker login': denied: requested access to the resource is denied.
See 'docker run --help'.
Error response from daemon: No such container: conainer_name
Error response from daemon: No such image: image_name:latest
Подробнее здесь: https://stackoverflow.com/questions/786 ... hon-script