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" ]
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.