using python to load docker image zip file without python docker module - python

Using python3.10.6 and docker 20.10.23, build 7155243 on Ubuntu 22.04. And trying not to use the docker module but just subprocess to load a docker image
def run(params):
output = subprocess.run(params, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if output.returncode == 0:
print(output.stdout.decode())
else:
print(output.stderr.decode())
run(['sudo', 'docker', 'load', '<', 'PATH/OF/AIMAGE.zip'])
run(['sudo', 'docker', 'run', '-t', '-d', '-v', PATH_REPO + ':/root/windows-mount', 'IMAGE:v3'])
I can see the image is loaded fine by using sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96d1f80916ce IMAGE:v3 "/bin/bash" 53 seconds ago Up 52 seconds happy_joliot
but the docker load always output an error:
"docker load" accepts no arguments.
See 'docker load --help'.
Usage: docker load [OPTIONS]
Load an image from a tar archive or STDIN
[docerapp.py:66]
Any suggestion to fix the error?

Instead of using '<' which means Load images from STDIN , '-i' / '--input' meaning Load images from a file should be used in the script.
run(['sudo', 'docker', 'load', '-i', 'PATH/OF/AIMAGE.zip'])

Related

FFmpeg youtube stream get corrupted until restart

I am streaming images from python to youtube using ffmpeg.
It's working fine with just one problem.
Sometimes the stream get somehow corrupted an the stream looks like this:
Corrupted stream image
Or you can see for yourself in the stream itself: https://www.youtube.com/watch?v=PG6NLtr0jjM (Scroll back to the beginning)
Then I have to restart ffmpeg and its working fine. Sometimes an error occurs regarding the network and I have to automatically restart the stream. Then there is the possibility it is corrupted again.
The parameters for ffmpeg are the following:
cmd_out = ['ffmpeg',
'-re',
'-f', 'concat',
'-i', os.path.join(BASE_DIR, 'music', 'music.txt').replace('\\', '/'),
'-r', '1',
'-f', 'image2pipe',
'-vcodec', 'png',
'-r', '1',
'-i', '-',
'-c:v', 'libx264',
'-c:a', 'copy',
'-g', '4',
'-f', 'flv',
'rtmp://a.rtmp.youtube.com/live2/{}'.format(stream_key)]
I never had that problem testing it on windows. It just appears when I use my linux server.
I am using ffmpeg version 3.2.18-0+deb9u1 and the generated images are fine, when I save them to a normal file.
Does someone know how to fix it? Or where the problem is comming from?
Solution: An old ffmpeg version was available while using docker. I solved the problem by using a static build from https://www.johnvansickle.com/ffmpeg/
I used this to use it in docker:
WORKDIR /app
ADD ./ffmpeg /app
RUN mv ffmpeg /usr/local/ffmpeg
RUN chmod +x /usr/local/ffmpeg
Until now the error seems to be solved.

How to print python statements in k8s container when running a java application through sub process

I tried making stdout, stdin as None and also subprocess.STDOUT. My container logs are still showing the logs of app.jar. I want the container to show the print statements and the application logs. Disabling logs completely is making the container print no logs. I do not want to clutter my container with application logs for viewers but want to show where the execution is currently
**code.py**
pro = subprocess.Popen(
['/bin/java', '-jar', '/apps/ZAP_2.10.0/app.jar',
'-daemon', '-host', 'localhost', '-port', '8080', '-dir', '', '-silent', '-config', 'api.key=ZAP',
'-config', 'api.addrs.addr.regex=true', '-config', 'connection.timeoutInSecs=300'], shell=False,
stdin=None, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT, close_fds=True)
print("App is running!")
Docker File:
RUN wget app.zip &&
unzip app.zip \
rm -f app.zip
CMD ["/bin/bash", "script.sh"]
My script.sh file:
#!/bin/bash`
source venv/bin/activate`
python code.py
My code.py has the content I mentioned in the question. I am deploying it as 'Deployment' config.

How to run a docker volume mount as a python subprocess?

I am trying to run a docker container, specifically openalpr found here:
https://hub.docker.com/r/openalpr/openalpr/
docker run -it --rm -v $(pwd):/data:ro openalpr -c us plateTest1.jpg
I am running into an error in python when trying to run this command as a subprocess:
import subprocess
app.py
result = subprocess.run(['docker', 'run', '-it', '--rm', '-v pwd:/data:ro', 'openalpr', '-c us', 'plateTest1.jpg'], capture_output=True)
print(result.stdout)
print(result.stderr)
Here is the error I am getting:
(base) mac#macs-MBP lpr % python openalpr_test.py
b''
b'docker: Error response from daemon: invalid volume specification: \' pwd:data:ro\': invalid mount config for type "volume": invalid mount path: \'data\' mount path must be absolute.\nSee \'docker run --help\'.\n'
I am assuming this has to do with escaping slashes?
When you call subprocess.run([...]), you are responsible for breaking the command into "words". There is no further processing here; everything gets passed on exactly as you have it.
In particular, when you
subprocess.run([..., '-v pwd:/data:ro', ...])
The argument passed is exactly -v pwd... including the space in the single argument. Docker sees a -v argument and parses the rest of this "word" as a volume specification, breaking it on colons, so mounting a named volume (with a leading space) pwd on container path /data in read-only mode. Since (space) pwd isn't a valid volume name, you get this error.
You can break this into two separate words to disambiguate this, or remove the intermediate space.
subprocess.run([..., '-v', 'pwd:/data:ro', ...])
subprocess.run([..., '-vpwd:/data:ro', ...])
(In general it's better to avoid launching things as subprocesses if there is a native library or SDK that has the same functionality. You might try to run this process using the Docker Python SDK instead. Remember that it's very easy to use a docker run command to root the entire host: be very careful about what you're launching and how command lines are processed.)
Not exactly the way I laid it out but this seems to work:
from subprocess import Popen
import subprocess
command='''
docker run -it --rm -v $(pwd):/data:ro openalpr -c us plateTest1.jpg
'''
process=Popen(command,shell=True,stdout=subprocess.PIPE)
result=process.communicate()
print(result)

How do I use Python to launch an interactive Docker container?

I am working with a Docker image which I launch in interactive mode like so: docker run -it --rm ubuntu bash
The actual image I work with has many complicated parameters, which is why I wrote a script to construct the full docker run command and launch it for me. As the logic grew more complicated, I want to migrate the script from bash to Python.
Using docker-py, I prepared everything to run the image. Seems like using docker.containers.run for interactive shells is not supported, however. Using subprocess instead seems logical, so I tried the following:
import subprocess
subprocess.Popen(['docker', 'run', '-it', '--rm', 'ubuntu', 'bash'])
But this gives me:
$ python3 docker_run_test.py
$ unable to setup input stream: unable to set IO streams as raw terminal: input/output error
$
Note that the error message appears in a different shell prompt from the python command.
How do I make python3 docker_run_test.py to do equivalent of running docker run -it --rm ubuntu bash?
You can use a pseudo-terminal to read from and write to the container process
import pty
import sys
import select
import os
import subprocess
pty, tty = pty.openpty()
p = subprocess.Popen(['docker', 'run', '-it', '--rm', 'ubuntu', 'bash'], stdin=tty, stdout=tty, stderr=tty)
while p.poll() is None:
# Watch two files, STDIN of your Python process and the pseudo terminal
r, _, _ = select.select([sys.stdin, pty], [], [])
if sys.stdin in r:
input_from_your_terminal = os.read(sys.stdin.fileno(), 10240)
os.write(pty, input_from_your_terminal)
elif pty in r:
output_from_docker = os.read(pty, 10240)
os.write(sys.stdout.fileno(), output_from_docker)
Can we use this ?
import os
os.system('docker run -it --rm ubuntu bash')

How to run docker command in Python SDK

I am using python SDK package to run docker from python.
Here is the docker command I tried to run using python package:
docker run -v /c/Users/msagovac/pdf_ocr:/home/docker jbarlow83/ocrmypdf-polyglot --skip-text 0ce9d58432bf41174dde7148486854e2.pdf output.pdf
Here is a python code:
import docker
client = docker.from_env()
client.containers.run('jbarlow83/ocrmypdf-polyglot', '--skip-text "0ce9d58432bf41174dde7148486854e2.pdf" "output.pdf"', "-v /c/Users/msagovac/pdf_ocr:/home/docker")
Error says file ot found. I am not sure where to set run options:
-v /c/Users/msagovac/pdf_ocr:/home/docker
Try with named parameters:
client.containers.run(
image='jbarlow83/ocrmypdf-polyglot',
command='--skip-text "0ce9d58432bf41174dde7148486854e2.pdf" "output.pdf"',
volumes={'/c/Users/msagovac/pdf_ocr': {'bind': '/home/docker', 'mode': 'rw'}},
)
Also it seems that the path of the volume to mount is incorrect, try with C:/Users/msagovac/pdf_ocr

Categories

Resources