-2

Hi I am building a simple python image using docker. I am using ubuntu 22.04 and vs code as my editor. I have created a folder inside my local machine and used vs code to write simple python script and created a docker file using vs code and wrote the instructions inside it. My docker file is created in the local repo but inside the file there is no contents which I wrote using vs code.

I have to copy the content of Dockerfile from vs code and use vi to paste inside the empty Dockerfile in my local repo.

Also the image gets built but does not show in docker ps as a running state but as exited one in docker ps -a

Python Script -

import numpy as np  
import matplotlib.pyplot as plt 
a=np.arange(1,18) 
plt.plot(a)  
plt.show()

When running python3 file1.py in cmd it displays the chart but when running sudo docker run graph.py it does not show the graph. What may be the issue ? Kindly explain.

My dockerfile -

FROM python
COPY . /app
WORKDIR /app
RUN pip3 install -r requirements.txt
CMD python file1.py
  • How are you building and running the image? You say without Docker, "it displays the chart"; does it show up in a new window? Running GUI applications in Docker is tricky and non-portable, and I might not bother with a container here. – David Maze Apr 11 '23 at 10:43

1 Answers1

0

Replace your dockerfile to below:

FROM python
COPY . /app
WORKDIR /app
RUN pip3 install -r requirement.txt   (You need to confirm whats the correct name)
CMD python file1.py

This should work. Hope this helps. Thanks.

Suchandra T
  • 569
  • 4
  • 8
  • Still not showing anything in the Docker file – Rising Sun Apr 11 '23 at 06:28
  • Post seeing your edit. it seems you are not facing issues with Dockerfile but with Docker basics. here is the link which will help you: https://stackoverflow.com/questions/28212380/why-docker-container-exits-immediately PS: There is a difference between the Docker image and Container, docker ps shows running container states. – Suchandra T Apr 11 '23 at 06:58
  • I know that when the process completes it automatically exit. In my case it exits after file1.py executes. But why is the output not showing i.e. the graph the script displays a simple linear graph. Lastly how to create a running instance of the image. – Rising Sun Apr 11 '23 at 07:02
  • Are you 100% sure it works locally? if yes have a look: https://stackoverflow.com/questions/46018102/how-can-i-use-matplotlib-pyplot-in-a-docker-container – Suchandra T Apr 11 '23 at 07:04
  • Brother still facing the same issue. When I run python3 file1.py it works in cmd but using sudo docker run graph.py its not showing. Do I need to change anything in the run command . I am not able to understand why is it not working – Rising Sun Apr 11 '23 at 07:14
  • Rising, your command is incorrect. graph.py is (could be) your project file and has nothing to do with docker as such. to see what the valid parameters to docker run have a look: https://docs.docker.com/engine/reference/commandline/run/ – Suchandra T Apr 11 '23 at 07:18
  • Issue is fixed Rising? – Suchandra T Apr 11 '23 at 10:35
  • No Its not resolved I am still not able to get it. If I want my output to display on as a webpage can it be done. My vscode issue is still there If I write in my docker file its not showing anything in the local repo of my docker file. – Rising Sun Apr 11 '23 at 10:49