Docker配置Web

昵称已经被使用 / 2023-08-13 / 原文

Docker配置Web

一、安装Docker

安装步骤详见:

Ubuntu 20.04安装Docker

Docker构建nginx+uwsgi+flask镜像(二)

Docker 容器化部署 Flask 项目 + Gunicorn + Nginx

二、方法一:搭建服务

1、抓取镜像

抓取最新镜像:

$ docker pull ubuntu

抓取指定版本镜像:

$ docker pull ubuntu:18.04

注意:访问 Ubuntu 镜像库地址 https://hub.docker.com/_/ubuntu?tab=tags&page=1。

2、查看本地镜像

$ docker images

3、运行容器,并且进入ubuntu 容器

用最新版本的镜像运行容器并进入

$ docker run -itd --name ubuntu-container ubuntu

用指定版本镜像运行容器并进入

$ docker run -it --name ubuntu-container ubuntu:18.04 /bin/bash

4、安装成功

$ docker ps

5、容器中装软件

如果未进入容器,可通过docker attach 进入

参考博客:

ubuntu18.04安装python3.6以及更新为清华源

Ubuntu18 安装 Python3.6

  1. 安装python3.6

    # 更新apt源
    apt update
    # 用apt安装Python3.6
    apt install python3.6
    # 设置python3.6命令为python
    ln -s /usr/bin/python3.6 /usr/bin/python
    # 安装pip3
    apt install python3-pip 
    # 升级pip3
    pip3 install -U pip
    
  2. 安装nginx

    # 更新源
    apt update
    # 安装nginx
    apt install nginx
    # 查看nginx是否安装成功
    nginx -v
    
  3. 安装vim

    apt install vim
    

4、装第三方库

安装flask、gunicorn

pip3 install Flask==2.0.2 gunicorn==20.1.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

5、创建测试网站

mkdir /app
cd /app/
vi app.py

填入并保存:

from flask import Flask
 
app = Flask(__name__)
 
@app.route('/')
def hello_world():
    return 'Hello World!!!\n'
 
if __name__ == '__main__':
    app.run()

6、配置nginx

vi /etc/nginx/nginx.conf

在/etc/nginx目录下的nginx.conf文件中<1>处配置pid选项,这是一个目录,用于保存nginx的进程号,默认保存进程号的目录不存在,如果不修改这个配置,会报错。同时,我们还需要在http模块下的<2>处配置uwsgi协议反向代理到我们uwsgi+flask应用:

/app # cd /etc/nginx/
/etc/nginx # cat nginx.conf
# /etc/nginx/nginx.conf
 
# <1>配置pid选项
pid /var/run/nginx.pid;
 
……
 
http {
		# <2>配置uwsgi协议反向代理
        server {
                listen 6666;
                charset UTF-8;
                client_max_body_size 75M;
                location / {
                        proxy_read_timeout 300;
                        proxy_connect_timeout 300;
                        proxy_pass http://127.0.0.1:9000;
                }

        }
		……
 
}

重启nginx:

service nginx restart 

三、方法二:搭建环境并打包应用

参考博客:

Docker构建nginx+uwsgi+flask镜像(二)

基本镜像

1、创建目录

mkdir ubuntu_dir

2、编写Dockerfile文件

先来写第一个Dockerfile文件,这个文件负责搭建运行环境,运行环境需要包括:nginx、gunicorn、Python3

# 配置基础镜像
FROM ubuntu:18.04
 
# 更新apt源
RUN apt update
# 用apt安装Python3.6
RUN apt -y install python3.6
# 设置python3.6命令为python
RUN ln -s /usr/bin/python3.6 /usr/bin/python
# 安装pip3
RUN apt -y install python3-pip 

# 安装nginx
RUN apt -y install nginx

# 安装vim
RUN apt -y install vim
 
# 安装软件
RUN pip3 install Flask==2.0.2 gunicorn==20.1.0 -i https://pypi.tuna.tsinghua.edu.cn/simple 
# 若有requirements.txt依赖包文件
# RUN pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

3、生成镜像

执行docker build命令,生成镜像,执行docker images命令可以看到生成nginx_uwsgi_py3镜像,标签为alpine3.8:

docker build -t nginx_gunicorn_py3:ubuntu18_04 .

编写应用

web_app
├── app
│   ├── app.py
│   ├── requirements.txt
├── Dockerfile
└── nginx.conf

最外层的web_app目录包含一个app目录,和两个文件,分别是Dockerfile、nginx.conf,注意,web_app下的Dockerfile文件和之前的Dockerfile文件不同,这里的Dockerfile文件是用来打包应用的。nginx.conf文件在打包应用时会拷贝到容器中,作为nginx启动的配置。

1、修改app.py文件

from flask import Flask
 
app = Flask(__name__)
 
@app.route('/hello')
def hello():
    return 'Hello!!!\n'
 
@app.route('/world')
def world():
    return 'World!!!\n'
 
@app.route('/')
def hello_world():
    return 'Hello World!!!\n'
 
if __name__ == '__main__':
    app.run()

2、修改nginx.conf

http {
    server {
      listen 6666;
      charset utf-8;
      client_max_body_size 75M;
      location / {
        include uwsgi_params;
        uwsgi_pass unix:///tmp/uwsgi.sock;
        uwsgi_send_timeout 300;
        uwsgi_connect_timeout 300;
        uwsgi_read_timeout 300;
      }
    }
    ……
}

3、修改打包应用的Dockerfile

# 使用基础镜像库
FROM nginx_gunicorn_py3:ubuntu18_04

# 创建工作路径
RUN mkdir /app

# 指定容器启动时执行的命令都在app目录下执行
WORKDIR /app

# 替换nginx的配置
COPY nginx.conf /etc/nginx/nginx.conf

# 将本地app目录下的内容拷贝到容器的app目录下
COPY ./app/ /app/

# pip读取requirements.txt内容安装所需的库
RUN pip333 install -r /app/requirements.txt -i  https://pypi.tuna.tsinghua.edu.cn/simple some-package --no-cache-dir

# 启动nginx和uwsgi
ENTRYPOINT service nginx start && gunicorn -b 0.0.0.0:6666 app:app

4、执行docker build命令,生成镜像

docker build -t web_app .

注意:工作目录为web_app

5、启动容器

根据镜像启动一个容器,容器内部的nginx监听的是6666端口,而宿主机则用9999端口接收请求,再转发到容器内部的6666端口:

# 启动并进入容器
docker run -p 9999:6666 -it web_app
# 后台启动容器
docker run -p 9999:6666 -d web_app

6、测试容器内的应用

ip:9999
ip:9999/hello
ip:9999/world

7、导出镜像到本地

docker save -o 镜像名.tar 原始镜像名(REPOSITORY项)

sudo docker save -o /home/ubuntu/ubuntu_dir/web_app.tar web_app

从容器创建一个新的镜像(可选)

sudo docker commit -m '容器创建镜像' 5ac7095cc4d5 web_app_to

8、导入镜像

重启docker

 systemctl restart docker

导入

docker load < /home/ubuntu/ubuntu_dir/web_app.tar