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

How to build, run, publish, and obtain docker images

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, the editor will share with you how to build, run, release and obtain docker images. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

1. Preface

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 worldview 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 above is all the content of the article "how to build, run, publish and get docker Images". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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

Development

Wechat

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

12
Report