Build, Run and Push Docker Images with Flask App.

Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications, whether on laptops, data center VMs, or the cloud.

I wanted to give a try to docker. I’m using Arch linux so I used pacman to install the docker. I’m sharing a few links here which might help someone.

1. Installation

Official Docker Documentation : https://docs.docker.com/
Download Docker for Windows/Linux/Mac : https://docs.docker.com/get-docker/
Demo Flask Repo with Dockerfile : https://github.com/codetarsier/sharktooth-flask-docker
SharktoothFlask Docker Image : https://hub.docker.com/r/codetarsier/sharktooth-flask

Docker installation for Arch Linux.

If people are coming from linux like Arch/Manjaro then this will help you. First I used SNAP store to install the docker then I found few commands were not working properly. Follow the below steps to install Docker on Arch.

$ sudo pacman -S docker

The above command will ask you to start the installation by pressing [Y/n] just press Y and it will take some time to install the dependency packages along with docker.
After the completion of installation, start and enable the docker service.

$ sudo systemctl start docker
$ sudo systemctl enable docker

2. Verify the Installation

$ sudo docker version
The above command will return the status. If it is not then debug. :P

Let’s test the docker image and run

$ docker run docker/whalesay cowsay Hello there!
Status: Downloaded newer image for docker/whalesay:latest
 ______________
< Hello there! >
--------------
    \
     \
      \     
                    ##        .            
              ## ## ##       ==            
           ## ## ## ##      ===            
       /""""""""""""""""___/ ===        
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
       \______ o          __/            
        \    \        __/             
          \____\______/   

You can try Hello World Image as well.

$ sudo docker run hello-world

3. Writing Flask App.

1. Create new folder.

$ mkdir sharktooth-flask
$ cd sharktooth-flask

2. Create app.py file and add the following code.

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def hello_from_shark():
    return render_template('sharktooth_hello.html')

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

3. Create templates folder and add sharktooth_hello.html file with following content

templates/sharktooth_hello.html

Shark and Whale discussing
<img src="https://i.pinimg.com/564x/89/39/5c/89395c4beb73dfe865f4f49a3e526439.jpg">

4. Create requirements.txt file

Flask==1.1.2

5. Create Dockerfile file and make sure it doesn’t have any file extension like .txt

FROM ubuntu:latest
MAINTAINER Codetarsier "writeto.ashishtiwari@gmail.com"
RUN apt-get update -y
RUN apt-get install -y python3-pip python3-dev build-essential
COPY . /app
WORKDIR /app
RUN echo $(ls -1 /app)
RUN pip3 install -r requirements.txt
ENTRYPOINT ["python3"]
CMD ["app.py"]

Obviously you have to user your name and email.

Now it is time to build your first docker image.

4. Build your docker image

Make sure that you are in the project folder.

$ sudo docker build -t sharktooth-flask:latest .

List available docker images on your machine,

$ sudo docker images

Run your docker image

$ sudo docker run -d -p 5000:5000 sharktooth-flask

Check running process

$ sudo docker ps -a

Open browser and visit http://localhost:5000 and flask app will be runnning.

5. Push Docker Image to Your Account

1. Create your docker account first at https://hub.docker.com/signup/

2. Login to your docker account

$ export DOCKER_ID_USER='codetarsier'
$ sudo docker login

3. Tag your image.

$ sudo docker tag sharktooth-flask codetarsier/sharktooth-flask

4. Push your image.

$ sudo docker push codetarsier/sharktooth-flask:latest

If all goes well, visit your docker hub profile and look for the newly pushed image.
https://hub.docker.com/u/{your-account-name}

My docker hub profile : https://hub.docker.com/u/codetarsier




0 Comments:

Post a Comment