paho MQTT is not responding with docker container - python

i am new to docker and making my first application I would be very thankful if someone points to me right direction.
I build the image and when run this image, I get no response from docker run commands. it keeps loading. below is python script:
When I interrupt(ctrl+c)through keyboard it immediately shows outputs(print statement) otherwise it does not perform anything.
The Dockerfile is:
FROM python:2.7-slim
WORKDIR /root/
ADD . /root
RUN pip install numpy
COPY app.py app.py
ENTRYPOINT []
CMD ["python", "app.py"]
Docker run command:
docker run ImageName
Please help!

This is probably because python buffers stdout/stdin by default. Edit your docker file to add the -u to the python command line:
CMD ["python", "-u", "app.py"]

This is the solution of my problem, docker run commands takes -it flag.
sudo docker run -it imageName

Related

Python docker image fails because of missing shell / bash in the image

I have the following Dockerfile:
FROM python:3-alpine
WORKDIR /usr/src/app
COPY requirements.txt .
RUN pip install -qr requirements.txt
COPY target-watch.py .
CMD ["python3", "./target-watch.py"]
If I deploy this to a Kubernetes cluster the build went fine, but I got an error from the Kubernetes logs. To verfiy my image I run the following command:
docker run --rm -it --entrypoint /bin/bash docker-conveyor.xxx.com/myorg/my_cron_jobs:2021.12.08_03.51_abcdef
Which gives me this response:
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "/bin/bash": stat /bin/bash: no such file or directory: unknown.
How can this be fixed? I assume a shell is missing in my image. How do i have to change my Dockerfile to make it work without any errors?
Your container image doesn't have bash so you should use /bin/sh instead of /bin/bash.
docker run --rm -it --entrypoint /bin/sh docker-conveyor.xxx.com/myorg/my_cron_jobs:2021.12.08_03.51_abcdef
Alpine docker image doesn't have bash installed by default. You will need to add the following commands to get bash:
RUN apk update && apk add bash
If you're using Alpine 3.3+ then you can just do
RUN apk add --no-cache bash

I cannot containerise my python code in a docker container

I am learning docker. I want to practice how to see logs inside of a docker container once I run a python image.
This is the python code I want to execute:
#loop.py
#loop.py
import time
while True:
print('test')
time.sleep(3)
This is the docker file:
FROM python:3.8-slim-buster
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
COPY requirements.txt .
RUN python -m pip install -r requirements.txt
WORKDIR /app
COPY . /app
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser
CMD ["python", "loop.py"]
I build the image like this:
docker build -t image_test .
Once it is created ( I can see it doing docker image ls) I want to run it, I have different ways but the container is created and is terminated instantly:
docker run <imageID>.
I have tried too:
docker docker run <imageID> sleep infinity. Then I can do: docker exec -it <containerID> bash and I can run inside the container python loop.py but it automatically ends.
I modified the CMD like this as well:
CMD ["python","-u", "loop.py"]
I have tried:
docker run -it --entrypoint=/bin/bash image_test and I directly go to the terminal inside of the container, but if I execute the python code as before, it automatically ends, instead of run the infinite loop.
why?
You don't need bash to run a python program. You can use it to exec into a running container. But the container will be running as long as the python is connected to the stdout.
I ran these commands and they all work on my machine.
sudo docker run image_test
sudo docker run -d image_test
sudo docker ps
sudo docker logs -f 041
sudo docker exec -ti 041 bash
sudo docker stop 041
sudo docker rm 041

How to start the container in Docker in my case

I am trying to setup my python environment in docker.
My docker image is like this:
FROM python:2.7
# updating repository
RUN apt-get update
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
COPY requirements.txt requirements.txt
RUN pip install --no-cache -r requirements.txt
EXPOSE 8888
COPY . .
CMD ["python", "test.py"]
with this build command:
docker build -t ml-python-2.7 .
After image is built,
I ran
docker run -it --name ml-container -v ${PWD}:/usr/src/app ml-python-2.7 python test.py
My sample test.py
print('test here')
It works when I first run this command and update the output every time I changed my test.py
The problem is if I want to keep the container and remove the --rm option, the container quit and I can't run
docker run -it --name ml-container -v ${PWD}:/usr/src/app ml-python-2.7 python test.py
anymore because it says there is a container name conflict. How do I keep the container and run the test.py again after that file is updated? Thanks!
After the container has exited, you can start it again using docker start. More information here: How to continue a docker which is exited

Docker Container stops immediately (Flask/Python/Megatutorial)

I've been following the flask megatutorial by the inestimable Miguel Grinberg (https://learn.miguelgrinberg.com/read/mega-tutorial/ch19.html), and recently hit on a snag in deployment.
The docker run command starts the container and then it immediately stops. It isn't showing up in docker ps -a either. I've trawled through lots of responses here which seem to suggest that the solution is to add "-it" to the docker run command however this does not solve the issue.
Here's my dockerfile:
FROM python:3.6-alpine
RUN adduser -D james
WORKDIR /home/myflix
COPY requirements.txt requirements.txt
RUN python -m venv venv
RUN venv/bin/pip install -r requirements.txt
RUN venv/bin/pip install gunicorn pymysql
COPY app app
COPY migrations migrations
COPY myflix.py config.py boot.sh ./
RUN chmod +x boot.sh
ENV FLASK_APP myflix.py
RUN chown -R james:james ./
USER james
EXPOSE 5000
ENTRYPOINT ["./boot.sh"]
My image is called myflix:secondattempt.
The command used to start the container:
sudo docker run --name myflixcont -d -p 8000:5000 --rm myflix:secondattempt
As I said, I've already tried dropping in various combinations of "-i" and "-t" in front of the "-d" to no avail.
-it means interactive tty.
You can not use -it in conjunction with -d which means detached.
Remove -d and add -it:
docker run --name myflixcont -it -p 8000:5000 --rm myflix:secondattempt
Another point (with the purpose of helping you) is that ENTRYPOINT runs in exec mode. meaning that it does not start a bash or dash itself. You should specify it manually and explicitly:
ENTRYPOINT ["sh", "file.sh"]
# or
ENTRYPOINT ["bash", "file.sh"]

Docker: No such file or directory when trying to run docker inside docker

Is docker binaries not available within docker? If so, how could I call it within docker?
app.py
import subprocess
cmd_payload = ['docker']
subprocess.Popen(cmd_payload, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Dockerfile
FROM python:2.7-slim
VOLUME /data
WORKDIR /data
ADD . /data
CMD ["python", "app.py"]
Then, run
docker build -t app .
docker run app
OUTOUT:
[Errno 2] No such file or directory
EDIT:
if i change
cmd_payload = ['docker']
to
cmd_payload = ['echo']
it doesnt throw an error. I would like to run docker executable . How could i do that?
If you want to run docker within of a container, the easy way to reach that is share docker daemon by volumes, Re-run your docker container with this:
docker run -it -v /var/run/docker.sock:/var/run/docker.sock:ro -v /usr/bin/docker:/usr/bin/docker:ro app
Moreover you have to add the library to Dockerfile
FROM python:2.7-slim
RUN apt-get update && apt-get install -y libltdl7 && rm -rf /var/lib/apt/lists/*
VOLUME /data
WORKDIR /data
ADD . /data
CMD ["python", "app.py"]
subprocess was able to run docker executable by adding shell=True
cmd_payload = 'docker'
subprocess.Popen(cmd_payload, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
Not sure why it couldnt find it if not invoked under a shell

Categories

Resources