0

I'm seeing the following error when running docker-compose up on my Flask application:

server    | Traceback (most recent call last):
server    |   File "/app/app.py", line 3, in <module>
server    |     import boto3
server    |   File "/usr/local/lib/python3.9/site-packages/boto3/__init__.py", line 16, in <module>
server    |     from boto3.session import Session
server    |   File "/usr/local/lib/python3.9/site-packages/boto3/session.py", line 17, in <module>
server    |     import botocore.session
server    |   File "/usr/local/lib/python3.9/site-packages/botocore/session.py", line 29, in <module>
server    |     import botocore.configloader
server    | ValueError: source code string cannot contain null bytes
server exited with code 1

Inside my Flask app.py I have the following:

import flask
import pandas as pd
import boto3

client = boto3.client('s3')
path = 's3://my-bucket/my-file.csv'
df = pd.read_csv(path)
...

When doing flask run on its own, however, everything works fine. My docker-compose.yml looks like this:

version: "3.9"
services:
    server:
        container_name: server
        build: ./server
        ports:
            - "80:5000"
        volumes:
            - ./server:/app
        environment:
            FLASK_ENV: development
        env_file: 
            - ./.env
    web:
        build: ./app
        ports:
            - "3000:3000"
        volumes:
            - ./app:/user/src/app
        depends_on: 
            - server

and the Dockerfile:

FROM python

WORKDIR /app

ENV FLASK_APP=app.py

ENV FLASK_ENV=development

COPY ./requirements.txt .

RUN pip3 install -r requirements.txt

ENV AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
ENV AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
ENV AWS_REGION='eu-west-2'

COPY . .

CMD ["python3", "app.py"]

Is there a way to fix this?

davidism
  • 121,510
  • 29
  • 395
  • 339
mlan
  • 119
  • 1
  • 9
  • 1
    Your error is `ValueError: source code string cannot contain null bytes`. You can check if you have any strange bytes in your application. Check https://stackoverflow.com/questions/31233777/python-source-code-string-cannot-contain-null-bytes if helps you – Saeed Jul 08 '21 at 14:53
  • 1
    If you're seeing inconsistent results, a good first step is often to delete `volumes:` that overwrite the source code in the image with bind mounts; especially if the host is a Windows system but it's a Linux container there can be compatibility issues (null bytes would be odd, though). – David Maze Jul 08 '21 at 14:59
  • @Saeed thanks for the link - I tried running ```sed -i '' 's/\x0//g' app.py``` but unfortunately it didn't help. Do the null bytes exist in the ```"/usr/local/lib/python3.9/site-packages...``` files denoted in the error message? Because I don't have those on my local machine. – mlan Jul 08 '21 at 15:02
  • @DavidMaze sorry, should have added that I'm on macOS. I tried deleting ```volumes``` and then rerunning ```docker-compose up``` but unfortunately I'm still getting the same error. – mlan Jul 08 '21 at 15:06
  • You can copy your code into a plain text editor like Notepad and then copy from Notepad to paste into your file. – Saeed Jul 08 '21 at 20:29
  • @Saeed tried using Notepad but unfortunately didn't work either... really getting confused with this, nothing seems to fix it. – mlan Jul 09 '21 at 10:12

0 Answers0