In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "python application containerization method of Docker practice". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. Preface
Containers use a sandbox mechanism to isolate each other. The advantage is that each application deployed in the container does not affect each other, runs independently, and provides higher security.
Second, write dockerfile files
The python image downloaded from the official website is relatively simple, and the dependencies related to web applications still need to be installed on your own. Writing dockerfile allows you to automate the construction of an image. Examples are as follows:
From python:3.6.4run mkdir / code\ & & apt-get update\ & & apt-get-y install freetds-dev\ & & apt-get-y install unixodbc-devcopy app / code copy requirements.txt / coderun pip install-r / code/requirements.txt-I https://pypi.douban.com/simpleworkdir / codecmd ["/ bin/bash", "run.sh"]
A very important command in from:dockerfile is to specify a base image for the build process. For example, python3.6.4 is specified as the basic image above, and all subsequent operations will be customized based on this image. If it does not exist, it will be downloaded from the official website. From must be the first command of dockerfile.
Run: dockerfile executes the core part of the command, executing parameters during the process of building the image.
Copy: copy the file. Copy
Workdir: working directory. If it doesn't exist, it will automatically create it for you.
Cmd: container startup command. Docker is either a virtual machine or a container is a process. Since it is a process, you need to specify the program and parameters to run when you start the container. The cmd directive is the startup command that specifies the default container main process. If docker run specifies command parameters, the cmd here will not work. For example, docker run-it-name redis docker.io/redis / bin/bash, the startup container does not execute cmd in dockerfile because docker run has specified the command parameter / bin/bash.
Third, build an image
Docker build [options] context path | url
[options]: usually the instruction includes-t, which specifies the name of the image. -f specifies the context path of the dockfile.
Context path | url: context path, if there is only one dot. Represents the current directory.
Docker bulid-t webtest. Command to build an image named webtest, and return an image id1dfa2905efac when the build is complete.
[root@centos webtest] # lsapp dockerfile requirements.txt run.sh [root@centos webtest] # docker build-t webtest. .removing intermediate container 9c510e88e659step 6ram 6: cmd / bin/bash run.sh--- > running in 0bd29255c648Murray-> 1dfa2905efacremoving intermediate container 0bd29255c648successfully built 1dfa2905efac
App:django project
Dockerfile
Requirements.txt is the python library needed for the project to run.
Django
Djangorestframework
Pydes
Pymysql
Redis
Requests
Pymssql
Pyodbc
Paramiko
Psutil
Run.sh is the shell script that needs to be called when running the container
Python / code/app/manage.py runserver 0.0.0.0:8000
Start the container and run the image you just built.
Docker run-it-p 6500 name web 8000-v / home/code/webtest:/code-- restart always-- privileged=true webtest
[root@centos webtest] # docker run-it-p 6500 home/code/webtest:/code-- name web-- restart always-- privileged=true webtestperforming system checks...system check identified no issues (0 silenced). You have 15 unapplied migration (s). Your project may not work properly until you apply the migrations for app (s): admin, auth, contenttypes, sessions.run 'python manage.py migrate' to apply them.august 09, 2018-09:56:51django version 2.1, using settings' shihangtool.settings'starting development server at http://0.0.0.0:8000/quit the server with control-c.
-p: map port 8000 of the container to host 6500
-v: the host's directory / home/code/webtest maps to the container's directory / code
-- name: name the container web,webtest is the image we just built
-- restart:always container always restarts when it exits
-- privileged=true: permissions required to execute files in the container
Enter ip:6500/home/ordersettle-k8s/
Run successfully!
5. Compose
If you think the above operation is too complex, it would be nice if you could deploy it automatically. Don't worry. Compose can help you.
1. Brief introduction:
Compose is the official docker open source project for rapid orchestration of docker clusters. Compose defines and runs one or more containers through a docker-compose.yml file. It's an upgraded version of fig.
two。 Installation:
Compose is written through python and calls the api provided by docker to manage the container. So can be installed through the python management tool pip
Pip install docker-compose
3. Write docker-compose.yml files
This is the docker-compose.yml master template format
Version: '3'services: web1: build: .image: web1 ports:-"7500 web1 8000" volumes:-/ home/code/webtest:/code privileged: true restart: always
4. Run the compose project
Put the dockerfile, requirements.txt, docker-compose.yml, app folders in the same directory and run the command docker-compose up
[root@centos webtest] # docker-compose upcreating network "webtest_default" with the default driverbuilding web1step 1 code 6: from python3.6.4-dev-- > ca46b1ed99abstep 2 copy requirements.txt 6: copy app / code-- > f59b9540f8abremoving intermediate container e987c66b51f5step 3 code 6: copy requirements.txt / code-- > 2095b64882acremoving intermediate container e3099b386727.
After the run is complete, docker ps looks at the container web1 that has just been started and is already running.
[root@centos ~] # docker pscontainer id image command created status ports nameseeab6b5a993b web1 "/ bin/bash run.sh" about a minute ago up 59 seconds 0.0.0.0 about a minute ago up 7500-> 8000/tcp webtest_web1_15fb51ce5a51c webtest "/ bin/bash run.sh" 23 hours ago up about an hour 0.0.0.0 python 6500-> 8000/tcp web "method of containerization of python applications in Docker practice" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.