Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of the steps of building, running, publishing and obtaining docker image for the first time

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1. Preface

In the past, if you were to start writing Python applications, the first step was to install the Python runtime environment on your machine, and the installation environment had to be the same as online, which was troublesome.

With Docker, you can obtain a portable Python runtime environment image from docker's official registry or other repositories without installation. You can then develop your application based on this image, which ensures that your application, dependencies, and runtime run together.

2. Build a python image

2.1.In order to build your own image, you first need to create a file called Dockerfile that defines the steps required to create the image and run container. Each instruction in Dockerfile creates a level in the mirror. When you change the Dockerfile and re-build the mirror, only the levels of those changes are rebuilt. Compared with other virtualization technologies, this is one reason why mirrors are lightweight, compact, and fast.

Create an empty directory, create a file named Dockerfile, copy and paste the following into the file and save it.

# Use an official Python runtime as a parent imageFROM python:2.7-slim# Set the working directory to / appWORKDIR / app# Copy the current directory contents into the container at / appADD. / app# Install any needed packages specified in requirements.txtRUN pip install-- trusted-host pypi.python.org-r requirements.txt# Make port 80 available to the world outside this containerEXPOSE 8 Define environment variableENV NAME World# Run app.py when the container launchesCMD ["python", "app.py"]

2.2 create requirements.txt and app.py files in the same directory as the Dockerfile file. Because of the ADD command of the Dockerfile file, the above two files will be added to the final image; because of the EXPOSE command, access to port 80 of the container can access the contents of the app.py. Note: Port 80 here refers to the port exposed by the container, not the port of the actual machine.

Requirements.txt

Flask

Redis

App.py

From flask import Flaskfrom redis import Redis, RedisErrorimport osimport socket# Connect to Redisredis = 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 = "cannot connect to Redis Counter disabled "html =" Hello {name}! "\" Hostname: {hostname} "\" Visits: {visits} "return html.format (name=os.getenv (" NAME "," world "), hostname=socket.gethostname (), visits=visits) if _ _ name__ =" _ _ main__ ": app.run (host='0.0.0.0', port=80)

2.3.Pack our application as an image and execute it in the DockerFile directory. This creates a Docker image, which we will mark with-t to give the image a friendly name.

Docker build-t friendlyhello

3. Run the image

Run the application and use-p to map the machine's port 4000 to the container's exposed port 80:

Docker run-p 4000 purl 80 friendlyhello

You can also use the curl command in shell to view the same content.

$curl http://localhost:4000Hello Worldwide Hostname: 8fc990912a14Visits: cannot connect to Redis, counter disabled

Press crtl+c to end the application

Now let's run the application in the background:

Docker run-d-p 4000UR 80 friendlyhello

View all container information

$docker container ls

CONTAINER ID IMAGE COMMAND CREATED

1fa4ab2cf395 friendlyhello "python app.py" 28 seconds ago

Now use docker container stop to end the process and use CONTAINER ID, as follows:

Docker container stop 1fa4ab2cf395

4. Publish the image

I use Aliyun's docker registry, which should be faster. First of all, you need to have an Aliyun account. Then log in to create a new warehouse and set up namespaces and other information.

Login to Aliyun's docker registry, subsequent operations need to be logged in before it can be performed.

Sudo docker login-username=admin registry.cn-hangzhou.aliyuncs.com

4.3.Mark the image. Tag is optional. If not, default is latest.

Format:

Docker tag image_name registry_url/namespace/repository_name: [tag]

For example

Docker tag friendlyhello registry.cn-hangzhou.aliyuncs.com/shuzhou/demo1:latest

View the list of local images

Docker image ls

4.4 publish Image

Docker push registry.cn-hangzhou.aliyuncs.com/shuzhou/demo1:latest

4.5 now you can run the image by executing the following command on any machine

Docker run-p 4000 purl 80 registry.cn-hangzhou.aliyuncs.com/shuzhou/demo1:latest

4.6 pull the image

Docker pull registry.cn-hangzhou.aliyuncs.com/shuzhou/demo1:latest

Summary

The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support. If you want to know more about it, please see the relevant links below.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report