how to successfully run docker image as container

Viewed 281

Below my docker file,

FROM python:3.9.0
ARG WORK_DIR=/opt/quarter_1
RUN apt-get update && apt-get install cron -y && apt-get install -y default-jre
# Install python libraries
COPY requirements.txt /tmp/requirements.txt
RUN pip install --upgrade pip && pip install -r /tmp/requirements.txt
WORKDIR $WORK_DIR
EXPOSE 8888
VOLUME /home/data/quarter_1/
# Copy etl code
# copy code on container under your workdir "/opt/quarter_1"
COPY . .

I tried to connect to the server then i did the build with docker build -t my-python-app . when i tried to run the container from a build image i got nothing and was not able to do it.

  docker run -p 8888:8888 -v /home/data/quarter_1/:/opt/quarter_1 image_id

enter image description here work here is opt

1 Answers

Update based on comments

If I understand everything you've posted correctly, my suggestion here is to use a base Docker Jupyter image, modify it to add your pip requirements, and then add your files to the work path. I've tested the following:

Start with a dockerfile like below

FROM jupyter/base-notebook:python-3.9.6

COPY requirements.txt /tmp/requirements.txt
RUN pip install --upgrade pip && pip install -r /tmp/requirements.txt

COPY ./quarter_1 /home/jovyan/quarter_1

Above assumes you are running the build from the folder containing dockerfile, "requirements.txt", and the "quarter_1" folder with your build files.

Note "home/joyvan" is the default working folder in this image.

Build the image

docker build -t biwia-jupyter:3.9.6 .

Start the container with open port to 8888. e.g.

docker run -p 8888:8888 biwia-jupyter:3.9.6

Connect to the container to access token. A few ways to do but for example:

docker exec -it CONTAINER_NAME bash
jupyter notebook list

Copy the token in the URL and connect using your server IP and port. You should be able to paste the token there, and afterwards access the folder you copied into the build, as I did below.

Jupyter screenshot

If you are deploying the image to different hosts this is probably the best way to do it using COPY/ADD etc., but otherwise look at using Docker Volumes which give you access to a folder (for example quarter_1) from the host, so you don't constantly have to rebuild during development.

Second edit for Python 3.9.0 request

Using the method above, 3.9.0 is not immediately available from DockerHub. I doubt you'll have much compatibility issues between 3.9.0 and 3.9.6, but we'll build it anyway. We can download the dockerfile folder from github, update a build argument, create our own variant with 3.9.0, and do as above.

Assuming you have git. Otherwise download the repo manually.

  1. Download the Jupyter Docker stack repo

    git clone https://github.com/jupyter/docker-stacks

  2. change into the base-notebook directory of the cloned repo

    cd ./base-notebook

  3. Build the image with python 3.9.0 instead

    docker build --build-arg PYTHON_VERSION=3.9.0 -t jupyter-base-notebook:3.9.0 .

  4. Create the version with your copied folders and 3.9.0 version from the steps above, replacing the first line in the dockerfile instead with:

    FROM jupyter-base-notebook:3.9.0

I've tested this and it works, running Python 3.9.0 without issue.

There are lots of ways to build Jupyter images, this is just one method. Check out docker hub for Jupyter to see their variants.

Related