Saturday 9 May 2020

Docker: build an image with numpy library

In this post, I am going to explain how to build an image with numpy library.

Step 1: Create Dockerfile with below content.

Dockerfile
FROM python:3

RUN pip3 install numpy

Step 2: Navigate to the folder where Dockerfile is located and execute below command to build the image from Dockerfile.
docker build -t my_numpi_image .
$docker build -t my_numpi_image .
Sending build context to Docker daemon   5.12kB
Step 1/2 : FROM python:3
 ---> 4f7cd4269fa9
Step 2/2 : RUN pip3 install numpy
 ---> Running in fc7102543fc6
Collecting numpy
  Downloading numpy-1.18.4-cp38-cp38-manylinux1_x86_64.whl (20.7 MB)
Installing collected packages: numpy
Successfully installed numpy-1.18.4
Removing intermediate container fc7102543fc6
 ---> b8cec2553f11
Successfully built b8cec2553f11
Successfully tagged my_numpi_image:latest

Execute the command ‘docker image ls’ to list all the docker images.
$docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
my_numpi_image      latest              b8cec2553f11        43 seconds ago      1.05GB

Step 3: Create hello.py file.
hello.py

from numpy import random

x = random.randint(100)

print(x)

Step 4: Execute below command to run the container and execute hello.py file.

docker run --rm -v $(pwd):/src my_numpi_image python3 /src/hello.py

--rm: Remove this container once the command is executed
-v $(pwd):/src: Mount current directory to /src folder.
my_numpi_image: Image to be loaded in container.
python3 /src/hello.py: Command to be executed.

$docker run --rm -v $(pwd):/src my_numpi_image python3 /src/hello.py
85
$
$docker run --rm -v $(pwd):/src my_numpi_image python3 /src/hello.py
46
$
$
$docker run --rm -v $(pwd):/src my_numpi_image python3 /src/hello.py
79



Previous                                                    Next                                                    Home

No comments:

Post a Comment