Showing posts with label image. Show all posts
Showing posts with label image. Show all posts

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

docker push: push the image

Using docker push command you can push the image to docker hub.

Prerequisite
Create an account in https://hub.docker.com

Follow below step-by-step procedure to push the image to docker hub.

Step 1: Create an image.

Create a directory ‘flask_hello_world’.

Navigate to flask_hello_world and create Dockerfile.
$mkdir flask_hello_world
$
$cd flask_hello_world/
$
$touch Dockerfile
$ls
Dockerfile

Update Dockerfile with below content.

Dockerfile
FROM ubuntu

RUN apt-get update

RUN apt-get install -y python python-setuptools python-dev build-essential python-pip python-mysqldb

RUN pip install flask

COPY app.py /opt/app.py

ENTRYPOINT FLASK_APP=/opt/app.py flask run --host=0.0.0.0

Create a file app.py in the folder where Dockerfile is located.

app.py
import os
from flask import Flask
app = Flask(__name__)

@app.route("/")
def main():
    return "Hello World"

@app.route('/welcome')
def welcome():
    return 'Welcome to Docker Container'

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8080)

$ls
Dockerfile app.py

Execute the command ‘docker build -t {docker_user_id}/flask_hello_world .’ from the directory where Dockerfile is located. Give the docker_user_id from your docker hub account.

$docker build -t harikrishna553/flask_hello_world .
Sending build context to Docker daemon  3.072kB
Step 1/6 : FROM ubuntu
 ---> 94e814e2efa8
Step 2/6 : RUN apt-get update
 ---> Using cache
 ---> 93102f1bf79f
Step 3/6 : RUN apt-get install -y python python-setuptools python-dev build-essential python-pip python-mysqldb
 ---> Using cache
 ---> c04fb08e8e87
Step 4/6 : RUN pip install flask
 ---> Running in fc7a5cc0eb51
Collecting flask
  Downloading https://files.pythonhosted.org/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94kB)
Collecting click>=5.1 (from flask)
  Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
Collecting Werkzeug>=0.15 (from flask)
  Downloading https://files.pythonhosted.org/packages/d1/ab/d3bed6b92042622d24decc7aadc8877badf18aeca1571045840ad4956d3f/Werkzeug-0.15.5-py2.py3-none-any.whl (328kB)
Collecting itsdangerous>=0.24 (from flask)
  Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting Jinja2>=2.10.1 (from flask)
  Downloading https://files.pythonhosted.org/packages/1d/e7/fd8b501e7a6dfe492a433deb7b9d833d39ca74916fa8bc63dd1a4947a671/Jinja2-2.10.1-py2.py3-none-any.whl (124kB)
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10.1->flask)
  Downloading https://files.pythonhosted.org/packages/fb/40/f3adb7cf24a8012813c5edb20329eb22d5d8e2a0ecf73d21d6b85865da11/MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl
Installing collected packages: click, Werkzeug, itsdangerous, MarkupSafe, Jinja2, flask
Successfully installed Jinja2-2.10.1 MarkupSafe-1.1.1 Werkzeug-0.15.5 click-7.0 flask-1.1.1 itsdangerous-1.1.0
Removing intermediate container fc7a5cc0eb51
 ---> 4f0eb1453957
Step 5/6 : COPY app.py /opt/app.py
 ---> fe4dd957f7a5
Step 6/6 : ENTRYPOINT FLASK_APP=/opt/app.py flask run --host=0.0.0.0
 ---> Running in 8d6d7a91eb12
Removing intermediate container 8d6d7a91eb12
 ---> 4e76840aaf25
Successfully built 4e76840aaf25
Successfully tagged harikrishna553/flask_hello_world:latest

Execute the command ‘docker images’ to see the list of all images.
$docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
krishna/flask_hello_world   latest              4e76840aaf25        45 seconds ago      467MB


Step 2: Login to dockerhub.

Open terminal and execute the command ‘docker login’.
$docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: *********
Password: 
Login Succeeded

Step 3: Push the image. By executing the command ‘docker push imageName’.
$docker push krishna/flask_hello_world
The push refers to repository [docker.io/{my_user_id}/flask_hello_world]
043f1d9747fa: Pushed 
396547e69703: Pushed 
8c78bede60e6: Pushed 
09c1a55a1045: Pushed 
b57c79f4a9f3: Mounted from library/ubuntu 
d60e01b37e74: Mounted from library/ubuntu 
e45cfbc98a50: Mounted from library/ubuntu 
762d8e1a6054: Mounted from library/ubuntu 
latest: digest: sha256:2c155af2c91d80628c7a19ce51ae842e77e4d981e0d9cad092b51c60f27014f8 size: 1993

