Docker Containers and Microservices
Georgetown University
Fall 2023
map
and reduce
Each command to modify the environment is a layer in your build process
FROM XXXXX
. This is the base image that has the core OS-level software.Students explain what each command is doing
INSTRUCTION arguments
FROM [--platform=<platform>] <image>[:<tag>] [AS <name>]
- Pulling the base image to start withRUN <command>
or RUN ["executable", "param1", "param2"]
- Run commands of any typeVOLUME
- Creates mountpoint for Docker volumeENV <key>=<value> ...
- Set environment variables for your containerWORKDIR
- Sets working directory for other commandsEXPOSE <port> [<port>/<protocol>...]
- Open a port for the containerCOPY [--chown=<user>:<group>] [--chmod=<perms>] <src>... <dest>
- Copies files from host file system to container file system. Note working directory matters in the container too!ADD
- Copy with a link to allow for reusing of already built layers. Helps with build speed!CMD command param1 param2
- Final command to prepare the launching container. Typically occurs before an ENTRYPOINT
instruction or as its own standalone command. CMD
can be overwritten by new docker run
arguments.ENTRYPOINT command param1 param2
- Command that will run the Docker container as an executable, such as a web server, Jupter environment, or data pipeline.RUN ["/bin/bash", "-c", "echo hello"]
FROM busybox
ENV FOO=/bar
WORKDIR ${FOO} # WORKDIR /bar
ADD . $FOO # ADD . /bar
COPY \$FOO /quux # COPY $FOO /quu
Basic needs:
FROM python:3.8-slim-buster
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]
Build the docker image using a tag and version number docker build ./ -t docker-tag-name:1.0
. Read about docker building options here.
Check your docker images in your local file system using docker images
Test your docker image using docker run -it docker-tag-name
. Read about the flags here. The i
is to keep STDIN open, the t
is for pseudo-TTY mode (shell).
Deploy your docker image somewhere!
Lambda (or \(\lambda\)) is the name given to anonymous functions in some languages like Python.
Lambda is a powerful tool to build serverless microfunctions in a variety of programming languages that can be triggered in a variety of ways.
The execution of a Lambda can scale to meet the burst needs of users going to a website, or the number of rows of incoming data.
There can be thousands of executions happening simultaneously.
Lambda is billed by the millisecond (ms).
Lambdas only start executing in response to an event, such as an API being invoked, a schedule or more complex things.
A Lambda function is used primarily in the following two ways:
Source: Understanding the different ways to invoke Lambda function
Transforming a picture taken by a delivery agent into a desired format and storing it in S3 in real-time.
As a front-end (along with an API Gateway) to a ML model to provide real-time prediction to a customer clicking a link on a website .
A backend API that gets invoked when a QR code is scanned.
A simple single page website with a form.
Sending a scheduled newsletter to email addresses on a mailing list.
Levels of Lambda complexity:
Beyond any simple prototyping, you would probably never use this method.
The “hello world” example and then numbers with an endpoint
The Lambda code (say the index.py
file) and all the dependencies i.e. Python packages used by the code are packaged together in a Zip file, typically called function.zip
.
The function.zip
file is put in an S3 bucket and the Lambda is configured to pull it from there.
Example script for building a Lambda function.zip
and uploading it to S3.
# syntax=docker/dockerfile:1
# adapted from https://www.philschmid.de/aws-lambda-with-custom-docker-image
# https://docs.aws.amazon.com/lambda/latest/dg/python-image.html
FROM public.ecr.aws/lambda/python:3.9
# copy requirements file and install necessary packages
ADD requirements.txt ${LAMBDA_TASK_ROOT}
RUN pip3 install -r ${LAMBDA_TASK_ROOT}/requirements.txt --target "${LAMBDA_TASK_ROOT}"
# Copy function code to docker container
COPY app.py ${LAMBDA_TASK_ROOT}
# app (name of py file)
# handler (name of function to execute for lambda job)
CMD [ "app.lambda_handler" ]
https://www.tatvasoft.com/blog/aws-lambda-vs-azure-functions/
serverless web scraper with python and lambda - up to date simple example https://towardsdatascience.com/serverless-covid-19-data-scraper-with-python-and-aws-lambda-d6789a551b78
DSAN 6000 | Fall 2023 | https://gu-dsan.github.io/6000-fall-2023/