# Cannot figure out Dockerfile template and start.sh

**URL:** <https://forums.balena.io/t/cannot-figure-out-dockerfile-template-and-start-sh/65605>\
**Category:** General\
**Tags:** raspberrypi3\
**Created:** [February 25, 2020, 8:37am UTC](https://forums.balena.io/t/cannot-figure-out-dockerfile-template-and-start-sh/65605 "2020-02-25T08:37:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![lakshaygroverr](https://avatars.discourse-cdn.com/v4/letter/l/b38774/32.png) [@lakshaygroverr](https://forums.balena.io/u/lakshaygroverr)\
**Post date:** [February 25, 2020, 8:37am UTC](https://forums.balena.io/t/cannot-figure-out-dockerfile-template-and-start-sh/65605/1 "2020-02-25T08:37:59Z")

</div>

Hello, I have been trying to run a python script that uses openCV to show videos and images.  
The main directory contains 1 folder that is cache ( used to store media) , the python script, Dockerfile.template and start.sh. After several tried I am unable to display the output of Python script using XServer. Since I am very new to this, could anyone please help me write the template and start.sh

Thanks

---

<div class="post-metadata">

**Author:** ![afitzek](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/afitzek/32/359_2.png) [@afitzek](https://forums.balena.io/u/afitzek)\
**Post date:** [February 25, 2020, 9:56am UTC](https://forums.balena.io/t/cannot-figure-out-dockerfile-template-and-start-sh/65605/2 "2020-02-25T09:56:48Z")

</div>

Hi,

To give you a starting point, we have an example project here that uses xserver [https://github.com/balena-io-playground/x11-window-manager](https://github.com/balena-io-playground/x11-window-manager) maybe you can find some more hints there. If you can share your project with us, we’ll probably be able to help you get started.

---

<div class="post-metadata">

**Author:** ![lakshaygroverr](https://avatars.discourse-cdn.com/v4/letter/l/b38774/32.png) [@lakshaygroverr](https://forums.balena.io/u/lakshaygroverr)\
**Post date:** [February 25, 2020, 10:02am UTC](https://forums.balena.io/t/cannot-figure-out-dockerfile-template-and-start-sh/65605/4 "2020-02-25T10:02:21Z")

</div>

> **[lakshaygroverr/XserverBalenaOs](https://github.com/lakshaygroverr/XserverBalenaOs)**
>
> Contribute to lakshaygroverr/XserverBalenaOs development by creating an account on GitHub.

The following is the git repo im trying to run on Xserver. Kindly help!

---

<div class="post-metadata">

**Author:** ![afitzek](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/afitzek/32/359_2.png) [@afitzek](https://forums.balena.io/u/afitzek)\
**Post date:** [February 25, 2020, 12:37pm UTC](https://forums.balena.io/t/cannot-figure-out-dockerfile-template-and-start-sh/65605/5 "2020-02-25T12:37:46Z")

</div>

Hi,

There are a couple of problems with your code.

- You are missing a `COPY` operation in your dockerfile to actually move the files into your docker image.
- You are trying to start the new\_video.py script, which in it’s path is missing the leading `/`
- If you want to use a `start.sh` you need to call the `start.sh` script as `CMD` in your dockerfile.

Please take a look at the referenced example repository and we are happy to help with specific questions.

---

<div class="post-metadata">

**Author:** ![lakshaygroverr](https://avatars.discourse-cdn.com/v4/letter/l/b38774/32.png) [@lakshaygroverr](https://forums.balena.io/u/lakshaygroverr)\
**Post date:** [February 26, 2020, 7:42am UTC](https://forums.balena.io/t/cannot-figure-out-dockerfile-template-and-start-sh/65605/9 "2020-02-26T07:42:56Z")

</div>

Hi, the above solution worked well and managed to run the python script well. I have installed the below mentioned packages and still I am getting error of import.

RUN install\_packages   
xserver-xorg-core   
xserver-xorg-input-all   
xserver-xorg-video-fbdev   
xorg   
libxcb-image0   
libxcb-util0   
xdg-utils   
libdbus-1-3   
libnotify4   
libgnome-keyring0   
libgconf-2-4   
libasound2   
libcap2   
libcups2   
libxtst6   
libxss1   
libnss3   
libsmbclient   
libssh-4   
fbset   
libexpat1   
python3-tk

**ERROR**

Traceback (most recent call last):  
26.02.20 13:11:20 (+0530) main File “new\_video.py”, line 6, in   
26.02.20 13:11:20 (+0530) main import tkinter  
26.02.20 13:11:20 (+0530) main File “/usr/local/lib/python3.5/tkinter/ **init**.py”, line 35, in   
26.02.20 13:11:20 (+0530) main import \_tkinter # If this fails your Python may not be configured for Tk  
26.02.20 13:11:20 (+0530) main ImportError: No module named ‘\_tkinter’  
26.02.20 13:12:16 (+0530) main Traceback (most recent call last):  
26.02.20 13:12:16 (+0530) main File “new\_video.py”, line 6, in   
26.02.20 13:12:16 (+0530) main import tkinter  
26.02.20 13:12:16 (+0530) main File “/usr/local/lib/python3.5/tkinter/ **init**.py”, line 35, in   
26.02.20 13:12:16 (+0530) main import \_tkinter # If this fails your Python may not be configured for Tk  
26.02.20 13:12:16 (+0530) main ImportError: No module named ‘\_tkinter’

Also I am using PIL,openCV and tkinter to build the application. What should be the imports in requirements.txt or dockerfile.template since when I am trying, I am getting the above errors. Thanks in advance

---

<div class="post-metadata">

**Author:** ![sradevski](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/sradevski/32/1609_2.png) [@sradevski](https://forums.balena.io/u/sradevski)\
**Post date:** [February 26, 2020, 9:36am UTC](https://forums.balena.io/t/cannot-figure-out-dockerfile-template-and-start-sh/65605/10 "2020-02-26T09:36:41Z")

</div>

Hey, a quick google search gave me this: [https://askubuntu.com/questions/815874/importerror-no-named-tkinter-please-install-the-python3-tk-package](https://askubuntu.com/questions/815874/importerror-no-named-tkinter-please-install-the-python3-tk-package). I believe you need to install the specific tkinter version for the python on the system.

---

<div class="post-metadata">

**Author:** ![lakshaygroverr](https://avatars.discourse-cdn.com/v4/letter/l/b38774/32.png) [@lakshaygroverr](https://forums.balena.io/u/lakshaygroverr)\
**Post date:** [February 26, 2020, 11:58am UTC](https://forums.balena.io/t/cannot-figure-out-dockerfile-template-and-start-sh/65605/11 "2020-02-26T11:58:35Z")

</div>

Hi, thanks for your reply. I tried installing the compatible version with no success.  
I tried installing python3.5-tk but the error persists

---

<div class="post-metadata">

**Author:** ![afitzek](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/afitzek/32/359_2.png) [@afitzek](https://forums.balena.io/u/afitzek)\
**Post date:** [February 26, 2020, 12:17pm UTC](https://forums.balena.io/t/cannot-figure-out-dockerfile-template-and-start-sh/65605/12 "2020-02-26T12:17:36Z")

</div>

Hi,  
It seems like you didn’t install tcl or tk ([https://stackoverflow.com/questions/5459444/tkinter-python-may-not-be-configured-for-tk](https://stackoverflow.com/questions/5459444/tkinter-python-may-not-be-configured-for-tk)). Maybe this would help.

---

<div class="post-metadata">

**Author:** ![lakshaygroverr](https://avatars.discourse-cdn.com/v4/letter/l/b38774/32.png) [@lakshaygroverr](https://forums.balena.io/u/lakshaygroverr)\
**Post date:** [February 27, 2020, 8:55am UTC](https://forums.balena.io/t/cannot-figure-out-dockerfile-template-and-start-sh/65605/14 "2020-02-27T08:55:13Z")

</div>

Okay, so after every possible solution I could try, I did. No success, but things got better when I used build version instead of run  
`FROM balenalib/raspberry-pi-python:3.6-stretch-build as build`

But still a long way to go. I am posting my Dockerfile.template and the python code. Please see what can be done since there are many errors and ultimately display screen using Xserver has to be. Thanks

**Dockerfile.template**

```auto
# Build stage
##
#FROM balenalib/%%BALENA_MACHINE_NAME%%-node:10-build as build
#FROM balenalib/i386-debian-python:latest
FROM balenalib/raspberry-pi-python:3.6-stretch-build as build	

#RUN pip install --upgrade pip

RUN install_packages \ 
  tcl \
  python3-tk

WORKDIR /usr/src/app
	
#RUN apt-get install python3-tk
#RUN apt-get --upgrade pip
#RUN apt-get update \
# PIP

#RUN pip install Pillow

#RUN install_packages blt-demo tk8.6 tix python3-tk-dbg tcl-tclreadline

RUN pip install Pillow==2.2.1

COPY . ./

#RUN --update add python py-pip openssl ca-certificates py-openssl wget
#RUN --update add --virtual build-dependencies libffi-dev openssl-dev python-dev py-pip build-base \
# && pip install --upgrade pip \
# && pip install -r requirements.txt 

# Install runtime dependencies
#RUN install_packages \ tcl \ tk \ apt-utils
	
#RUN pip install -r requirements.txt

# Start app
# CMD ["./start.sh"]
CMD ["python","-u","new_video.py"]

```

**new\_video.py**

```auto
if sys.version_info[0] == 2: # the tkinter library changed it's name from Python 2 to 3.
    import Tkinter
    print("Version 2 it is")
    tkinter = Tkinter #I decided to use a library reference to avoid potential naming conflicts with people's programs.
else:
    import tkinter
from PIL import Image, ImageTk
import time
import glob
import cv2

root = tkinter.Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.overrideredirect(1)
root.geometry("%dx%d+0+0" % (w, h))
root.focus_set()
canvas = tkinter.Canvas(root,width=w,height=h)
canvas.pack()
canvas.configure(background='black')

def showPIL(pilImage):
    #imgWidth, imgHeight = pilImage.size
 # resize photo to full screen 
    #ratio = min(w/imgWidth, h/imgHeight)
    #imgWidth = int(imgWidth*ratio)
    #imgHeight = int(imgHeight*ratio)
    #pilImage = pilImage.resize((imgWidth,imgHeight), Image.NEAREST)   
    image = ImageTk.PhotoImage(pilImage)
    imagesprite = canvas.create_image(w/2,h/2,image=image)
    #root.update_idletasks()
    root.update()
# root.bind("<Escape>", lambda e: (e.widget.withdraw(), e.widget.quit()))

names = glob.glob('cache/*')
print(names)
while True:
    for file in names:
        if file.split('.')[-1] == 'mp4':
            print('video')
            start = time.time()
            video = cv2.VideoCapture(file)
            while True:
                ret,frame = video.read()
                if ret:
                    frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
                    frame = Image.fromarray(frame)
                    showPIL(frame)
                else:
                    print('time taken:',(time.time()-start))
                    break

        elif file.split('.')[-1] == 'jpg' or file.split('.')[-1] == 'png':
            print(file)
            media_file=Image.open(file)
            showPIL(media_file)
            sleep_duration = file.split('.')[0].split('_')[-1]
            print(sleep_duration)
            time.sleep(int(sleep_duration))
        else:
            print('format not supported')

```

**ERROR**

```auto
27.02.20 14:25:42 (+0530) main Traceback (most recent call last):
27.02.20 14:25:42 (+0530) main File "new_video.py", line 7, in <module>
27.02.20 14:25:42 (+0530) main import tkinter
27.02.20 14:25:42 (+0530) main File "/usr/local/lib/python3.6/tkinter/ __init__.py", line 36, in <module>
27.02.20 14:25:42 (+0530) main import _tkinter # If this fails your Python may not be configured for Tk
27.02.20 14:25:42 (+0530) main ModuleNotFoundError: No module named '_tkinter'

```

---

<div class="post-metadata">

**Author:** ![chrisys](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/chrisys/32/1731_2.png) [@chrisys](https://forums.balena.io/u/chrisys)\
**Post date:** [February 27, 2020, 12:38pm UTC](https://forums.balena.io/t/cannot-figure-out-dockerfile-template-and-start-sh/65605/16 "2020-02-27T12:38:36Z")

</div>

@lakshaygroverr have you searched for these error messages online? I’m having a quick look and there are quite a few suggestions for things to try.

[https://askubuntu.com/questions/815874/importerror-no-named-tkinter-please-install-the-python3-tk-package](https://askubuntu.com/questions/815874/importerror-no-named-tkinter-please-install-the-python3-tk-package) (Stevche suggested this one above but it doesn’t look like you’ve tried the suggestion there in the current version of your Dockerfile)

[https://stackoverflow.com/questions/5459444/tkinter-python-may-not-be-configured-for-tk](https://stackoverflow.com/questions/5459444/tkinter-python-may-not-be-configured-for-tk) - Again Andy posted this above, but if you’re not able to get it working you can install your own version of Python as it suggests here, you don’t necessarily have to use the base image. I’m also interested to know why you’re using Stretch? I’d suggest `balenalib/raspberry-pi-debian-python:3.7-buster-build` or `balenalib/raspberry-pi-debian:-buster-build` if you are going to install your own version of Python.