Once you login to the docker hub, go to your profile, you can see the repository.



Previous                                                    Next                                                    Home

Docker: Python: Flask: Build an image using Docker file

Step 1: Create a directory ‘flask_hello_world’.

Step 2: Navigate to flask_hello_world and create Dockerfile.
$mkdir flask_hello_world
$
$cd flask_hello_world/
$
$touch Dockerfile
$ls
Dockerfile

Step 3: Update Dockerfile with below content.

Dockerfile
FROM ubuntu

RUN apt-get update

RUN apt-get install -y python python-setuptools python-dev build-essential python-pip python-mysqldb

RUN pip install flask

COPY app.py /opt/app.py

ENTRYPOINT FLASK_APP=/opt/app.py flask run --host=0.0.0.0

Step 4: Create a file app.py in the folder where Dockerfile is located.

app.py
import os
from flask import Flask
app = Flask(__name__)

@app.route("/")
def main():
    return "Hello World"

@app.route('/welcome')
def welcome():
    return 'Welcome to Docker Container'

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8080)

$ls
Dockerfile   app.py

Step 5: Execute the command ‘docker build -t flask_hello_world .’ from the directory where Dockerfile is located.
$docker build -t flask_hello_world .
Sending build context to Docker daemon  3.072kB
Step 1/6 : FROM ubuntu
 ---> 94e814e2efa8
Step 2/6 : RUN apt-get update
 ---> Using cache
 ---> 93102f1bf79f
Step 3/6 : RUN apt-get install -y python python-setuptools python-dev build-essential python-pip python-mysqldb
 ---> Using cache
 ---> c04fb08e8e87
Step 4/6 : RUN pip install flask
 ---> Running in a57ed8d799a6
Collecting flask
  Downloading https://files.pythonhosted.org/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94kB)
Collecting click>=5.1 (from flask)
  Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
Collecting Werkzeug>=0.15 (from flask)
  Downloading https://files.pythonhosted.org/packages/d1/ab/d3bed6b92042622d24decc7aadc8877badf18aeca1571045840ad4956d3f/Werkzeug-0.15.5-py2.py3-none-any.whl (328kB)
Collecting itsdangerous>=0.24 (from flask)
  Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting Jinja2>=2.10.1 (from flask)
  Downloading https://files.pythonhosted.org/packages/1d/e7/fd8b501e7a6dfe492a433deb7b9d833d39ca74916fa8bc63dd1a4947a671/Jinja2-2.10.1-py2.py3-none-any.whl (124kB)
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10.1->flask)
  Downloading https://files.pythonhosted.org/packages/fb/40/f3adb7cf24a8012813c5edb20329eb22d5d8e2a0ecf73d21d6b85865da11/MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl
Installing collected packages: click, Werkzeug, itsdangerous, MarkupSafe, Jinja2, flask
Successfully installed Jinja2-2.10.1 MarkupSafe-1.1.1 Werkzeug-0.15.5 click-7.0 flask-1.1.1 itsdangerous-1.1.0
Removing intermediate container a57ed8d799a6
 ---> 080f962d64bf
Step 5/6 : COPY app.py /opt/app.py
 ---> 217efc0c9c63
Step 6/6 : ENTRYPOINT FLASK_APP=/opt/app.py flask run --host=0.0.0.0
 ---> Running in 272c2c703d4a
Removing intermediate container 272c2c703d4a
 ---> a04372b9c2d0
Successfully built a04372b9c2d0
Successfully tagged flask_hello_world:latest

Step 6: Execute the command ‘docker images’ to see list of images.
$docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
flask_hello_world               latest              a04372b9c2d0        55 seconds ago      467MB
mysql                           latest              62a9f311b99c        2 weeks ago         445MB
jboss/wildfly                   latest              4e440230e01d        2 months ago        748MB

You can see that ‘flask_hello_world’ image is available.

Step 7: Run the container by executing the below command.
docker run -p 1234:5000 flask_hello_world
$docker run -p 1234:5000 flask_hello_world
 * Serving Flask app "/opt/app.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
172.17.0.1 - - [30/Aug/2019 21:47:34] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [30/Aug/2019 21:47:34] "GET /favicon.ico HTTP/1.1" 404 –

Open the url ‘http://localhost:1234/’ in browser, you can see below kind of screen.

Open the url ‘http://localhost:1234/welcome’ in browser, you can see below kind of screen.





Previous                                                    Next                                                    Home

How to delete a docker image?

