Skip to content

Bureau d'Γ©tudes Cloud & DockerπŸ”—

Link to slides

Objectives of this BEπŸ”—

This Bureau d'Γ©tudes (BE, for short) will guide you through the essential notions to be able to manipulate with regard to cloud computer and docker,

We will illustrate the following:

  • Work in a remote environment (inside a VM, using google cloud shell)
  • Creation and ssh connection to virtual machine instances
  • Usage of managed storage capabilities
  • Creating your own docker images
  • Exchanging docker images through a Container Registry
  • Pulling and running docker images created by your teammates

In particular, this workflow:

workflow

Warning

Please read all the text in the question before executing the step-by-step instructions because there might be help or indications after the instructions.

How to run this BEπŸ”—

The best way to run this BE is to setup a Github Codespace VM and install the google cloud sdk. Refer to the previous TP to learn more

We will be using the gcloud CLI for the following:

  • Create a GCE Virtual Machine
  • Connect to SSH with port forwarding to said machine

For the rest of this walkthrough, if it is written "from your local machine", this will be "github codespace"

If it is written "inside the VM", this means that you should run it inside the GCE VM that you have to run the SSH tunnel first...

πŸ™πŸ» Use Google Chrome without any ad blockers if you have any issues

Warning

⚠️ Normally you will do everything from your browser, connected to the github codespace, so it should work
⚠️ if you have any issues, switch your wi-fi connection between isae-edu, eduroam and a 4G hotspot

Team compositionπŸ”—

You should be in team of 5, however this will work with a minimum of 2 people.

Each team member picks a different cute mascot and remembers it:

  • 🐈 cat
  • πŸ• dog
  • πŸ‘½ (baby) yoda
  • πŸ¦‰ owl
  • 🐼 panda

Find a groupname, because you will need it for the next steps

1 - BuildπŸ”—

1.1 - Start Development Environment (Github Codespace)πŸ”—

1.2 - Get the necessary resources from Google Cloud StorageπŸ”—

From your github codespace,

The resources are located at the URI gs://fchouteau-isae-cloud/be/${MASCOT},

Your ${MASCOT} name is either:

  • cat
  • dog
  • owl
  • panda
  • yoda

I advise you to export MASCOT=.... to remember it :)

ONLY DOWNLOAD your mascot resources (no cheating ! this will only cause confusion later)

Download them to your instance using the gcloud cli (refer to your previous work for more information)

Hint

gsutil -m cp -r {source} {destination}
Remember that google storage URIs always begin with gs://

Go to (cd) the folder where you downloaded your resources

You should see a file structure like this

fchouteau@be-cloud-mascot:~/be$ tree yoda  -L 2
yoda
β”œβ”€β”€ app.py
β”œβ”€β”€ AUTHOR.txt
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ favicon.ico
β”œβ”€β”€ imgs
β”‚Β Β  β”œβ”€β”€ 1.gif
β”‚Β Β  β”œβ”€β”€ 2.gif
β”‚Β Β  β”œβ”€β”€ 3.gif
β”‚Β Β  β”œβ”€β”€ 4.gif
β”‚Β Β  └── 5.gif
└── template.html.jinja2

1 directory, 10 files

1.3 - Build your docker imageπŸ”—

Question

  • Look at the Dockerfile (cat Dockerfile), what does it seem to do ?
  • Look at app.py (cat app.py). What is Flask ? What does it seem to do ?
  • Edit the file AUTHOR.txt to add your name instead of the placeholder
  • Refer to your previous work to build the image

Danger

On which port is your flask app running ? (cat Dockerfile) Note it carefully ! You will need to communicate it to your teammate :)

  • When building the image, name it appropriately... like eu.gcr.io/${PROJECT_ID}/webapp-gif:${GROUPNAME}-${MASCOT}-1.0 !
Hint

to get your project id:

PROJECT_ID=$(gcloud config get-value project 2> /dev/null)

  • now if you list your images you should see it !
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
eu.gcr.io/{your project name}/{your-app}    1.0                 d1c5993848bf        2 minutes ago       62.1MB

Question

Describe concisely to your past self what is a Docker Image

2 - ShipπŸ”—

2.1 - Push your Docker image in the Container RegistryπŸ”—

Question

Describe succintly to your past self what is a Container Registry

Bug

in case of error

gcloud auth configure-docker

In the end, things should look like this

gcr

3 - Run (deploy)πŸ”—

3.1 - Create Google Compute Engine VMπŸ”—

Each team member creates a separate GCE Instance (Virtual Machine) on the same project,

Here, you will create a Google Compute Engine instance, preconfigured with everything you need,

