docker - 部署java/python项目

目录

1、docker - 部署 java 项目

1. 创建 mysql 容器

2. 验证mysql,dbserver 连接 mysql ,服务器ip:3306 ,账户:root 密码:123456

3. 创建tomcat容器

4. 验证:http://ip:80

5. 开发项目war包,放到tomcat容器webapps目录下即可

2、docker - 部署 python 项目

1. 在 gitee 下载源码zip包,并解压到D盘的 docker-python 目录下,pycharm中修改settings.py

2. 安装mysql容器

3. 安装gunicorn容器 [web的]

4. 安装nginx容器 [处理网站的静态资源]

5. 自定义 docker 网络

6. 关掉并删除所有容器

7. 停止不用的容器,确保部署代码的端口没有被占用,启动项目:

8. 重新执行以下 docker-compose up -d,并检查容器是否运行成功,如果失败,则docker logs -f 容器名,进行日志定位解决

9. 设置项目的初始密码

10. 验证

​🎁更多干货

完整版文档下载方式:


1、docker - 部署 java 项目

1. 创建 mysql 容器

docker run -p 3306:3306 --name some-mysql -e MYSQL_ROOT_PASSWORD=123456 -d daocloud.io/library/mysql:5.7.7
# 停止容器,删掉这个容器,重跑这条命令,不会重新下载镜像,会调用本地已经下载的镜像创建容器

2. 验证mysql,dbserver 连接 mysql ,服务器ip:3306 ,账户:root 密码:123456

3. 创建tomcat容器

docker run -d -p 80:8080 --rm daocloud.io/library/tomcat:8.0.45
# 80是因为腾讯云默认开启端口80,如果你设置了其他端口,需要在防火墙那开启端口

4. 验证:http://ip:80

5. 开发项目war包,放到tomcat容器webapps目录下即可

1、根目录下执行(war文件已经放到根目录下了): docker cp XGYSytest12.war 70:/user/local/tomcat/webapps
2、查看日志:docker logs -f tomcat容器标识
把war文件放进去,在浏览器就可以直接访问测试环境了,tomcat会解析war文件生成网站

2、docker - 部署 python 项目

1. 在 gitee 下载源码zip包,并解压到D盘的 docker-python 目录下,pycharm中修改settings.py

import os

# 报错不会显示在网页上
DEBUG = False
# 你允许访问的hostname名
ALLOWED_HOSTS = ["127.0.0.1", "localhost", "162.14.119.65"]
# 配置项目数据库,只修改了HOST的值为:数据库的容器名:auto_test_platform_mysql
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'auto_test_plt',
        'USER': 'auto_test_plt',
        'PASSWORD': 'auto_test_plt',
        'HOST': 'auto_test_platform_mysql',
        'PORT': '3306',
    }
}

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

2. 安装mysql容器

项目根目录下创建文件 docker-compose.yml

Services:
  auto_test_platform_mysql:
    image: daocloud.io/library/mysql:5.7.7
    restart: always
    container_name: auto_test_platform_mysql
    volumes:
      - ./mysql/data:/var/lib/mysql
      - ./default.cnf:/etc/mysql/conf.d/default.cnf
    ports:
      - 3306:3306
    environments:
      MYSQL_ROOT_PASSWORD: 123456
      TZ: Asiz/Shanghai
      MYSQL_DATA_BASE: auto_test_plt
      MYSQL_USER: auto_test_plt
      MYSQL_PASSWORD: auto_test_plt
    networks:
      - net1

项目外面的创建文件 default.cnf

[mysqld]
character-set-server = utf8mb4
collection-server = utf8mb4_general_ci
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4

 

3. 安装gunicorn容器 [web的]

Gunicorn是一个WSGI HTTP Server,是针对Python的、在Unix系统上运行的、用来解析HTTP请求的网关服务。特点是:能和大多数的Python web框架兼容;使用简单;轻量级的资源消耗;高性能。相当于java里面的tomcat,对静态资源的处理不够好,所以需要nginx来管理静态资源

  • 1、创建 requirements.txt
Django==3.2.9
mysqlclient==2.1.0
gunicorn

  • 2、创建 dockerfile
FROM python:3.8
COPY ./requirements.txt /app/
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r /app/requirements.txt
ENTRYPOINT cd /app/auto_test_platform; python manage.py collectstatic -c --no-input; python manage.py migrate; gunicorn -b 0.0.0.0:8000 auto_test_platform.wsgi;

  • 3、编辑 docker-compose.py 文件,制定 gunicorn容器
