Docker (5): Build Python http project using Alpinelinux

Source: Internet
Author: User
Tags connection reset
This is a creation in Article, where the information may have evolved or changed.

The original text connection is: http://blog.csdn.net/freewebsys/article/details/53509676
Not reproduced without the permission of the blogger.
Bo main address is: Http://blog.csdn.net/freewebsys

1. Start learning to build Docker images

http://study.163.com/course/courseMain.htm?courseId=1273002
The course is about building with CentOS. I've been using CentOS all the time, but there's a problem.
Is the CentOS mirror is relatively large, installed more things. Real Linux does not really use that much, especially when using Docker, the more times you pack, the more image files accumulate.
So choose to use Alpine (the Alps, as with that sugar name). Official website:
https://alpinelinux.org/
There is a version of iOS, about 81MB, but the version used on Docker is more streamlined than 4MB. But this is no bash and needs to install a bash.
After installation, it is only 5MB. The rest is to install a variety of software. The basic version is this small, and for CentOS, a mirror will be more than 600 MB. Not to mention the loading of things.

2, build a alpine image

Target, do 2 mirrors, one database, one python.
And then connect them. Can be stored, can query, on it.
To take advantage of Docker, first build a basic Python environment.
Then develop the program on the basis of this environment.
Otherwise, it's too slow to build from 0 at a time. Mainly because the network is slow.
mkdir Alpine-python
VI Alpine-python/dockerfile

From Docker.Io/alpine:latestmaintainer Demo<Juest a Demo>ENV TZ"Asia/shanghai"RUN Echo"Https://mirror.tuna.tsinghua.edu.cn/alpine/v3.4/main" >/etc/apk/repositoriesrun apk Add--Update\Bash\Python\Python-dev \Py-pip \Mysql-dev \Build-base \  &&Pip Install Flask\  &&Pip Install MySQL-python \  &&Rm-RF/var/Cache/apk/*

Special attention should be paid to the following:

echo"https://mirror.tuna.tsinghua.edu.cn/alpine/v3.4/main" > /etc/apk/repositories

Where bash is easy to access in the mirror view.
This is a site to set up a domestic Tsinghua university.
Because the direct pull of the official website is too slow. Sometimes the cards are dead.
Of course, you can also directly create an intranet. That would have been quicker.
Refer to this buddy for a few more:
https://my.oschina.net/funwun/blog/710877

Then package a basic Python package:

-t demo/alpine-python:1.0.

If you don't have an error, you can. The Python environment relies on more things.
And Python also needs the GCC compiler tool when installing.
To view a compiled image using Docker images:
The discovery has also been hundreds of trillion. Seems to understand why to develop a golang.
Golang's files are just a few trillion. It's really good for mirroring.

To view the image using Docker images:

# Docker ImagesREPOSITORYTAG               IMAGEIdCREATEDSize<none> <none> CA29D11BDFB9 -Minutes ago458.6Mbdocker.io/alpine Latest Baa5d63471ea7Weeks ago4.799MB#如果发现没有tag, the tag command is named one directly. Use the image ID. #docker tag ca29d11bdfb9 demo/alpine-python:1.0#再查看:# Docker ImagesREPOSITORYTAG               IMAGEIdCREATEDSizedemo/alpine-python1.0Ca29d11bdfb9 -Minutes ago458.6Mbdocker.io/alpine Latest Baa5d63471ea7Weeks ago4.799MB#这下就有了. 

3. Building an HTTP Project

Then make an HTTP service using the Alpine-python image
VI main.py

fromimport Flaskapp = Flask(__name__)@app.route("/")def hello():    return"Hello World!"if"__main__":    app.run(host='0.0.0.0', port=5000)

Writing Dockerfile

FROM       demo/alpine-python:1.0MAINTAINER demo <juest a demo>WORKDIR /dataONBUILDCOPY main.py /dataEXPOSE5000CMD ["/env/bin/python""/data/main.py"]

Compile:

docker build-t demo/py- HTTP : 1.0 .  #查看镜像:  # Docker images  REPOSITORY TAG IMAGE ID CREATED sizedemo/py-ht        TP  1.0  41  a2e1d1fd4d 18  seconds  ago  458.6  mbdemo/alpine-python 1.0  ca29d11bdfb9 22  minutes ago 458.6  mbdocker.io/alpine Latest Baa5d63471ea 7  weeks ago 4.799  MB  /pre>

4, Run HTTP

-d-p5000:5000--name py-http demo/py-http:1.0

You can run it directly. But there is a problem, the access port is not the result.

# curl localhost:5000curl: (56) Recv failure: Connection reset by peer

The error is this:
https://github.com/docker/docker/issues/2174
But no solution has been found.

Already resolved, the Python program needs to be actively bound.

    app.run(host='0.0.0.0', port=5000)

That's all you have to say.

4, summary

The original text connection is: http://blog.csdn.net/freewebsys/article/details/53509676
Not reproduced without the permission of the blogger.
Bo main address is: Http://blog.csdn.net/freewebsys

In general, Docker is very useful, but encountered a problem.
Projects that use both Python and PHP need to have very large packages installed.
resulting in a very small mirror, alpine 5MB became hundreds of MB.
Probably understand the Golang for the cloud service. The last compiled file is only a few megabytes because of Golang.
It's perfect to tie up the Alpine. Two add up dozens of MB to fix.
Every time you upgrade a backup is very convenient. and Java plus JDK, in addition to the jar package dependency.
It becomes bloated. Continue to study.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.