Docker+Git 搭建Flask项目实验

18 篇文章 2 订阅
14 篇文章 0 订阅

本文需用到的资源:

项目地址: 99Kies@github.com

实验环境:

  1. ubuntu:16.04服务器
  2. docker

主要是模拟一下利用docker和git仓库搭建webserver

安装Docker,Git

sudo apt-get update
sudo apt-get install docker*
sudo apt-get install git

在服务器上简单搭建项目服务

git clone https://github.com/99kies/sayhello # download our project
cd sayhello	
pip install -r piplist.txt # 安装所需依赖包
export FLASK_APP=sayhello  # 设置环境变量
flask forge				   # forge是项目里自己定义的方法,用于生成虚假数据
flask run --host=0.0.0.0 --port=80 # 运行项目

如果你的服务器没有更新python3的版本,可能会出现下面的报错
若保留在3.5版本 flask forge 会失效,flask run 会报错
另外一提:尽量不要安装python-dotenv,在某些情况下,他会导致无法使用自定义的flask方法
在这里插入图片描述
解决方法:(我之前是3.5)升级python版本,将python版本提升到3.7.4以上。
Ubuntu下python安装教程
成功运行之后就搭建成功了
在这里插入图片描述

利用Docker搭建项目

既然在服务器上搭建都问题了,那就直接来编写Dockerfile吧,用docker来实现搭建网页。
自己写安装python的Dockerfile的话肯定麻烦重重,这时候就需要用到python官方的Dockerfile作为我们项目的父镜像(第一次实验时,一定要,为了方便以后其他项目的搭建)
我使用的是官方的debian-python3.7.4,因为官方的Dockerfile不会安装git,所以我们需要修改一下,让我们的父镜像自带git和python。这样以后就可以实现快速建站。

父镜像 debian/python:3 Dockerfile
#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "update.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#

FROM debian:buster-slim

# ensure local python is preferred over distribution python
ENV PATH /usr/local/bin:$PATH

# http://bugs.python.org/issue19846
# > At the moment, setting "LANG=C" on a Linux system *fundamentally breaks Python 3*, and that's not OK.
ENV LANG C.UTF-8