If you use the google cloud CLI (from your codespace), you can use this

First, set a variable with the name of your instance,

export INSTANCE_NAME="be-cloud-mascot-{yourgroup}-{yourname}" # Don't forget to replace values !

Then create your VM

gcloud compute instances create $INSTANCE_NAME \
        --zone="europe-west1-b" \
        --image-family="common-cpu" \
        --image-project="deeplearning-platform-release" \
        --maintenance-policy="TERMINATE" \
        --scopes="storage-rw" \
        --machine-type="n1-standard-1" \
        --boot-disk-size="50GB" \
        --boot-disk-type="pd-standard"

If you have an issue with quota, use any of europe-west4-{a,b,c,d} or europe-west1-{b,c,d}

If you use the web interface, follow this

Question

Describe concisely to your past self what is a Virtual Machine and what is Google Compute Engine

3.2 - Connect using SSH to the instanceπŸ”—

If you are using the google cloud sdk in your computer or github codespace, you can connect to ssh using the usual command (refer to the first hands-on) with SSH Tunneling. FIRST CONNECT TO EDUROAM as eduroam allows connecting to ssh.

Tunnel the following ports to your local machine:

  • 8080: This is reserved for a jupyter lab session by default, it makes it easy to see & edit text
  • 8081: You will neeed to run containers and expose them on a port
Hint
gcloud compute ssh {user}@{instance} -- \
    -L {client-port}:localhost:{server-port} \
    -L {client-port-2}:localhost:{server-port-2}

Go to your browser and connect to http://localhost:8080, you should be in a jupyter lab where you can access a terminal, a text editor etc...

Question

Where is this jupyter lab hosted ? Describe concisely what is a SSH Tunnel and what is port forwarding

3.3 - Pull Docker Images from your teammatesπŸ”—

You should be inside the your VM,

Question

How to check that you're inside your VM ? On your terminal you should see user@hostname at the beginning. Hostname should be the name of your VM

3.4 - Run Docker Containers from their Docker ImagesπŸ”—

Hint

the port is not the same as yours
if you don't set the username, it will come to bite your later ;)

3.5 - Display the results & share themπŸ”—

  • You just launched a webapp on the port 8081 of your remote instance.

  • If you have a ssh tunnel directly from your laptop, ensure that you made a tunnel for your port 8081 to any port of your machine then, go to http://localhost:(your port) inside your browser. The resulting webpage should appear

  • If you are using github codespace, open web preview on port 8081 (you should have a tunnel running between your github codespace and your GCE instance)

  • The 8081 port has been opened to the internet, you can also connect to your machine's public ip address on the port 8081 (http//{ip}:8081) and you should see it.

How to get your public IP ? Go to the GCP interface and you can find it, or run gcloud compute instances list | grep {your instance name}

Note : This only works in 4G so use your mobile phone to check for the website

Success

  • The webpage should show the mascot your chose to run
  • The webpage should show the name of the author (not you)
  • The webpage should show your name

Bug

If any of the three item above are missing, find the bug and solve it :)

Example

Try to refresh the webpage to make more gifs appear

Share your result on slack

4. Cleanup the GCP projectπŸ”—

5. Yay !πŸ”—

Success

πŸŽ‰ you have successfully finished the mandatory part of the BE. You know how to manipulate the basic notions around cloud computing and docker so that you won't be completely lost when someone will talk about it

Continue the BE below (you can do it alone or by group of 2 or 3) to discover more nice things !

6. Let's discover StreamlitπŸ”—

We will now introduce streamlit, which is a very nice tool to build quick webapps in python !

In this TP you will build your first interactive webapp in python and package it in a container.

First, look at this video,

Then, take a look at an introduction to streamlit and the streamlit application gallery

Question

Can you describe what exactly is streamlit ? Could you find any way it could be useful to you ?

6.1. Your first streamlit applicationπŸ”—

Take a look at the code below,

import streamlit as st
from streamlit_image_comparison import image_comparison
import cv2

st.set_page_config("Webb Space Telescope vs Hubble Telescope", "πŸ”­")

st.header("πŸ”­ J. Webb Space Telescope vs Hubble Telescope")

st.write("")
"This is a reproduction of the fantastic [WebbCompare](https://www.webbcompare.com/index.html) app by [John Christensen](https://twitter.com/JohnnyC1423). It's built in Streamlit and takes only 10 lines of Python code. If you like this app, please star [John's original repo](https://github.com/JohnEdChristensen/WebbCompare)!"
st.write("")