auto_test_platform_gunicorn:
  build:
    conttext: ./
    dockerfile: Dockerfile
  restart: always
  environments:
    TZ: Asia/Shanghai
  image: auto_test_platform:1.0
  container_name: auto_test_platform_gunicorn
  working_dir: /app/auto_test_platform
  volumes:
    - ./auto_test_platform:/app/auto_test_platform
  ports:
    - 8000:8000
  depends_on:
    - auto_test_platform_mysql
  networks:
    - net1

4. 安装nginx容器 [处理网站的静态资源]

  • 1、先处理nginx配置文件
server {
	listen		80;
	server_name	auto_test_platform;
	
	location /static/ {
		autoindex on;
		alias /static/;
	}
	
	location / {
		proxy_pass http://auto_test_platform_gunicorn:8000;
		proxy_set_header Host $host;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	}
}

  • 2、编辑 docker-compose.py 文件,制定 nginx 容器
auto_test_platform_nginx:
  image: nginx:latest
  container_name: auto_test_platform_nginx
  volumes:
    - ./auto_test_platform/static:/static
    - ./auto-test-platform-nginx.conf:/etc/nginx/conf.d/default.conf
  ports:
    - 8088:80
  depends_on:
    - auto_test_platform_gunicorn
  networks:
    - net1

5. 自定义 docker 网络

把3容器通过桥接方式放在一个网络,方便他们可以用容器名相互访问

Services:
  auto_test_platform_mysql:
    image: daocloud.io/library/mysql:5.7.7
    restart: always
    container_name: auto_test_platform_mysql
    volumes:
      - ./mysql/data:/var/lib/mysql
      - ./default.cnf:/etc/mysql/conf.d/default.cnf
    ports:
      - 3306:3306
    environments:
      MYSQL_ROOT_PASSWORD: 123456
      TZ: Asiz/Shanghai
      MYSQL_DATA_BASE: auto_test_plt
      MYSQL_USER: auto_test_plt
      MYSQL_PASSWORD: auto_test_plt
    networks:
      - net1

  auto_test_platform_gunicorn:
    build:
      conttext: ./
      dockerfile: Dockerfile
    restart: always
    environments:
      TZ: Asia/Shanghai
    image: auto_test_platform:1.0
    container_name: auto_test_platform_gunicorn
    working_dir: /app/auto_test_platform
    volumes:
      - ./auto_test_platform:/app/auto_test_platform
    ports:
      - 8000:8000
    depends_on:
      - auto_test_platform_mysql
    networks:
      - net1

  auto_test_platform_nginx:
    image: nginx:latest
    container_name: auto_test_platform_nginx
    volumes:
      - ./auto_test_platform/static:/static
      - ./auto-test-platform-nginx.conf:/etc/nginx/conf.d/default.conf
    ports:
      - 8088:80
    depends_on:
      - auto_test_platform_gunicorn
    networks:
      - net1

networks:
  net1:
    driver: bridge

6. 关掉并删除所有容器

docker-compose down

7. 停止不用的容器,确保部署代码的端口没有被占用,启动项目:

docker-compose up -d 启动日志:


[+] Running 7/8
 ⠿ auto_test_platform_nginx Pulled                                                                                                                                                                       7.5s
   ⠿ e5ae68f74026 Pull complete                                                                                                                                                                          4.1s
   ⠿ 21e0df283cd6 Pull complete                                                                                                                                                                          4.9s
   ⠿ ed835de16acd Pull complete                                                                                                                                                                          5.0s
   ⠿ 881ff011f1c9 Pull complete                                                                                                                                                                          5.0s
   ⠿ 77700c52c969 Pull complete                                                                                                                                                                          5.1s
   ⠿ 44be98c0fab6 Pull complete                                                                                                                                                                          5.2s
 ⠿ auto_test_platform_gunicorn Error                                                                                                                                                                    35.5s