‘docker rmi {imageName}’ or ‘docker rmi {imageId}’ is used to remove an image.

$docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
postgres            latest              79db2bf18b4a        2 months ago        312MB
busybox             latest              e4db68de4ff2        2 months ago        1.22MB
jboss/wildfly       latest              4e440230e01d        2 months ago        748MB
tagsapp             1                   17ff919cfdbd        4 months ago        88.9MB
tagsapp             2                   17ff919cfdbd        4 months ago        88.9MB
tagsapp             latest              a1868b148d8d        4 months ago        88.9MB
ubuntu              latest              94e814e2efa8        5 months ago        88.9MB
jenkins             latest              cd14cecfdb3a        13 months ago       696MB

Let’s remove the image busybox using name.
$docker rmi busybox
Untagged: busybox:latest
Untagged: busybox@sha256:7a4d4ed96e15d6a3fe8bfedb88e95b153b93e230a96906910d57fc4a13210160
Deleted: sha256:e4db68de4ff27c2adfea0c54bbb73a61a42f5b667c326de4d7d5b19ab71c6a3b
Deleted: sha256:6194458b07fcf01f1483d96cd6c34302ffff7f382bb151a6d023c4e80ba3050a
$
$
$docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
postgres            latest              79db2bf18b4a        2 months ago        312MB
jboss/wildfly       latest              4e440230e01d        2 months ago        748MB
tagsapp             1                   17ff919cfdbd        4 months ago        88.9MB
tagsapp             2                   17ff919cfdbd        4 months ago        88.9MB
tagsapp             latest              a1868b148d8d        4 months ago        88.9MB
ubuntu              latest              94e814e2efa8        5 months ago        88.9MB
jenkins             latest              cd14cecfdb3a        13 months ago       696MB

Let’s remove postgres image using id.

$docker rmi 79db2bf18b4a
Untagged: postgres:latest
Untagged: postgres@sha256:fb0ff33f663bcb5bc4962cac44ccffb57f4523c1fc2a17b0504db1c07c1d2666
Deleted: sha256:79db2bf18b4a48fdc4f89b5ca84490ff842d0b45d8dc5e7e26927c6c7ae5e7e5
Deleted: sha256:c82a8733a1f099314f7b9709c75f493405613996535831422d4e2c2938e607a7
Deleted: sha256:174f239b700ffbf93906b00df58a66c15d6dd213d49391f99fbab7e9b7e9be75
Deleted: sha256:467439ba0a3fef8dc47901cb8ceb3136c9d9bbad759a307187e44a96ec1ef77b
Deleted: sha256:845279ff221d47eec7f400fff8803f5c2474f75118dc88e30d49ca2f66aef942
Deleted: sha256:1e5fa5a8e5cbe62531a6b9a0cfbbb19ef6ac1b2f08d3922b71feda94092a310c
Deleted: sha256:4b439fe1793670eb57213c1d3f8e1bfbcae89cc6f68b8b84d085f362aeb09ded
Deleted: sha256:cfac349bd70ab8736344af4d893fdb6cb339447282d465f4bdda80bce05863aa
Deleted: sha256:dae9c54767816fa8bdfb009181f19f5a0113e3c5684dacdc410cf38039e4d8d6
Deleted: sha256:5e0556d2cf6fc334e058bed3813fae21d0eb25b0da3a77f6080418055d5cd414
Deleted: sha256:1533bc51c26b7f13702b71d95e4949927e4f2f37632a77d9d6c2c9ded93495ce
Deleted: sha256:83b55c6a2862401e578cce4865006ede988b3538576600b2087e89912560bdbb
Deleted: sha256:3a42b7f34a8bec13305bd693cbe4522822707faadb5c79c8af1ec652aa0a3ea3
Deleted: sha256:bf6a0011068b5cf64a5dbca2de889006f2117bd0cfd828106012e4b9328d0fef
Deleted: sha256:cf5b3c6798f77b1f78bf4e297b27cfa5b6caa982f04caeb5de7d13c255fd7a1e
$
$
$docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
jboss/wildfly       latest              4e440230e01d        2 months ago        748MB
tagsapp             1                   17ff919cfdbd        4 months ago        88.9MB
tagsapp             2                   17ff919cfdbd        4 months ago        88.9MB
tagsapp             latest              a1868b148d8d        4 months ago        88.9MB
ubuntu              latest              94e814e2efa8        5 months ago        88.9MB
jenkins             latest              cd14cecfdb3a        13 months ago       696MB

Note
You must stop and delete all the dependent containers to remove an image.

Previous                                                    Next                                                    Home