st.markdown("### Southern Nebula")
image_comparison(
    img1="https://www.webbcompare.com/img/hubble/southern_nebula_700.jpg",
    img2="https://www.webbcompare.com/img/webb/southern_nebula_700.jpg",
    label1="Hubble",
    label2="Webb",
)


st.markdown("### Galaxy Cluster SMACS 0723")
image_comparison(
    img1="https://www.webbcompare.com/img/hubble/deep_field_700.jpg",
    img2="https://www.webbcompare.com/img/webb/deep_field_700.jpg",
    label1="Hubble",
    label2="Webb",
)

st.markdown("### Carina Nebula")
image_comparison(
    img1="https://www.webbcompare.com/img/hubble/carina_2800.png",
    img2="https://www.webbcompare.com/img/webb/carina_2800.jpg",
    label1="Hubble",
    label2="Webb",
)

st.markdown("### Stephan's Quintet")
image_comparison(
    img1="https://www.webbcompare.com/img/hubble/stephans_quintet_2800.jpg",
    img2="https://www.webbcompare.com/img/webb/stephans_quintet_2800.jpg",
    label1="Hubble",
    label2="Webb",
)

Question

Can you describe, by reading the documentation, what does the code do ?

6.2. Local deployment in codespaceπŸ”—

First, we will install in the codespace the dependencies for our application,

pip install streamlit streamlit opencv-python-headless

Then create a file streamlit_jswt.py and copy/paste the code above.

Then execute it streamlit run streamlit_jswt.py

This will launch the application on the port 8501 (by default) of our codespace. You can connect to it as usual.

🀩 Nice, isn't it ?

Now you can quit the server.

6.3. A more complex applicationπŸ”—

We will run and package a more complex application, but a lot more useful for your deep learning class

Clone the following repository git clone https://github.com/fchouteau/isae-demo-streamlit-activation-functions.git

cd to the directory cd isae-demo-streamlit-activation-functions then as last time, install the dependencies pip install -r requirements.txt then run the application streamlit run app.py

You can visualize it as last time. This should be quite useful for you given you just left the Deep Learning Class !

6.3. Transform application into docker imageπŸ”—

Refer to the previous TP where we built a website to convert what we just did into a docker image.

In short, create a Dockerfile that inherits from FROM python:3.10, copy all the app files COPY ./ /app/, install the dependencies RUN pip install -r /app/requirements.txt, expose the port EXPOSE 8501 then run as the app as an entrypoint CMD ["python", "-m", "streamlit", "run", "app.py"].

You should be able to do it yourself, but if you need help, here's what your Dockerfile looks like :

Solution
  FROM python:3.10

  COPY ./ /app/
  RUN pip install -r /app/requirements.txt

  EXPOSE 8501

  WORKDIR /app/

  CMD ["python", "-m", "streamlit", "run", "app.py"]

Then build your image, and run it locally (using the correct port forwarding which is 8501)

Solution
  # build
  docker build -t eu.gcr.io/sdd2324/streamlit-fch:1.0 -f Dockerfile . 
  # run
  docker run --rm -p 8501:8501 eu.gcr.io/sdd2324/streamlit-fch:1.0 # change this name to yours

Once you know it works locally, tag it and push it to our shared container registry

Solution
  # push to registry
  docker push eu.gcr.io/sdd2324/streamlit-fch:1.0 # change this name to yours

6.4. Deployment in a VMπŸ”—

We will now create yet another VM to deploy our application. This time, we will deploy directly our container in a VM without connecting to ssh to it,

Don't forget to change the instance name & zone according to what you did previously.

Take a note to the --container-image and change it to the name of the image you just pushed

gcloud compute instances create-with-container fch-streamlit-demo \
    --project=sdd2324 \
    --zone=europe-west1-b \
    --machine-type=n1-standard-1 \
    --image=projects/cos-cloud/global/images/cos-stable-109-17800-66-27 \
    --boot-disk-size=10GB \
    --boot-disk-type=pd-standard \
    --container-image=eu.gcr.io/sdd2324/streamlit-fch:2.0 \
    --container-restart-policy=always

Compared to previously, note that we explicitly specify a container to deploy to the VM and we don't use ubuntu but a container optimized OS.

Then, locate the public IP of your VM using the google cloud console.

Finally, take your phone (it won't work over ISAE wifi) and connect to its port 8501, http://ip-of-the-machine:8501

🧐 The app should appear !

We just deployed a webapp written in python to a public website :)

7. Yay !πŸ”—

Success

🍾 you have successfully finished the all parts of the BE. You know how to manipulate the basic notions around cloud computing and docker so that you won't be completely lost when someone will talk about it

Finish the previous hands-on (cloud & docker) if you have time. In particular, take a look at the docker-compose section.