In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What this article shares with you is about how to make the first docker image. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
First, create a new directory to store the files we need to make the image.
Go to the new directory and execute touch Dockerfile to create a Dockerfile file. Dockerfile defines the environment that the container needs to run, network ports, disk resources, commands to be executed, and so on.
Copy the following to Dockerfile
# 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 / appCOPY. / 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 808 Define environment variableENV NAME World# Run app.py when the container launchesCMD ["python", "app.py"]
Execute the command touch requirements.txt in the new directory to copy the following
FlaskRedis
Execute the command touch app.py in the new directory to copy the following
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=8081)
Execute the command docker build-- tag=helloworld:v0.0.1. Build your first docker image and don't forget the last one of the command. .
Use the following command to view the image you just made
# docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEhelloworld v0.0.1 f471662fe76e 2 minutes ago 131MB
The image starts a simple web service through the Python code. Let's start running your image.
# docker run-p 8099 app 8081 helloworld:v0.0.1 * Serving Flask app "app" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on http://0.0.0.0:8081/ (Press CTRL+C to quit)
The output above shows that your docker image helloworld starts successfully. You can test whether the container is running normally with the curl command. Docker run-p actually maps the local port 8099 to the container port 8081.
# curl http://localhost:8099Hello World!Hostname: a64e25c2a522Visits: cannot connect to Redis, counter disabled
You can view the container in which the current machine is running with the following command
# docker container lsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa64e25c2a522 helloworld:v0.0.1 "python app.py" 7 minutes ago Up 7 minutes 0.0.0.0 minutes 8099-> 8081/tcp frosty_newton is how to create the first docker image The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.