官术网_书友最值得收藏!

  • Deployment with Docker
  • Srdjan Grubor
  • 423字
  • 2021-07-02 23:22:13

Service from scratch

Our last example was decently comprehensive but it left out some important Docker commands that we should also know, so we will use another example, albeit reworking the web server solution in a slightly less optimal way, to both show them used and to explain what they do. In the process, we will go a bit deeper and see whether we can make as many parts of the service on our own.

We will start this example with creating a clean directory and creating the same test file we used earlier:

$ mkdir ~/python_webserver
$ cd ~/python_webserver

$ echo "Just a test file" > test.txt

Now we will create our bit-more-complex Python-based web server container by putting the following content in the Dockerfile:

FROM python:3

# Add some labels for cache busting and annotating
LABEL version="1.0"
LABEL org.sgnn7.name="python-webserver"

# Set a variable that we will keep reusing to prevent typos
ENV SRV_PATH=/srv/www/html

# Make sure we are fully up to date
RUN apt-get update -q && \
apt-get dist-upgrade -y

# Let Docker know that the exposed port we will use is 8000
EXPOSE 8000

# Create our website's directory, then create a limited user
# and group
RUN mkdir -p $SRV_PATH && \
groupadd -r -g 350 pythonsrv && \
useradd -r -m -u 350 -g 350 pythonsrv

# Define ./external as an externally-mounted directory
VOLUME $SRV_PATH/external

# To serve things up with Python, we need to be in that
# same directory
WORKDIR $SRV_PATH

# Copy our test file
COPY test.txt $SRV_PATH/

# Add a URL-hosted content into the image
ADD https://raw.githubusercontent.com/moby/moby/master/README.md \
$SRV_PATH/

# Make sure that we can read all of these files as a
# limited user
RUN chown -R pythonsrv:pythonsrv $SRV_PATH

# From here on out, use the limited user
USER pythonsrv

# Run the simple http python server to serve up the content
CMD [ "python3", "-m", "http.server" ]

Using Python's built-in web server is highly discouraged in almost all cases, as it is neither scalable nor configurable in any significant way, but it serves as a good example of a service that could be hosted through Docker and is available on almost all systems with Python. Do not use this in real production services unless you really know what you are doing.

Barring the note about using python's web server module in production, this is still a good example of all of the other major Dockerfile directives that we didn't cover and that you will now learn how to use.

主站蜘蛛池模板: 荔浦县| 洛宁县| 长丰县| 龙游县| 垣曲县| 炉霍县| 柘荣县| 宁化县| 乡城县| 新乐市| 运城市| 大洼县| 安庆市| 增城市| 海淀区| 上蔡县| 建瓯市| 余姚市| 察雅县| 庐江县| 宣恩县| 泸溪县| 响水县| 德令哈市| 建瓯市| 黄大仙区| 灌阳县| 沧源| 四会市| 金山区| 三台县| 凌源市| 安吉县| 育儿| 洞头县| 德江县| 图片| 延长县| 延津县| 惠东县| 德兴市|