# runtime dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
		ca-certificates \
		netbase \
		git \
	&& rm -rf /var/lib/apt/lists/*

ENV GPG_KEY 0D96DF4D4110E5C43FBFB17F2D347EA6AA65421D
ENV PYTHON_VERSION 3.7.4

RUN set -ex \
	\
	&& savedAptMark="$(apt-mark showmanual)" \
	&& apt-get update && apt-get install -y --no-install-recommends \
		dpkg-dev \
		gcc \
		libbz2-dev \
		libc6-dev \
		libexpat1-dev \
		libffi-dev \
		libgdbm-dev \
		liblzma-dev \
		libncursesw5-dev \
		libreadline-dev \
		libsqlite3-dev \
		libssl-dev \
		make \
		tk-dev \
		uuid-dev \
		wget \
		xz-utils \
		zlib1g-dev \
# as of Stretch, "gpg" is no longer included by default
		$(command -v gpg > /dev/null || echo 'gnupg dirmngr') \
	\
	&& wget -O python.tar.xz "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz" \
	&& wget -O python.tar.xz.asc "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz.asc" \
	&& export GNUPGHOME="$(mktemp -d)" \
	&& gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys "$GPG_KEY" \
	&& gpg --batch --verify python.tar.xz.asc python.tar.xz \
	&& { command -v gpgconf > /dev/null && gpgconf --kill all || :; } \
	&& rm -rf "$GNUPGHOME" python.tar.xz.asc \
	&& mkdir -p /usr/src/python \
	&& tar -xJC /usr/src/python --strip-components=1 -f python.tar.xz \
	&& rm python.tar.xz \
	\
	&& cd /usr/src/python \
	&& gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)" \
	&& ./configure \
		--build="$gnuArch" \
		--enable-loadable-sqlite-extensions \
		--enable-optimizations \
		--enable-shared \
		--with-system-expat \
		--with-system-ffi \
		--without-ensurepip \
	&& make -j "$(nproc)" \
# setting PROFILE_TASK makes "--enable-optimizations" reasonable: https://bugs.python.org/issue36044 / https://github.com/docker-library/python/issues/160#issuecomment-509426916
		PROFILE_TASK='-m test.regrtest --pgo \
			test_array \
			test_base64 \
			test_binascii \
			test_binhex \
			test_binop \
			test_bytes \
			test_c_locale_coercion \
			test_class \
			test_cmath \
			test_codecs \
			test_compile \
			test_complex \
			test_csv \
			test_decimal \
			test_dict \
			test_float \
			test_fstring \
			test_hashlib \
			test_io \
			test_iter \
			test_json \
			test_long \
			test_math \
			test_memoryview \
			test_pickle \
			test_re \
			test_set \
			test_slice \
			test_struct \
			test_threading \
			test_time \
			test_traceback \
			test_unicode \
		' \
	&& make install \
	&& ldconfig \
	\
	&& apt-mark auto '.*' > /dev/null \
	&& apt-mark manual $savedAptMark \
	&& find /usr/local -type f -executable -not \( -name '*tkinter*' \) -exec ldd '{}' ';' \
		| awk '/=>/ { print $(NF-1) }' \
		| sort -u \
		| xargs -r dpkg-query --search \
		| cut -d: -f1 \
		| sort -u \
		| xargs -r apt-mark manual \
	&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
	&& rm -rf /var/lib/apt/lists/* \
	\
	&& find /usr/local -depth \
		\( \
			\( -type d -a \( -name test -o -name tests \) \) \
			-o \
			\( -type f -a \( -name '*.pyc' -o -name '*.pyo' \) \) \
		\) -exec rm -rf '{}' + \
	&& rm -rf /usr/src/python \
	\
	&& python3 --version

# make some useful symlinks that are expected to exist
RUN cd /usr/local/bin \
	&& ln -s idle3 idle \
	&& ln -s pydoc3 pydoc \
	&& ln -s python3 python \
	&& ln -s python3-config python-config

# if this is called "PIP_VERSION", pip explodes with "ValueError: invalid truth value '<VERSION>'"
ENV PYTHON_PIP_VERSION 19.2.3
# https://github.com/pypa/get-pip
ENV PYTHON_GET_PIP_URL https://github.com/pypa/get-pip/raw/309a56c5fd94bd1134053a541cb4657a4e47e09d/get-pip.py
ENV PYTHON_GET_PIP_SHA256 57e3643ff19f018f8a00dfaa6b7e4620e3c1a7a2171fd218425366ec006b3bfe

RUN set -ex; \
	\
	savedAptMark="$(apt-mark showmanual)"; \
	apt-get update; \
	apt-get install -y --no-install-recommends wget; \
	\
	wget -O get-pip.py "$PYTHON_GET_PIP_URL"; \
	echo "$PYTHON_GET_PIP_SHA256 *get-pip.py" | sha256sum --check --strict -; \
	\
	apt-mark auto '.*' > /dev/null; \
	[ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; \
	apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
	rm -rf /var/lib/apt/lists/*; \
	\
	python get-pip.py \
		--disable-pip-version-check \
		--no-cache-dir \
		"pip==$PYTHON_PIP_VERSION" \
	; \
	pip --version; \
	\
	find /usr/local -depth \
		\( \
			\( -type d -a \( -name test -o -name tests \) \) \
			-o \
			\( -type f -a \( -name '*.pyc' -o -name '*.pyo' \) \) \
		\) -exec rm -rf '{}' +; \
	rm -f get-pip.py

# CMD ["python3"]

有能力的同学可以用alpine-python作为父镜像(alpine比debian更小更轻)
build alpine/python image的时候,大陆服务器的同学,需要注意test_threading 的安装(如果不需要就取消吧,可能导致timeout),我实验的时候,差不多可以比debian轻200M
alpine换源指令

sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

在这里插入图片描述
build 父镜像

docker build -t debian/python:3 .

build 父镜像之后,根据Git项目进行编写子镜像的Dockerfile

子镜像 Dockerfile
FROM debian/python:3

RUN git clone https://github.com/99kies/sayhello && \
    cd /sayhello && \
    pip install -r piplist.txt && \
    export FLASK_APP=/sayhello/sayhello && \
    flask forge

ENV FLASK_APP=/sayhello/sayhello

build 子镜像

docker build -t my/web .

在这里插入图片描述
最后根据子镜像创建一个项目容器

docker run -id -p 5000:5000 --name app_test my/web flask run --host=0.0.0.0 --port=5000

在这里插入图片描述
可以看到我们已经将容器内的5000端口映射到服务器的5000端口,也就是说访问5000端口就可以看见我们的项目了

在这里插入图片描述
可以用logs命令查看容器内的情况

docker logs -f app_test

在这里插入图片描述

搭建完成!!

关于作者

联系方式 - 1290017556@qq.com

你也可以通过 github | csdn | @新浪微博 关注我的动态

    如果有任何问题或者建议,欢迎大家评论点赞👍

我的Docker专栏

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

99Kies

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值