---
title: "Lab 3: Docker"
subtitle: Biostat 203B
author: "Dr. Hua Zhou @ UCLA"
date: today
format:
  html:
    theme: cosmo
    embed-resources: true
    number-sections: true
    toc: true
    toc-depth: 4
    toc-location: left
    code-fold: false
knitr:
  opts_chunk: 
    echo: true
    fig.align: 'center'
---

A customized Docker container provides a self-contained Linux Ubuntu environment for reproducing materials in this course. This tutorial shows how to install Docker Desktop on your computer, run the Docker container, build your own Docker image, and push the image to Docker Hub.

This tutorial assumes that you have already installed R, RStudio, and Git on your computer. If not, please refer to the [Lab 1](https://ucla-biostat-203b.github.io/2024winter/labs/lab01/lab01.html).

## Install Docker Desktop

Download and install the Docker Desktop <https://www.docker.com/products/docker-desktop/>.  

## Run a Docker container 

- Open Terminal (Mac Terminal, Windows Git Bash, or Windows WSL) at the `/Docker` folder of the course material.

- To run a Docker container, we first modify the `volumes` section of the `docker-compose.yml` file to map the `203b-lecture`, `203-hw`, and `mimic` folders on your computer to the home directory in the Ubuntu system in Docker container. Then type
```{bash}
#| eval: false
docker-compose up
```
to run the Docker container. This can take up to 10 minutes for Docker to pull the [image](https://hub.docker.com/repository/docker/huazhou/ucla-biostat-203b-2024w/general), depending on internet connection. 

- Point your browser to `localhost:8787` to connect to the RStudio Server running on the Ubuntu system in the container.

## Build your own Docker image

- Modify the `Dockerfile.amd64` (Intel or AMD CPU) or `Dockerfile.arm64` (ARM CPU e.g. Apple M1/M2/M3 CPU) according to your computer architecture. 

- Open Terminal (Mac Terminal, Windows Git Bash, or Windows WSL) at the `/Docker` folder of course material. Type
```{bash}
#| eval: false
docker build -f Dockerfile.amd64 . -t [DOCKERHUB_USERNAME]/ucla_biostat_203b_2024w
```
to build the new Docker image. 

## Push the Docker image to Docker Hub

- Create a Docker Hub account at <https://hub.docker.com/>.

- Type
```{bash}
#| eval: false
docker push [DOCKERHUB_USERNAME]/ucla_biostat_203b_2024w
```
to push the new Docker image to Docker Hub.

## Run Docker container on Hoffman2 (TODO)

Follow the instructions [here](https://www.hoffman2.idre.ucla.edu/Using-H2/Software/Software.html#apptainer-workflow) to run the Docker container on Hoffman2.

To pull the image:
```{bash}
#| eval: false
apptainer pull docker://huazhou/ucla_biostat_203b_2024w
```
Run the container:
```{bash}
#| eval: false
apptainer shell ucla-biostat-203b-2024w_latest.sif
```
Not worked out yet.

## Run Docker container on cloud (TODO)

### Initial set up to push Docker image to GCR (Google Cloud Registry)

- Install [`Google Cloud CLI`](https://cloud.google.com/sdk/docs/install-sdk) on local machine (laptop or desktop).

- Initialize the gcloud CLI:
```{bash}
#| eval: false
gcloud init
```

- GCP authentication:
```{bash}
#| eval: false
gcloud auth configure-docker -q
```

- List existing Docker images and tag for gcr:
```{bash}
#| eval: false
docker image list
docker tag huazhou/ucla-biostat-203b-2024w gcr.io/biostat-203b-2024-winter/ubuntu-rstudio-server
```

- Push the image to gcr:
```{bash}
#| eval: false
docker push gcr.io/biostat-203b-2024-winter/ubuntu-rstudio-server 
```

### Run Docker container on GCP

- Create a GCE (Google Compute Engine) instance

- Create a firewall rule `rstudio-server` that opens the `8787` port and add Network tag `rstudio-server` to the GCE instance.

- Paste the GCR image name `gcr.io/biostat-203b-2024-winter/ubuntu-rstudio-server` to the `Container image` field of the GCE instance.

- Input Environment Variables: `PASSWORD: 203b`, `ROOT: true`, and `DISABLE_AUTH: true` to the GCE instance.

- Add volume and disks to the GCE instance. I haven't got the permissions right yet. The `/mnt/mimic` and `/home/studio/203b-lecture` appear to be owned by `root` and not writable by `rstudio`. (TODO)
