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 use docker Dockerfile file to make your own image

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this article, the editor introduces in detail "how to use docker Dockerfile files to make your own image". The content is detailed, the steps are clear, and the details are handled properly. I hope this "how to use docker Dockerfile files to make your own image" article can help you solve your doubts.

1. Create an empty directory

$cd / home/xm6f/dev $mkdir myapp$ cd myapp/

2.vim dockerfile, the content is as follows:

# # A basic python runtime environment from python## sets the working directory workdir / app## to copy the contents of the current system folder to the container's app directory add. / app## installs the necessary dependency package run pip install-r softwares.txt## open port for access to expose 80expose 3088expose 8080expose 8066 outside the container # defines the environment variable env name hello## runs the command cmd ["python", "app.py"]

3. Installation dependency

Vim softwares.txt, the content is as follows:

Flaskredis

4.vim app.py, the content is as follows:

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)

5. Compile

$docker build-t myfirstapp.

6. Check that a new image has been generated

$docker imagesrepository tag image id created sizemyfirstapp latest 01ea1129a831 2 hours ago 699mb

7. Start image

$docker run-p 4000 purl 80 myfirstapp

You can also run it in the background:

$docker run-d-p 4000 purl 80 myfirstapp

8. Access servic

# curl http://localhost:4000hello world!hostname: a6655d0d7e74visits: cannot connect to redis, counter disabled

Or browser access service: http://192.168.1.160:4000

9. View the currently running image

$docker pscontainer id mage command created status ports names2db45cab2bb4 myfirstapp "python app.py" 2 minutes ago up 2 minutes 0.0.0.0 minutes ago up 4000-> 80/tcp elastic_wilson

10. Stop mirroring

Docker stop 2db45cab2bb4 read here, this article "how to use docker Dockerfile files to make your own image" article has been introduced, want to master the knowledge of this article still need to practice and use to understand, if you want to know more related articles, welcome to follow 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