Deployment Flow(docker part)

The deployment sequence looks as follows:

  1. Writing the application code.
  2. Creating a container image for the application.
  3. Uploading the image to a container registry in your own repository.
  4. Writing your own Kubernetes manifest that will reference the image.
  5. Executing the kubectl command that will send this manifest to the API server.

Let's recall how a new image is uploaded to Docker Hub:

For deployment, you will need the Python application that you already used in the Docker module. This is the initial version, without a file for state preservation, without a database, and without a multistage Docker image build:

from flask import Flask, Response, send_file
import os

app = Flask(__name__)

@app.route('/')
def hello():
   return f'''
   Docker is Awesome!
<pre>                   ##        .</pre>
<pre>             ## ## ##       ==</pre>
<pre>          ## ## ## ##      ===</pre>
<pre>      /""""""""""""""""\___/ ===</pre>
<pre> ~~~ (~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===-- ~~~</pre>
<pre>      \______ o          __/</pre>
<pre>        \    \        __/</pre>
<pre>         \____\______/</pre>
   '''

@app.route('/logo')
def docker_logo():
   return send_file('docker-logo.png', mimetype='image/png')

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

Now to the Dockerfile:

# Use the official Python image as the base image
FROM python:3.8

# Set the working directory within the container
WORKDIR /app

# Copy the application code into the container
COPY app.py .
COPY docker-logo.png .

# Install Flask and other dependencies
RUN pip install Flask

# Expose port 8080 to the host
EXPOSE 8080

# Define the command to run the application
ENTRYPOINT ["python", "app.py"]

Next, you need to:

  1. Define the working directory.
  2. Copy the files.
  3. Install Flask.
  4. Set port 8080, which the application will listen on.
  5. Specify that the Python file is the entrypoint.

Now you can verify that everything works by building the image and running the application locally. You are already in the terminal, in the directory where the Dockerfile is located, so you need to execute the command:

docker build . -t kub2py:1.0.0

Now check all images using the docker images command:

REPOSITORY      TAG      IMAGE ID       CREATED          SIZE
kub2py          1.0.0    692ce37d00a1   24 minutes ago   1.01GB

Run the command docker run -p 8080:8080 kub2py:1.0.0 -n app-py:

If the output looks similar to what you see above, everything is fine. The last step remains — uploading the image to a registry. For this, you need to go to Docker Hub and create a new repository for this image. After logging in to Docker Hub under your account, click the Create repository button. Name this repository kub2py and click Create.

Now all that's left is to change the tag on the local image and execute docker push. First, change the tag.

docker tag kub2py:1.0.0 ikulyk404/kub2py:1.0.0
docker images
REPOSITORY         TAG     IMAGE ID       CREATED          SIZE
Kub2py                  1.0.0   692ce37d00a1   32 minutes ago   1.01GB
ikulyk404/kub2py   1.0.0   692ce37d00a1   32 minutes ago   1.01GB

Execute docker login:

docker login -u ikulyk404                               1 ✘  16s   13:26:48
Password:
Login Succeeded

And now docker push:

docker push ikulyk404/kub2py:1.0.0                        ✔  20s   13:28:08

In the console, you will see the following:

The push refers to repository [docker.io/ikulyk404/kub2py]
c674b179e1aa: Pushed
e5d739c0bb6e: Pushed
9b55140f3e55: Pushed
974a375a56cf: Pushed
ef104393f94e: Pushed
aeffcba91ac0: Pushing [===========================================>]5.12kB
eecf0e3feddd: Pushing [===========================================>]44.21MB
6a829aee45dc: Pushing [===========================================>]19.7MB
292bbffe4917: Pushing [=======================>]  262.5MB/559.8MB
d64e22d53437: Pushing [===========================================>]187.5MB
369d2341f368: Waiting
77ad615d04d4: Waiting

If everything looks like the above, you can proceed to checking the repository on Docker Hub. Reload the page — if the output looks similar to the one below, you can move on: