nebi
nebi

Reputation: 751

Run python process in docker in linux screen in detach mode

I have written Dockerfile for my python application.

Requirement is :

Below is my Dockerfile:

FROM ubuntu:16.04

# Update OS
RUN apt-get update
RUN apt-get -y upgrade

# Install Python
RUN apt-get install -y python-dev python-pip screen npm vim net-tools
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install mysql-server python-mysqldb

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY requirements.txt /usr/src/app
RUN pip install --no-cache-dir -r requirements.txt

COPY src /usr/src/app/src
COPY ./src/nsd.ini /etc/

RUN pwd
RUN cd /usr/src/app

RUN service mysql start  
RUN /bin/bash -c "chmod +x src/run_demo_app.sh && src/run_demo_app.sh"

Below is the content of bash script

$ cat src/run_demo_app.sh

$ screen -dm bash -c "sleep 10; python -m src.app";

The problem is Mysql doesn't start. I need to start it manually from container.

Also, the screen becomes dead and application do not start. Manually running the script works fine.

Upvotes: 1

Views: 2087

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146630

So this is a understanding gap and nothing else. Note below issues in your docker file

Never use service command

RUN service mysql start  

Docker doesn't use a init system. So never use a service command inside docker.

Don't put everything in same container

You should not put everything in the same container. So mysql should run in its own container and python in its own

Use official images

You don't need to re-invent the wheel. Use official images as much as possible. You should be using mysql and python images in your case

Use docker-compose when multiple services are needed

In your case since you are requiring multiple services, use docker-compose.

No need to use screen in docker

Screen is used when your want your process to be running even if your SSH disconnects. So that in not needed in docker. If you run your docker run or docker-compose up command with an additional -d flag then your container will automatically be launched in background

Upvotes: 5

Related Questions