How to pack a Flask’s Hello World page into a Docker image and send it to a remote image repository.

0. Create a Docker image

First, create the application files app.py, requirments. TXT, and a docker file.

mkdir demo && cd demo
touch Dockerfile app.py requirements.txt
Copy the code
requirments.txt
flask
redis
Copy the code
app.py
from flask import Flask
from redis import Redis, RedisError
import os

redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

app = Flask(__name__)

@app.route("/")
def hello():
    try:
        visits = redis.incr("counter")
    except RedisError:
        visits = 0

    html = "

Hello {name}!

"
\ "<b>Visits:</b> {visits}" return html.format(name=os.getenv("NAME"."world"), visits=visits) if __name__ == "__main__": app.run(host='0.0.0.0', port=80) Copy the code
Dockerfile
Based on python2.7The FROM python: 2.7 - slimSet the working directory
WORKDIR /app

Copy files from the current directory to the working directory
COPY . /app

Install the PIP library
RUN pip install --trusted-host pypi.python.org -r requirements.txt

Expose port 80
EXPOSE 80

Define environment variables
ENV NAME World

After the container is started, execute the command to run the app
CMD ["python"."app.py"]
Copy the code

With the files ready, you can start packing the image

docker build --tag=demo .
Copy the code

View a New image

docker image ls -a
Copy the code

Run the mirror

docker run -p 8000:80 demo
Copy the code

Open your browser and visit http://localhost:8000 to see Hello World.

1. Docker image warehouse

In order to facilitate the learning of Docker we can install the desktop client Docker for Mac/Docker for Windows.

Before pushing the image, we usually label the image for easy maintenance and management

Label command:

Docker tag image username# We can do thisDocker tag demo monk/demo: v1.0Copy the code
1.1 Docker Hub

Go to the official image warehouse Docker Hub registration login, push image will see your image here, public image can be shared with others to use.

Before pushing an image, you need to log in to dockerHub on terminal:

docker login
Copy the code

After pushing, we can see the image on docker Hub.

Docker push monk/demo: v1.0Copy the code
1.2 ali cloud

The speed of Docker Hub in China is relatively slow, so it is suggested to use the domestic mirror warehouse, such as Ali, which needs to manage the background container mirror service to set up the warehouse.

Switch Ali’s Docker account in the terminal

docker login --username=username registry.cn-shenzhen.aliyuncs.com
Copy the code

Ali management background explains how to login, push, and Intranet address and other operations, just follow the steps of DockerHub similar to just change ali address.

2. Mirror acceleration

Ali cloud also has a mirror accelerator, which is actually an accelerated link.

Set Prefrence > Daemon > Basic on Docker Desktop

Registy Mirrors use the accelerated address assigned to you by Ali and then apply restart Docker for Mac/Docker for Windows.

To the end.