Running Servers in Containers
INFO 253B: Backend Web Architecture
Kay Ashaolu
What is Docker
- Docker is an open-source project based on Linux containers
- Docker enables us to create containers that will run our backend web applications
What does Docker & Containers allow yo to do?
- Easy to get up and running
- Easy to find a docker image that you can use right from the box (more on this later)
- Easy to break down your system into multiple parts, each running in it's own isolated user space
Terminology
- Docker Engine: like the hypervisor, it's the runtime where docker runs. Manages the containers and builds and run natively on linux
- Docker Client: Part of the Docker Engine that enables you to communicate with Docker via the terminal
- Docker Daemon: process that actually executes the commands you send to the docker client
- Dockerfile: set of instructions you give docker to build your container. Think of this more as a set of terminal commands
More Terminology
- Docker Image. Read only templates that is built from a Dockerfile. Used to create Docker Containers
- Volumes: Disk volumes that enable you to persist and share a container's data. Once a container shuts down, everything within it is deleted. In order to persist data you will need to connect a volume to the container
- Docker Containers. The actual containers running on your computer. Everything your application needs to run: operating system, application code, system libraries, etc. Built on top of a Docker image
Docker CLI commands
- docker build: Builds an image from a dockerfile
-
docker run: builds and runs a container from a dockerfile. If image has not been built, rebuilds image
- -d: detached, can connect to it via terminal at any time
- -i: interactive: be able to "ssh" into your container
- -t: allows you when ssh to actually send commands
- docker ps -a: Provides list of containers that have or had running processes in them
Demo: convert quote service to Docker
FROM python:3.9-alpine
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 5000
ENTRYPOINT ["python3"]
CMD ["-m", "flask", "--app", "quote", "run", "--host=0.0.0.0", "--port=5000"]
Demo: convert dictionary service to Docker
# In terminal
# This command build the image and tags it with a name that you can refer to later
docker build -t quote_service_image .
# This command executes an container based on the image built above. It assigns an
# environment variable "FLASK_APP", and it maps http://localhost:5000 on your host
# machine to the flask app on port 5000 one he container.
#
# The EXPOSE keyword in the docker file tells docker
# that the container is communicating on port 5000, but your docker run command must
# map that port to a port on the host machine
docker run -dit --name=quote_service_container -p 5050:5000 quote_service_image
# To see and follow the logs of a running container (Ctrl-C to exit)
docker logs -f quote_service_container
# Now check http://localhost:5050 on host computer to see if it worked (note port 5000 in
# container is mapped to port 5050 on your computer)
Questions?
Running Servers in Containers - Backend Webarch
By kayashaolu
Running Servers in Containers - Backend Webarch
Course Website: https://www.ischool.berkeley.edu/courses/info/253b
- 1,482