Deploy the django project to the container and access it in a local browser

1. Make sure Docker is installed and configured

2. Create a file named Dockerfile in the root directory of the demo project. The content of the file is as follows:

FROM python:3.9

# 设置 Python 环境变量
ENV PYTHONUNBUFFERED 1

# 安装 MySQL 客户端库
RUN apt-get update && apt-get install -y default-libmysqlclient-dev

# 创建项目目录并设置工作目录
RUN mkdir /app
WORKDIR /app

# 复制项目依赖文件并安装依赖
COPY requirements.txt /app/
RUN pip install -r requirements.txt

# 复制项目文件
COPY . /app/

# 设置环境变量
ENV DB_HOST=db
ENV DB_NAME=database_name
ENV DB_USER=user_name
ENV DB_PASSWORD=user_password

# 运行 Django 项目
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

This Dockerfile builds from the Python 3.9 image, sets the PYTHONUNBUFFERED environment variable, and sets the working directory to /app. Then install dependencies and copy the entire project into the container's /app directory.

3. Create a file called requirements.txt in the project root directory and list all Django project dependencies in it, for example:

Django==3.2.4
psycopg2-binary==2.8.6

Dependencies can be added or removed according to project needs.
If the project depends on mysql, you also need to create a docker-compose.yml file to define how to start the Docker container.
This file can be placed in the same directory as the Dockerfile:

version: '3'

services:
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: mysecretpassword
      MYSQL_DATABASE: database_name
      MYSQL_USER: user_name
      MYSQL_PASSWORD: user_password
    volumes:
      - db-data:/var/lib/mysql

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/app
    ports:
      - "8000:8000"
    depends_on:
      - db

volumes:
  db-data:

In this configuration file, we define two services: db and app. The db service uses the MySQL 8.0 image on Docker Hub, and sets some environment variables and data volumes. The app service uses your Django image and maps port 8000 of the container to port 8000 of the host. Also, we set some environment variables for connecting to the MySQL database. Finally, we define a data volume for persistent MySQL database data.

In this configuration file, we set the DB_HOST environment variable to db, which means that the Django container will connect to the MySQL database in the db service. We also set some other environment variables, including the database name, username, and password. These environment variables will be passed to the application when the Django container starts so that it can connect to the MySQL database.

Finally, we define a data volume called db-data, which stores the data of the MySQL database persistently on the local host. This data volume ensures that even if the Docker container is deleted or recreated, the database data is preserved.

4. Modify settings.py

DATABASES = {
    
    
    'default': {
    
    
        'ENGINE': 'django.db.backends.mysql',
        'NAME': os.getenv('DB_NAME'),
        'USER': os.getenv('DB_USER'),
        'PASSWORD': os.getenv('DB_PASSWORD'),
        'HOST': os.getenv('DB_HOST'),
        'PORT': '3306',
    }
}

5. Use the following commands to build and run the Docker container:

docker-compose up --build

6. Enter the container

#进入docker-compose.yml文件所在目录
docker-compose exec web bash

or:

docker exec -it <container_name> bash

implement:

python manage.py runserver 0.0.0.0:8000

7. Enter the http://localhost:8000/accessed
In the output of the docker inspect command, the IP address of the container can be found under the Networks field, where NetworkSettings.Networks is the network setting of the container, and IPAddress is the IP address of the container.
You can use the following command to get the IP address of the container:

docker inspect -f '{
    
    {range .NetworkSettings.Networks}}{
    
    {.IPAddress}}{
    
    {end}}' <CONTAINER_NAME>

Then you can use http://$IP:8000/to access django

The difference between docker-compose up --build and docker build is as follows:

docker-compose up --build will build and start an application with multiple service containers defined by the docker-compose.yml file. If the image does not exist, docker-compose will automatically build the image. It will not repeat the build if the image already exists. This command also creates the required networks and volumes.

docker build will just build a Docker image and look for a Dockerfile in the current directory. It does not start containers, nor does it create networks or volumes.

Therefore, if you want to build and start a multi-container application, you should use docker-compose up --build . If you only need to build a Docker image, you should use the docker build command.

Guess you like

Origin blog.csdn.net/liulanba/article/details/130516307
Recommended