I am using docker to run a python script that uses subprocess
module to run some commands in terminal ,
After Running the script using docker, if i use ctrl+c
or kill
command to kill the the process , in my terminal process is getting killed, but process continue to run in background and create logs as well.
I tried running the script directly in my system , and it works fine and after killing it just stop, but when i ran using docker it continue to run in background even after kill -9 process_id
.
sample code to simulate the issue.
import getpass
import subprocess
import os
import signal, time
print('python user: ', getpass.getuser())
def test(i):
try:
drop_temp_tbl = subprocess.Popen(f'echo "test">>test.txt;sleep 40; echo "test6{i}">>test.txt;',
shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(drop_temp_tbl_stdout, drop_temp_tbl_stderr) = drop_temp_tbl.communicate()
print('_subprocess_user,', drop_temp_tbl_stdout, '\n', drop_temp_tbl_stderr)
except Exception as e:
print('terminate ',os.killpg(os.getpgid(drop_temp_tbl.pid), signal.SIGTERM) )
print('ERROR', e)
for i in range(2):
test(i)
Docker run command
docker run -i --rm -v `pwd`:/code ${docker_id} python_script.py
Dockerfile
FROM ubuntu:latest
RUN apt-get update && \
apt-get install -yf software-properties-common
RUN apt-get -yq update && \
apt-get -yqq install \
openssh-client
COPY ./docker_requirement ./
RUN pip install --no-cache-dir -r docker_requirement
RUN groupadd --gid 1000 app
RUN useradd --uid 1000 --gid app --home /app app
USER 1000
ADD . /code
WORKDIR /code
ENTRYPOINT ["python3"]
for above example running through docker will continue to append test.txt
even after kill -9 process_id