I'm trying to write a python file in json within a Docker container. I'm using Jupyter Notebook in the container to load some urls from the NBA API, and eventually I want to write it to a NoSql database somewhere but for now I want the files to be saved inside the volume of the Docker container.. Unfortunately, when I run this Python code, I get the error
FileNotFoundError: [Errno 2] No such file or directory: '/usr/src/app/nba-matches/0022101210.json'
Here is the Python code... I'm running
from run import *
save_nba_matches(['0022101210'])
on Jupyter notebook.
The Python script:
import os
import urllib.request
import json
import logging
BASE_URL = 'https://cdn.nba.com/static/json/liveData/playbyplay/playbyplay_'
PATH = os.path.join(os.getcwd(), 'nba-matches')
LOGGER = logging.getLogger()
LOGGER.setLevel(logging.INFO)
def save_nba_matches(match_ids):
for match_id in match_ids:
match_url = BASE_URL + match_id + '.json'
json_file = os.path.join(PATH, match_id+'.json')
web_url = urllib.request.urlopen(
match_url)
data = web_url.read()
encoding = web_url.info().get_content_charset('utf-8')
json_object = json.loads(data.decode(encoding))
with open(json_file, "w+") as f:
json.dump(json_object, f)
logging.info(
'JSON dumped into path: [' + json_file + ']')
Dockerfile:
FROM python:3
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["jupyter", "notebook", "--port=8888", "--no-browser", "--ip=0.0.0.0", "--allow-root"]
and finally, docker-compose.yml
version: '3.4'
services:
sports-app:
build: .
volumes:
- '.:/usr/src/app'
ports:
- 8888:8888
nba-matchesdirectory exist in the mounted host directory? You'll get that error if it doesn't. (Consider mounting only that directory in the Compose setup so that the application code in the image isn't overwritten by the bind mount.)