- 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.
- Practical Ansible 2
- Excel 2007函數(shù)與公式自學(xué)寶典
- 計(jì)算機(jī)應(yīng)用復(fù)習(xí)與練習(xí)
- 自動(dòng)化控制工程設(shè)計(jì)
- Windows游戲程序設(shè)計(jì)基礎(chǔ)
- Linux服務(wù)與安全管理
- 嵌入式GUI開發(fā)設(shè)計(jì)
- 所羅門的密碼
- 青少年VEX IQ機(jī)器人實(shí)訓(xùn)課程(初級(jí))
- Building Google Cloud Platform Solutions
- 智慧未來
- Creating ELearning Games with Unity
- Microsoft Dynamics CRM 2013 Marketing Automation
- SQL語言與數(shù)據(jù)庫操作技術(shù)大全
- 小數(shù)據(jù)之美:精準(zhǔn)捕捉未來的商業(yè)小趨勢(shì)