pip freeze > requirements.txt Command to package the dependent package list of the project docker -v and docker-vompose -v Command to see if the installation is successful .Docker Allows you to build an image through a configuration file in text format , The default name is Dockerfile
# from Docker Pull out the warehouse with Python3.7 Of Linux Environmental Science
FROM python:3.7
# Set up python environment variable
ENV PYTHONUNBUFFERED 1
# These two lines are installed in the system clock MySQL The connectors
RUN apt-get update
RUN apt-get install python3-dev default-libmysqlclient-dev -y
# establish code Folder and set it as the working directory
RUN mkdir /code
WORKDIR /code
# to update pip
RUN pip install pip -U
# take requirements.txt Copy... To container code Catalog
ADD requirements.txt /code/
# Installation Library
RUN pip install -r requirements.txt
# Copy the current directory to the container code Catalog
ADD . /code/Understand these Docker The key to instruction , Keep in mind that the environment in the container is isolated from the host , The core problem is to find out which operations are targeted at the host , Which operations are for containers .
FROM python:3.7 The instruction pulls a containing from the warehouse python 3.7 Of Linux Operating system environment (Linux Version is Debian).
RUN and WORKDIR Instructions are for containers , The function is to In the container Create directory 、 And set it to the working directory . Be careful The host machine There is no such Directory .
ADD The command appears twice .ADD requirements.txt /code/ This means that the current directory of the host computer ( namely Dockerfile In the directory ) Of requirements.txt Copy the file to the container /code Directory .ADD . /code/ It means to copy all the contents of the current directory to the container /code/ Catalog , Pay attention to the one in the middle spot .
version: "3"
services:
app:
restart: always
build: .
command: bash -c "python3 manage.py collectstatic --no-input && python3 manage.py migrate && gunicorn --timeout=30 --workers=4 --bind :8000 django_app.wsgi:application"
volumes:
- .:/code
- static-volume:/code/collected_static
expose:
- "8000"
depends_on:
- db
networks:
- web_network
- db_network
db:
image: mysql:5.7
volumes:
- "./mysql:/var/lib/mysql"
ports:
- "3307:3306"
restart: always
environment:
- MYSQL_ROOT_PASSWORD=mypassword
- MYSQL_DATABASE=django_app
networks:
- db_network
nginx:
restart: always
image: nginx:latest
ports:
- "8001:8000"
volumes:
- static-volume:/code/collected_static
- ./config/nginx:/etc/nginx/conf.d
depends_on:
- app
networks:,
- web_network
networks:
web_network:
driver: bridge
db_network:
driver: bridge
volumes:
static-volume:version representative docker-compose.yml Version of , The latest version is 3, No need to change it .
On the whole , We define three Containers , Namely app、db、 and nginx, Containers communicate with each other through defined ports . Two are defined The Internet , Namely web_network and db_network, Only containers under the same network can communicate with each other . Different networks are isolated , Even with the same port , No communication . Defined a Data volume static-volume. Data volume is very suitable for multiple containers to share the same data , You can see app and nginx Used it .expose and ports Can expose the port of the container , The difference is that expose Only expose to other containers , and ports Will be exposed to other containers and hosts .
Let's analyze it in detail :
Defined a name app The container of . The following content is app Configuration of the container :
restart : In addition to normal operation , The container will restart at any time , For example, encounter bug、 Process breakdown 、docker Restart, etc .build : Specify an include Dockerfile The path of , And through this Dockerfile To build a container image . Watch that. "." , Represents the current directory .command : Commands to be executed when the container runs . Here is the familiar running development server .volumes : volume , This is a very important concept . As mentioned earlier, the container is completely isolated from the host , But sometimes it needs to be connected ; For example, we developed Django Project code is often updated , And the update also depends on, such as Git Procedures like that , It's inconvenient to operate in a container . So there is volume , It defines the mapping between the host and the container :"." Indicates the current directory of the host ,":" Separator ,"/code" Represents the directory in the container . That is, the current directory and container of the host /code The directory is connected , Of the current directory of the host Django When code is updated , In container /code The code in the directory has been updated accordingly . It's kind of like punching a hole in a container , To some extent practical and Isolation, It's a compromise . Strictly speaking , What we use here .:/code Not at all volume , But is called mount , There is a difference between the two , It's just docker-compose Allows mounting to be written to the configuration of the volume .
expose: Expose the container 8000 Ports are accessible to other containers , The host and the outside world cannot access networks: Be able to access web_network and db_networkdepends_on , This container needs to wait db The container can only be started after starting . Look at the db Containers :
image : Pull... From the warehouse MySQL 5.7 .volumes : Here comes static-volume It's called volume . It's used in a way like this :static-volume:/code/collected_static , After the colon, it's the directory inside the container , But the directory before the colon is not the host Directory 、 Just the name of the volume . essentially , Data volume also implements directory mapping between host and container , But the data volume was created by Docker managed , You don't even need to know where the data volume is stored on the host .Compared to mount , The advantage of data volume is due to Docker Unified management , There is no mount problem due to insufficient permissions , You don't need to specify different paths on different servers ; The disadvantage is that it is not suitable for single profile mapping . It's the same as mounting , The life cycle of data volume is out of container , After deleting the container, the volume still exists . The next time you build the image , Specify the name of the volume to continue using .
ports :MySQL The default communication port is 3306 . Because I already have one running on my machine MySQL service , So I put the 3306 The port is mapped to the native 3307 port .environment : Define the container's environment variables , Set up MySQL Of root User's password 、 Database name .network: Can only access db_network. add to db Remember the changes after the container Django Database settings in .
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_app',
'USER': 'root',
'PASSWORD': 'mypassword',
'HOST': 'db',
'PORT': '3306',
'OPTIONS': {'charset': 'utf8mb4'},
}
} Finally, let's analyze nginx Containers , Other configurations are roughly the same as the above two , It is worth saying that ports Set up , Because other services are deployed on my server , So I will nginx Port map to 8001.
Docker Allows users to define the network that each container works on , Only in the same network can we communicate . You can see nginx The container is in web_network The Internet , and db The container is in db_network The Internet , So they can't communicate , In fact, communication is not needed . and app The container is at the same time web_network and db_network The Internet , It's a bridge , Connected 3 A container .
modify Nginx Configuration file for , That is, mapping to nginx Inside the container config/nginx/django_app.conf
upstream app {
ip_hash;
server app:8000;
}
server {
listen 8000;
server_name localhost;
location /static/ {
autoindex on;
alias /code/collected_static/;
}
location / {
proxy_pass http://app/;
}
} In this configuration Nginx Will monitor the container 8000 port , And send the received request to app Containers ( Except for static file requests ).
After the requirements.txt Add... To the last two lines of the file mysqlclient Kuhe gunicorn library .
mysqlclient==2.0.1
gunicorn==19.9.0Revise Django The configuration file for the project
ALLOWED_HOSTS = ['*']
...
STATIC_ROOT = os.path.join(BASE_DIR, 'collected_static')
STATIC_URL = '/static/' Run the command docker-compose build Construct a mirror image , Reuse docker-compose up To enable the service .
Here are some frequently used commands :
docker-compose downdocker-compose up -d.docker-compose up -d db perhaps docker-compose up -d app You can start db Container or app Containers .docker exec -it container_id /bin/bashcontainer_id If you don't know how to get it, you can go through docker ps Command view .
Solve the problem of running Python under win10 CMD to pop up windows app store
Search for “ Manage applicatio
Computer graduation design Python+django train ticketing system (source code + system + mysql database + Lw document)
Project IntroductionWith the r