Skip to content

Commit

Permalink
Merge branch 'main' of github.com:CSSE6400/software-architecture
Browse files Browse the repository at this point in the history
Merge practical updates into week 3 content updates.
  • Loading branch information
applebyter committed Mar 3, 2024
2 parents 6d28ebe + 969a02d commit ecca9a6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
4 changes: 1 addition & 3 deletions practicals/week01/main.tex
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ \section{Practicals}
\teacher{
Spend approximately the first 5 minutes introducing yourself, getting to know the students, and discussing the practicals in general.
Spend the next 10 minutes explaining the basic concepts of HTTP and REST.

You can consider using these slides: \url{https://csse6400.uqcloud.net/slides/web-apis.pdf}
}

\info{
Expand Down Expand Up @@ -932,7 +930,7 @@ \subsubsection{Creating more endpoints}
"deadline_at": "2023-02-27T00:00:00",
"created_at": "2023-02-20T00:00:00",
"updated_at": "2023-02-20T00:00:00"
}, 201)
}), 201
\end{code}

You will notice that currently this function is the same as the GET request but in future weeks we will build out the functionality to actually create a todo. Next is our endpoint to update a todo. In the \texttt{routes.py} file, add the following code to the bottom of the file:
Expand Down
52 changes: 25 additions & 27 deletions practicals/week03/main.tex
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ \subsubsection{Dockerfile}
Otherwise, you likely want to start from an image that has a number of the common utilities pre-packaged.

\begin{code}[language=docker,numbers=none]{Dockerfile}
FROM ubuntu:latest
FROM ubuntu:22.04
\end{code}

In this case we are using the latest version of ubuntu.
Expand All @@ -112,9 +112,8 @@ \subsubsection{Dockerfile}
The layer will be used for the next step in the Dockerfile.

\begin{code}[language=docker,numbers=none]{Dockerfile}
FROM ubuntu:latest
FROM ubuntu:22.04

# Installing dependencies for running a python application
RUN apt-get update && apt-get install -y python3 python3-pip
\end{code}

Expand All @@ -131,9 +130,8 @@ \subsubsection{Dockerfile}
The COPY instruction copies files or directories from <src> on the host machine where the image is being built to the filesystem of the container at the path <dest>.

\begin{code}[language=docker,numbers=none]{Dockerfile}
FROM ubuntu:latest
FROM ubuntu:22.04

# Installing dependencies for running a python application
RUN apt-get update && apt-get install -y python3 python3-pip

# Copying our application into the container
Expand All @@ -145,7 +143,7 @@ \subsubsection{Dockerfile}
The following example will still expose the \texttt{secrets.txt} file even though we removed it in another layer.

\begin{code}[language=docker,numbers=none]{Dockerfile}
FROM ubuntu:latest
FROM ubuntu:22.04

# Installing dependencies for running a python application
RUN apt-get update && apt-get install -y python3 python3-pip
Expand Down Expand Up @@ -177,7 +175,7 @@ \subsubsection{Dockerfile}
This instruction is a readability feature to help you reduce the amount of moving around within the container.

\begin{code}[language=docker,numbers=none]{Dockerfile}
FROM ubuntu:latest
FROM ubuntu:22.04

# Installing dependencies for running a python application
RUN apt-get update && apt-get install -y python3 python3-pip
Expand All @@ -194,7 +192,7 @@ \subsubsection{Dockerfile}
The ENTRYPOINT instruction allows you to configure what process is executed when a container is run.

\begin{code}[language=docker,numbers=none]{Dockerfile}
FROM ubuntu:latest
FROM ubuntu:22.04

# Installing dependencies for running a python application
RUN apt-get update && apt-get install -y python3 python3-pip
Expand Down Expand Up @@ -227,7 +225,7 @@ \subsubsection{Dockerfile}
CMD provides defaults for an executing container.

\begin{code}[language=docker,numbers=none]{Dockerfile}
FROM ubuntu:latest
FROM ubuntu:22.04

# Installing dependencies for running a python application
RUN apt-get update && apt-get install -y python3 python3-pip
Expand Down Expand Up @@ -279,7 +277,7 @@ \subsubsection{Dockerfile Layers}
In this Dockerfile, our application now has Python dependencies that need to be installed.

\begin{code}[language=docker,numbers=none]{Dockerfile}
FROM ubuntu:latest
FROM ubuntu:22.04

# Installing dependencies for running a python application
RUN apt-get update && apt-get install -y python3 python3-pip
Expand All @@ -302,7 +300,7 @@ \subsubsection{Dockerfile Layers}
This means that if we change our application code we do not need to reinstall the requirements.

\begin{code}[language=docker,numbers=none]{}
FROM ubuntu:latest
FROM ubuntu:22.04

