Saturday, 9 May 2020

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

No comments:

Post a Comment