Backend | Frontend | Database | Cloud | Other |
---|---|---|---|---|
Django4 | Bootstrap4 | MySQL | GCP | Docker |
安裝django
pip3 install django
開啟新項目
django-admin startproject [mysite]
cd [mysite]
python manage.py runserver
創建程式
python manage.py startapp [blog]
一鍵安裝shell
$ cat <<"EOF" | bash
sudo apt update && \
sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y && \
sudo apt-get remove docker docker.io containerd runc -y && \
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - && \
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" && \
sudo apt update && \
sudo apt install docker-ce docker-ce-cli containerd.io -y
EOF
檢查docker運行情形
$ sudo docker run hello-world
方法1
$ sudo apt update
$ sudo apt install apt-transport-https ca-certificates gnupg
$ curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
$ echo "deb https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
$ sudo apt update
$ sudo apt install google-cloud-sdk
方法2
$ sudo snap install google-cloud-sdk --classic
$ cd google-cloud-sdk/bin
$ sudo gcloud init
$ gcloud auth login
$ mkdir -p nginx-image; cd nginx-image;touch Dockerfile
$ vim Dockerfile
#docker file
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /django-files
WORKDIR /django-files
RUN git clone [git respo url]
RUN pip install Django
RUN pip install mysqlclient
ENTRYPOINT python Django-blog-api/mysite/manage.py runserver 0.0.0.0:80
$ sudo docker build --no-cache . -t [image name]
$ sudo docker image ls
> > docker tag SOURCE_IMAGE HOSTNAME/PROJECT-ID/TARGET-IMAGE:TAG
$ sudo docker tag blog asia.gcr.io/djangopraticeblog/blog
> > docker push HOSTNAME/PROJECT-ID/IMAGE:TAG
$ sudo docker push asia.gcr.io/djangopraticeblog/blog
ISSUE:[unauthorized] You don't have the needed permissions to perform this operation, and you may have invalid credentials. To authenticate your request, follow the steps in: https://cloud.google.com/container-registry/docs/advanced-authentication
$ gcloud auth configure-docker $ export PATH=$PATH:/lib/google-cloud-sdk/bin $ gcloud auth print-access-token |sudo docker login -u oauth2accesstoken --password-stdin https://asia.gcr.io
$ sudo docker pull mysql:8
$ sudo docker images
$ sudo docker tag mysql:8 asia.gcr.io/djangopraticeblog/mysql:8
$ sudo docker push asia.gcr.io/djangopraticeblog/mysql:8
$ docker ps
$ docker exec -i -t 2c5fe59531e7 /bin/bash
# mysql -u root -p
mysql> show databases;
$ docker ps
$ docker exec -i -t 3579c277f5f7 /bin/bash
# ls
# cd Django-blog-api/
# cd mysite/
# python manage.py runserver
# apt-get upgrade
# apt-get update
# apt-get install vim
更改資料庫連線(host使用內部IP) 設定ALLOW HOST(外部IP) 遷移資料庫結構
# python manage.py migrate
若要使用create article時記得要新增tag類別 建立使用者
# python manage.py createsuperuser