First, preface
Containers use sandbox mechanisms to isolate each other, and the advantage is that each application deployed in the container is independent, running independently, providing greater security. This article focuses on Python applications (Django) running in Docker containers, writing dockerfile for mirroring build automation, and Docker artifact compose.
II. Preparation of Dockerfile documents
The python images downloaded on the website are relatively streamlined, and Web application dependent dependencies need to be installed on their own. Writing dockerfile allows you to automate when you build the 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-devcopycopy requirements.txt /codeRUN pip install-r/code/requirements.txt-i pypi.douban.com/simpleworkdir /code CMD ["/bin/bash", "run.sh"]
From : A very important command in Dockerfile, which is to specify a base image for the build process. For example, the above specified python3.6.4 as the base image, all subsequent operations will be based on this image as a basis for customization, if not present, will be downloaded from the official website. The from must be the Dockerfile first command.
RUN: Dockerfile executes the core part of the command, executing the parameters during the build of the image.
Copy : copies the file. COPY < Source path > < destination path >
Workdir: working directory, if not present, will automatically help you create.
CMD: container Start command, Docker is not a virtual machine, 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 C MD directive is the start command used to specify the default container master process. If Docker run specifies a command parameter, the CMD here will not work. For example, Docker run-it-name Redis Docker.io/redis/bin/bash, the boot container does not execute cmd in Dockerfile because the Docker run has specified the command parameter/bin/bash.
Third, build the image
Docker build[options] Context Path | Url
[options]: Typically directives include-T, which specifies the name of an image. -f Specifies the context path of the dockfile.
• Context Path | URL: The context path, if only a small dot "." represents the current directory.
Docker Bulid-t WebTest. The command builds the image and returns a mirror ID 1DFA2905EFAC after the build is completed.
ls app Dockerfile requirements.txt run. SH -6/6 : Cmd/bin/bash run. SH inch 0bd29255c648---> 1dfa2905efacremoving Intermediate container 0bd29255c648successfully built 1DFA2905EFAC
App:django Project
Dockerfile
Requirements.txt is the Python library required for the project to run
Djangodjangorestframeworkpydespymysqlredisrequestspymssqlpyodbcparamikopsutil
View Code
run.sh is the shell script that needs to be invoked when the container is run
0.0. 0.0:8000
View Code Iv. operation of the container
Start the container and run the image you just built.
Docker run-it-p 6500:8000-v/home/code/webtest:/code--name Web--restart always--privileged=true Web
[Root@centos webtest]# Docker Run-it-p6500:8000-v/home/code/webtest:/code--name Web--restart always--privileged=truewebperforming System checks ... System Check identified no issues (0silenced). you have theUnapplied migration (s). Your project may isn't work properlyuntilYou apply the Migrations forapp (s): admin, Auth, contenttypes, sessions. Run'python manage.py Migrate'To apply them. August the,2018- the: About:WuyiDjango version2.1, using Settings'shihangtool.settings'starting Development Server at http://0.0.0.0:8000/Quit the server with Control-c.
-P: Mapping container 8000 ports to host 6500
-V: The Directory of the host/home/code/webtest mapped to the container's directory/code
--name: Give the container a name web,webtest is the image we just built
--restart:always always restarts when the container exits
--privileged=true: Required permissions to execute files in the container
Input ip:6500/home/ordersettle-k8s/
Run successfully!
Wu, Compose
Do you feel that the above operation is too complex, if you can automate the deployment is good. Don't worry, compose can help you.
1. Introduction:
Compose is the official Docker Open source project for quick orchestration of Docker clusters. Compose defines and runs one or more containers through the Docker-compose.yml file. is an upgraded version of FIG.
2. Installation:
Compose is written in Python and invokes the API provided by Docker to manage the container. So can be installed through the Python management tool pip
Pip Install Docker-compose
3. Writing the Docker-compose.yml file
This is the DOCKER-COMPOSE.YML main template format
' 3 ' Services: web1: build:. Image:web1 ports: "7500:8000" volumes: -/ home/code/webtest:/Code true restart:always
4. Run the Compose project
Run command docker-compose up
[Root@centos webtest]# docker-Compose upcreating Network"Webtest_default"With the default driverbuilding web1step1/6: From Python3.6.4-Dev--->Ca46b1ed99abstep2/6: COPY app/Code--->f59b9540f8abremoving Intermediate Container e987c66b51f5step3/6: COPY requirements.txt/Code--->2095b64882acremoving Intermediate Container e3099b386727 ... .
After the run is complete, Docker PS looks at the container web1 that just started and is already running
[Root@centos ~]# DockerPSCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS nameseeab6b5a993b Web1"/bin/bash run.sh"About a minute ago -Seconds0.0.0.0:7500-8000/TCP webtest_web1_15fb51ce5a51c Web"/bin/bash run.sh" atHours ago up about an hour0.0.0.0:6500-8000/TCP Web
5. Summary
Compose build the image, run the container two steps in a yml file to configure for automated deployment. The above example may not be obvious enough, if you are running multiple containers, you will find that compose can save you a lot of time.