title: Python exits with code 0 immediatelly after stand up with docker
body: <p>I am trying to prepare a docker-compose file that stands up 2 containers. They are postgres and python app inside an alpine image. Just consider before reading ı need to use python inside alpine. </p>
  
  <p>My Dockerfile is:</p>
  
  <pre><code>FROM python:3
  
  WORKDIR /usr/src/app
  
  COPY requirements.txt ./
  RUN pip install --no-cache-dir -r requirements.txt
  
  COPY . .
  
  CMD [ "python", "./app.py" ]
  </code></pre>
  
  <p>My app.py file:</p>
  
  <pre><code>import psycopg2
  from config import config
  
  def connect():
      """ Connect to the PostgreSQL database server """
      conn = None
      try:
          # read connection parameters
          params = config()
  
          # connect to the PostgreSQL server
          print('Connecting to the PostgreSQL database...')
          conn = psycopg2.connect(**params)
  
          # create a cursor
          cur = conn.cursor()
  
   # execute a statement
          print('PostgreSQL database version:')
          cur.execute ("SELECT * FROM my_table;")
  
          # display the PostgreSQL database server version
          db_version = cur.fetchone()
          print(db_version)
  
       # close the communication with the PostgreSQL
          cur.close()
      except (Exception, psycopg2.DatabaseError) as error:
          print(error)
      finally:
          if conn is not None:
              conn.close()
              print('Database connection closed.')
  
  
  if __name__ == '__main__':
      connect()
  </code></pre>
  
  <p>I started python container with that command:</p>
  
  <blockquote>
    <p>docker run -it my_image app.py</p>
  </blockquote>
  
  <p>I seperately started 2 container(postgres and python) and make it worked. However my container works only once and its job is to make an <code>select</code> job inside postgresql database. </p>
  
  <p>That was first part. My main goal is below.</p>
  
  <p>For the simplicity i prepared a docker-compose.yml file:</p>
  
  <pre><code>version: '3'
  
  services:
  
    python:
      image: python
      build:
          context: .
          dockerfile: Dockerfile
  
    postgres:
      image: postgres:${TAG:-latest}
      build:
        context: .
      environment:
        POSTGRES_PASSWORD: example
      ports:
        - "5435:5432"
      networks:
        - postgres
  
  networks:
    postgres:
  </code></pre>
  
  <p>My dockerfile is <a href="https://github.com/docker-library/python/blob/caba688d74997c2d063907e35f5439b1b1811d9e/3.6/alpine3.7/Dockerfile" rel="nofollow noreferrer">here</a></p>
  
  <p>When i type docker-compose up my postgres starts but python exits with code 0.</p>
  
  <blockquote>
    <p>my_file_python_1 exited with code 0</p>
  </blockquote>
  
  <p>What should i do for stands alone container for python apps with docker-compose? It always works only once. I can make it work constantly with </p>
  
  <blockquote>
    <p>docker run -d -it my_image app.py</p>
  </blockquote>
  
  <p>But my goal is to make it with docker-compose.</p>