Sending build context to Docker daemon  72.39MB
Step 1/4 : FROM python:3.8
3.8: Pulling from library/python
5e0b432e8ba9: Pull complete
a84cfd68b5ce: Pull complete
e8b8f2315954: Pull complete
0598fa43a7e7: Pull complete
83098237b6d3: Pull complete
b92c73d4de9a: Pull complete
ef9b6ee59783: Pull complete
c1f6285e6406: Pull complete
a0ee73333012: Pull complete
Digest: sha256:881e0df149c29af8b29a973a9e80814dae6cddf123fe38a0bcac71864c85fb8a
Status: Downloaded newer image for python:3.8
 ---> f746089c9d02
Step 2/4 : COPY ./requirements.txt /app/
 ---> cf8dab81cbac
Step 3/4 : RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r /app/requirements.txt
 ---> Running in 23f583394c99
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting Django==3.2.9
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a8/ca/e88eb097959c48cd313dfc4bc394699a48fe5c158ed3a64c13e4fa46c1fd/Django-3.2.9-py3-none-any.whl (7.9 MB)
Collecting mysqlclient==2.1.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/de/79/d02be3cb942afda6c99ca207858847572e38146eb73a7c4bfe3bdf154626/mysqlclient-2.1.0.tar.gz (87 kB)
Collecting gunicorn
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/e4/dd/5b190393e6066286773a67dfcc2f9492058e9b57c4867a95f1ba5caf0a83/gunicorn-20.1.0-py3-none-any.whl (79 kB)
Collecting pytz
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d3/e3/d9f046b5d1c94a3aeab15f1f867aa414f8ee9d196fae6865f1d6a0ee1a0b/pytz-2021.3-py2.py3-none-any.whl (503 kB)
Collecting asgiref<4,>=3.3.2
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/fe/66/577f32b54c50dcd8dec38447258e82ed327ecb86820d67ae7b3dea784f13/asgiref-3.4.1-py3-none-any.whl (25 kB)
Collecting sqlparse>=0.2.2
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/05/40/d836d55fb3f467243ee839ab7b814822fda522cd395fa41e282684e71ee5/sqlparse-0.4.2-py3-none-any.whl (42 kB)
Requirement already satisfied: setuptools>=3.0 in /usr/local/lib/python3.8/site-packages (from gunicorn->-r /app/requirements.txt (line 3)) (57.5.0)
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (setup.py): started
  Building wheel for mysqlclient (setup.py): finished with status 'done'
  Created wheel for mysqlclient: filename=mysqlclient-2.1.0-cp38-cp38-linux_x86_64.whl size=113481 sha256=af552566c448c8f6fcf2b984745c92ee1614d375be5afe41ec7187ce962b1645
  Stored in directory: /root/.cache/pip/wheels/46/f8/f5/836501cfe21c884346cf554d596e63d4107dc5a30e5c6922ea
Successfully built mysqlclient
Installing collected packages: sqlparse, pytz, asgiref, mysqlclient, gunicorn, Django
Successfully installed Django-3.2.9 asgiref-3.4.1 gunicorn-20.1.0 mysqlclient-2.1.0 pytz-2021.3 sqlparse-0.4.2
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
WARNING: You are using pip version 21.2.4; however, version 21.3.1 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
 ---> cda5bf0476f1
Step 4/4 : ENTRYPOINT cd /app/auto_test_platform; python manage.py collectstatic -c --no-input; python manage.py migrate; gunicorn -b 0.0.0.0:8000 auto_test_platform.wsgi;
 ---> Running in 3ca4e1573bc8
 ---> aa0ca134a4be
Successfully built aa0ca134a4be
Successfully tagged auto_test_platform:1.0

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
[+] Running 4/4
 ⠿ Network docker-python_net1             Created                                                                                                                                                        0.1s
 ⠿ Container auto_test_platform_mysql     Started                                                                                                                                                        0.5s
 ⠿ Container auto_test_platform_gunicorn  Started                                                                                                                                                        1.4s
 ⠿ Container auto_test_platform_nginx     Started   

8. 重新执行以下 docker-compose up -d,并检查容器是否运行成功,如果失败,则docker logs -f 容器名,进行日志定位解决

 

9. 设置项目的初始密码

  • docker images
  • docker exec -it gun容器名 bash,执行 python manage.py createsuperuser

10. 验证

​🎁更多干货


 完整版文档下载方式:

这些资料,对于从事【软件测试】等相关工作的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!以上均可以分享。

在评论区和我互动交流或者私❤我【软件测试学习】领取即可,拿走不谢。


如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “👍点赞” “✍️评论” “💙收藏” 一键三连哦!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值