Docker Community Forums

Share and learn in the Docker community.

Can't See Ports When running docker-compose ps

Hello Everyone… I am using docker for the first time
And I am attempting to host a python script on a docker container from my windows 10 laptop
The issue is that… the container starts successfully, but when I run docker-compose ps I can’t see the ports I defined in the docker-compose file.

Here are my codes

Docker File:
FROM python:3.7-alpine

WORKDIR /.

COPY script.py /

CMD [ "python", "./script.py"  ]

Docker-Compose

version: "3.9"
services:
  web:
   build: 
      context: .
      dockerfile: Dockerfile.dockerfile
   ports:
    - "80:80"

Python Script
import json as j, urllib.request

with urllib.request.urlopen("https://raw.githubusercontent.com/victorebhojie/devopsproject/master/data.json") as url:

    d = j.loads(url.read().decode())

import xml.etree.cElementTree as e        

r = e.Element("Employee")

e.SubElement(r,"Name").text = d["Name"]

e.SubElement(r,"Designation").text = d["Designation"]

e.SubElement(r,"Salary").text = str(d["Salary"])

e.SubElement(r,"Age").text = str(d["Age"])

a = e.ElementTree(r)

a.write("/tmp/json_to_xml.xml")

Docker-compose output

   Name                Command         State    Ports
---------------------------------------------------------
devopsproject_web_1   python ./script.py   Exit 0

Please where is this issue coming from?

The state of the container should be “Up”, not “Exit”. A container needs a running service in the foreground to stay alive. If your script just runs one time and then exits the container will be stopped.

1 Like

Thank you very much, I modified my script to run a loop and verified your answer