# Installing dependencies for running a python application
RUN apt-get update && apt-get install -y python3 python3-pip
Expand Down Expand Up @@ -444,7 +442,7 @@ \subsubsection{Docker Volumes}
In our fan club example,
we are storing our application within the \texttt{/app} directory of the container.
If we want this to live update with the contents of our application on the host (assuming we run this command from the applications directory), we can do so with:
\bash{docker run -v $(pwd):/app fan-club}
\bash{docker run -v \$(pwd):/app fan-club}

This command mounts our current working directory (\texttt{\$(pwd)}) to the \texttt{/app} directory inside the container.
This allows us to update our application and have the change reflected inside the container.
Expand All @@ -471,7 +469,7 @@ \subsection{Creating a Dockerfile}
To start, we will use the following Dockerfile:

\begin{code}[language=docker,numbers=none]{Dockerfile}
FROM ubuntu:latest
FROM ubuntu:22.04

# Installing dependencies for running a python application
RUN apt-get update && apt-get install -y python3 python3-pip postgresql-client libpq-dev
Expand All @@ -483,7 +481,7 @@ \subsection{Creating a Dockerfile}

\begin{code}[language=docker,numbers=none]{}
# Install pipenv
RUN pip3 install pipenv
RUN pip3 install poetry
\end{code}

Let's now change our working directory to \texttt{/app} and copy our \texttt{Pipfile} and \texttt{Pipfile.lock} into the container.
Expand All @@ -494,8 +492,8 @@ \subsection{Creating a Dockerfile}
WORKDIR /app

# Install pipenv dependencies
COPY Pipfile Pipfile.lock ./
RUN pipenv install --system --deploy
COPY pyproject.toml ./
RUN poetry install --no-root
\end{code}

Note that we are installing dependencies in a layer separate to our application.
Expand All @@ -512,26 +510,26 @@ \subsection{Creating a Dockerfile}

\begin{code}[language=docker,numbers=none]{}
# Running our application
CMD ["flask", "--app", "todo", "run", "--host", "0.0.0.0", "--port", "6400"]
CMD ["poetry", "run", "flask", "--app", "todo", "run", "--host", "0.0.0.0", "--port", "6400"]
\end{code}

We should now have a complete Dockerfile, as shown below:

\begin{code}[language=docker,numbers=none]{}
FROM ubuntu:latest
FROM ubuntu:22.04
# Installing dependencies for running a python application
RUN apt-get update && apt-get install -y python3 python3-pip postgresql-client libpq-dev
# Install pipenv
RUN pip3 install pipenv
RUN pip3 install poetry
# Setting the working directory
WORKDIR /app
# Install pipenv dependencies
COPY Pipfile Pipfile.lock ./
RUN pipenv install --system --deploy
COPY pyproject.toml ./
RUN poetry install --no-root
# Copying our application into the container
COPY todo todo
# Running our application
CMD ["flask", "--app", "todo", "run", "--host", "0.0.0.0", "--port", "6400"]
CMD ["poetry", "run", "flask", "--app", "todo", "run", "--host", "0.0.0.0", "--port", "6400"]
\end{code}

\subsection{Building our Docker Image}
Expand Down Expand Up @@ -633,7 +631,7 @@ \subsection{Moving to a Postgresql Database}

\begin{code}[language=docker,numbers=none]{}
# Adding a delay to our application startup
CMD ["bash", "-c", "sleep 10 && flask --app todo run --host 0.0.0.0 --port 6400"]
CMD ["bash", "-c", "sleep 10 && poetry run flask --app todo run --host 0.0.0.0 --port 6400"]
\end{code}

In the long run you would want your application to be able to retry connecting to the database if it fails but for now we will just add a delay.
Expand Down Expand Up @@ -678,16 +676,16 @@ \subsection{Python Based Dockerfile}
\begin{code}[language=docker,numbers=none]{}
FROM python:3.10
# Install pipenv
RUN pip3 install pipenv
RUN pip3 install poetry
# Setting the working directory
WORKDIR /app
# Install pipenv dependencies
COPY Pipfile Pipfile.lock ./
RUN pipenv install --system --deploy
COPY pyproject.toml ./
RUN poetry install --no-root
# Copying our application into the container
COPY todo todo
# Running our application
CMD ["flask", "--app", "todo", "run", "--host", "0.0.0.0", "--port", "6400"]
CMD ["poetry", "run", "flask", "--app", "todo", "run", "--host", "0.0.0.0", "--port", "6400"]
\end{code}

You will notice that we did not have to install python3 and pip and the postgresql depenedencies.
Expand Down

0 comments on commit ecca9a6

Please sign in to comment.