From 41234f7a07cfc36d55be0c6def49a51b5b31666e Mon Sep 17 00:00:00 2001 From: liangliang Date: Tue, 13 Jul 2021 21:47:51 +0800 Subject: [PATCH 1/2] docker support --- .github/workflows/codeql-analysis.yml | 50 +++++++++++++-------------- .github/workflows/docker.yml | 15 +++----- DjangoBlog/settings.py | 5 --- Dockerfile | 4 ++- bin/docker_start.sh | 41 +++++++++++----------- bin/es-support/Dockerfile | 2 -- docker-compose.es.yml | 13 +++++-- requirements.txt | 4 +-- 8 files changed, 66 insertions(+), 68 deletions(-) delete mode 100644 bin/es-support/Dockerfile diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 6d80df8..08fbfc1 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -48,34 +48,34 @@ jobs: # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed steps: - - name: Checkout repository - uses: actions/checkout@v2 + - name: Checkout repository + uses: actions/checkout@v2 - # Initializes the CodeQL tools for scanning. - - name: Initialize CodeQL - uses: github/codeql-action/init@v1 - with: - languages: ${{ matrix.language }} - # If you wish to specify custom queries, you can do so here or in a config file. - # By default, queries listed here will override any specified in a config file. - # Prefix the list here with "+" to use these queries and those in the config file. - # queries: ./path/to/local/query, your-org/your-repo/queries@main + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v1 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + # queries: ./path/to/local/query, your-org/your-repo/queries@main - # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). - # If this step fails, then you should remove it and run the build manually (see below) - - name: Autobuild - uses: github/codeql-action/autobuild@v1 + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v1 - # ℹ️ Command-line programs to run using the OS shell. - # 📚 https://git.io/JvXDl + # ℹ️ Command-line programs to run using the OS shell. + # 📚 https://git.io/JvXDl - # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines - # and modify them (or add more) to build your code if your project - # uses a compiled language + # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines + # and modify them (or add more) to build your code if your project + # uses a compiled language - #- run: | - # make bootstrap - # make release + #- run: | + # make bootstrap + # make release - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1 + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v1 diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 5b5b589..77b87ae 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -12,23 +12,18 @@ jobs: docker: runs-on: ubuntu-latest steps: - - - name: Checkout + - name: Checkout uses: actions/checkout@v2 - - - name: Set up QEMU + - name: Set up QEMU uses: docker/setup-qemu-action@v1 - - - name: Set up Docker Buildx + - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 - - - name: Login to DockerHub + - name: Login to DockerHub uses: docker/login-action@v1 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - - - name: Build and push + - name: Build and push uses: docker/build-push-action@v2 with: context: . diff --git a/DjangoBlog/settings.py b/DjangoBlog/settings.py index 078425e..1344243 100644 --- a/DjangoBlog/settings.py +++ b/DjangoBlog/settings.py @@ -182,11 +182,6 @@ CACHE_CONTROL_MAX_AGE = 2592000 # cache setting CACHES = { 'default': { - 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', - 'LOCATION': os.environ.get('DJANGO_MEMCACHED_LOCATION') or '127.0.0.1:11211', - 'KEY_PREFIX': 'django_test' if TESTING else 'djangoblog', - 'TIMEOUT': 60 * 60 * 10 - } if env_to_bool('DJANGO_MEMCACHED_ENABLE', False) else { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'TIMEOUT': 10800, 'LOCATION': 'unique-snowflake', diff --git a/Dockerfile b/Dockerfile index 7a43ef9..7bc47ec 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,8 @@ RUN apt-get install default-libmysqlclient-dev -y && \ ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ADD requirements.txt requirements.txt RUN pip install -Ur requirements.txt && \ - pip install gunicorn[gevent] + pip install gunicorn[gevent] && \ + pip cache purge + ADD . . ENTRYPOINT ["/code/DjangoBlog/bin/docker_start.sh"] diff --git a/bin/docker_start.sh b/bin/docker_start.sh index 2d7f1a8..d7c957e 100644 --- a/bin/docker_start.sh +++ b/bin/docker_start.sh @@ -1,34 +1,33 @@ #!/bin/bash + NAME="djangoblog" # Name of the application -DJANGODIR=/code/DjangoBlog # Django project directory +DJANGODIR=/var/www/DjangoBlog # Django project directory +SOCKFILE=/var/www/DjangoBlog/run/gunicorn.sock # we will communicte using this unix socket USER=root # the user to run as GROUP=root # the group to run as -NUM_WORKERS=1 # how many worker processes should Gunicorn spawn -#DJANGO_SETTINGS_MODULE=DjangoBlog.settings # which settings file should Django use +NUM_WORKERS=3 # how many worker processes should Gunicorn spawn +DJANGO_SETTINGS_MODULE=DjangoBlog.settings # which settings file should Django use DJANGO_WSGI_MODULE=DjangoBlog.wsgi # WSGI module name - echo "Starting $NAME as `whoami`" # Activate the virtual environment cd $DJANGODIR - +source /var/www/dev/python3/bin/activate +export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE export PYTHONPATH=$DJANGODIR:$PYTHONPATH -#pip install -Ur requirements.txt -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com && \ -# pip install gunicorn -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com -python manage.py makemigrations && \ - python manage.py migrate && \ - python manage.py collectstatic --noinput && \ - python manage.py compress --force && \ - python manage.py build_index && \ + +# Create the run directory if it doesn't exist +RUNDIR=$(dirname $SOCKFILE) +test -d $RUNDIR || mkdir -p $RUNDIR + # Start your Django Unicorn # Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon) -exec gunicorn ${DJANGO_WSGI_MODULE}:application \ ---name $NAME \ ---workers $NUM_WORKERS \ ---user=$USER --group=$GROUP \ ---bind 0.0.0.0:8000 \ ---log-level=debug \ ---log-file=- \ ---worker-class gevent \ ---threads 4 +exec /var/www/dev/python3/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \ + --name $NAME \ + --workers $NUM_WORKERS \ + --user=$USER --group=$GROUP \ + --bind=unix:$SOCKFILE \ + --log-level=debug \ + --log-file=- + diff --git a/bin/es-support/Dockerfile b/bin/es-support/Dockerfile deleted file mode 100644 index 2e66242..0000000 --- a/bin/es-support/Dockerfile +++ /dev/null @@ -1,2 +0,0 @@ -FROM elasticsearch:7.12.1 -RUN /usr/share/elasticsearch/bin/elasticsearch-plugin install -b https://ghproxy.com/https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.1/elasticsearch-analysis-ik-7.12.1.zip diff --git a/docker-compose.es.yml b/docker-compose.es.yml index 2440a65..fa0114c 100644 --- a/docker-compose.es.yml +++ b/docker-compose.es.yml @@ -2,11 +2,20 @@ version: '3' services: es: - build: ./bin/es-support + image: liangliangyy/elasticsearch-analysis-ik:7.13.2 container_name: es + restart: always ports: - - 9200:9200 + - 9200:9200 + volumes: + - ./bin/datas/es/:/usr/share/elasticsearch/data/ + kibana: + image: kibana:7.13.2 + restart: always + container_name: kibana + environment: + - ELASTICSEARCH_HOSTS=http://es:9200 djangoblog: build: . diff --git a/requirements.txt b/requirements.txt index 5187311..75809ab 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,14 +5,14 @@ django-haystack==3.0 django-ipware==3.0.2 django-mdeditor==0.1.18 django-uuslug==1.2.0 -elasticsearch==7.13.1 +elasticsearch==7.13.2 elasticsearch-dsl==7.3.0 gevent==21.1.2 jieba==0.42.1 jsonpickle==2.0.0 mistune==0.8.4 mysqlclient==2.0.3 -Pillow==8.3.0 +Pillow==8.3.1 Pygments==2.9.0 python-logstash==0.4.6 python-memcached==1.59 From 9475ff5d94b0f97646699610323e46e5bc98ff99 Mon Sep 17 00:00:00 2001 From: liangliang Date: Tue, 13 Jul 2021 23:18:57 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0docker=20es=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/docker_start.sh | 41 +++++++++++++++++++++-------------------- docker-compose.es.yml | 5 +++++ docs/docker.md | 8 +++++++- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/bin/docker_start.sh b/bin/docker_start.sh index d7c957e..ddada3a 100644 --- a/bin/docker_start.sh +++ b/bin/docker_start.sh @@ -1,33 +1,34 @@ #!/bin/bash - NAME="djangoblog" # Name of the application -DJANGODIR=/var/www/DjangoBlog # Django project directory -SOCKFILE=/var/www/DjangoBlog/run/gunicorn.sock # we will communicte using this unix socket +DJANGODIR=/code/DjangoBlog # Django project directory USER=root # the user to run as GROUP=root # the group to run as -NUM_WORKERS=3 # how many worker processes should Gunicorn spawn -DJANGO_SETTINGS_MODULE=DjangoBlog.settings # which settings file should Django use +NUM_WORKERS=1 # how many worker processes should Gunicorn spawn +#DJANGO_SETTINGS_MODULE=DjangoBlog.settings # which settings file should Django use DJANGO_WSGI_MODULE=DjangoBlog.wsgi # WSGI module name + echo "Starting $NAME as `whoami`" # Activate the virtual environment cd $DJANGODIR -source /var/www/dev/python3/bin/activate -export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE -export PYTHONPATH=$DJANGODIR:$PYTHONPATH - -# Create the run directory if it doesn't exist -RUNDIR=$(dirname $SOCKFILE) -test -d $RUNDIR || mkdir -p $RUNDIR +export PYTHONPATH=$DJANGODIR:$PYTHONPATH +#pip install -Ur requirements.txt -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com && \ +# pip install gunicorn -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com +python manage.py makemigrations && \ + python manage.py migrate && \ + python manage.py collectstatic --noinput && \ + python manage.py compress --force && \ + python manage.py build_index && \ # Start your Django Unicorn # Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon) -exec /var/www/dev/python3/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \ - --name $NAME \ - --workers $NUM_WORKERS \ - --user=$USER --group=$GROUP \ - --bind=unix:$SOCKFILE \ - --log-level=debug \ - --log-file=- - +exec gunicorn ${DJANGO_WSGI_MODULE}:application \ +--name $NAME \ +--workers $NUM_WORKERS \ +--user=$USER --group=$GROUP \ +--bind 0.0.0.0:8000 \ +--log-level=debug \ +--log-file=- \ +--worker-class gevent \ +--threads 4 \ No newline at end of file diff --git a/docker-compose.es.yml b/docker-compose.es.yml index fa0114c..dcffaa3 100644 --- a/docker-compose.es.yml +++ b/docker-compose.es.yml @@ -5,6 +5,9 @@ services: image: liangliangyy/elasticsearch-analysis-ik:7.13.2 container_name: es restart: always + environment: + - discovery.type=single-node + - "ES_JAVA_OPTS=-Xms512m -Xmx512m" ports: - 9200:9200 volumes: @@ -14,6 +17,8 @@ services: image: kibana:7.13.2 restart: always container_name: kibana + ports: + - 5601:5601 environment: - ELASTICSEARCH_HOSTS=http://es:9200 diff --git a/docs/docker.md b/docs/docker.md index 3de2cf3..761e448 100644 --- a/docs/docker.md +++ b/docs/docker.md @@ -19,7 +19,13 @@ docker-compose up -d ``` 本方式生成的mysql数据文件在 `bin/datas/mysql` 文件夹。 等启动完成后,访问 [http://127.0.0.1](http://127.0.0.1) 即可。 - +### 使用es +如果你期望使用es来作为后端的搜索引擎,那么可以使用如下命令来启动: +```shell +docker-compose -f docker-compose.yml -f docker-compose.es.yml build +docker-compose -f docker-compose.yml -f docker-compose.es.yml up -d +``` +本方式生成的es数据文件在 `bin/datas/es` 文件夹。 ## 配置说明: 本项目较多配置都基于环境变量,所有的环境变量如下所示: