---
# System prepended metadata

title: 7/21 Lab Meeting SOP

---

# 7/21 Lab Meeting SOP

## DIY - Docker
host: 10.21.21.5
### Docker at local host
- List images
```bash=
docker image ls
```
```bash=
docker images
```
- List containers
```bash=
docker container ls
```
- Pull image from the docker hub registry
```bash=
docker pull ubuntu:latest
```
- Use an existed image to run a container
```bash=
docker run --name mycontainer -it ubuntu /bin/bash
```
> To detatch from the container without stopping it press CTRL+P followed by CTRL+Q.
> -i: keep STDIN open even if not attached
> -t: allocate a pseudo-tty
>> For interactive processes (like a shell), you must use -i -t together in order to allocate a tty for the container process.
- Attach to a running container
```bash=
docker attach mycontainer
```
- Pause a running container
```bash=
docker pause mycontainer
```
- Kill a running container
```bash=
docker kill mycontainer
```
- Remove a stopped/killed container
```bash=
docker rm mycontainer
```
#### Creat a docker image with an existed container
- Step 1 : Creat the image from a running container
```bash=
docker commit mycontainer
```
> Use ```docker images``` to see the ID of image which just created. The default name will be "none".
- Step 2 : Rename the image file
```bash=
docker tag image-ID my_image
```
- ==Or== you can tag the image name as it is created
```bash=
docker commit mycontainer my_image
```

### Dockerfile
- write your own script for container
```dockerfile=
FROM ubuntu:latest
MAINTAINER phoebe
RUN apt-get update || apt-get upgrade
RUN apt-get install -y vim
RUN apt-get install -y git
RUN apt-get install -y iproute2
RUN apt-get install -y net-tools
```
- 常用指令
  - FROM： 使用到的 Docker Image
  - MAINTAINER： 用來說明撰寫和維護這個 Dockerfile 的人是誰
  - RUN： 指令後面放 Linux 指令，用來執行安裝和設定這個 Image 需要的東西
  - ADD： 把 Local 的檔案複製到 Image 裡，如果是 tar.gz 檔複製進去 Image 時會順便自動解壓縮
  - ENV： 用來設定環境變數

- Build Docker Image
```dockerfile=
docker build -t image_name . --no-cache
```
> --no-cache: do not use cache when building the image [Understanding the Docker Cache for Faster Builds](https://thenewstack.io/understanding-the-docker-cache-for-faster-builds/)
> Remember to use ```docker tag``` to rename the image with image-ID
- Run new container via this image
```dockerfile=
docker run --name mycontainer -it -P image_name /bin/bash
```
> -P: publish all exposed ports to the host interfaces 

### Docker Hub
- Login your docker account
```
docker login
```
- If your OS is ubuntu, docker login will get error massage
```bash=
sudo apt install gnupg2 pass
gpg2 --full-generate-key
gpg2 -k
pass init "<input the path appeared in previous command>"
```

- Tag the image you want to push, remember to add your docker hub account in front of repository name
```bash=
docker tag image_name dockerhub_account/new-repo:tagname
```
- Push your own image to a registry
```bash=
docker push dockerhub_account/new-repo:tagname 
```
- Pull it
```bash=
docker pull dockerhub_account/new-repo:tagname
```

### Network of Docker container
- Bridge
  - It is the Docker default networking mode which will enable the connectivity to the other interfaces of the host machine as well as among containers.
```bash=
docker run -it --network=bridge ubuntu:latest /bin/bash
```
- Host
  - In this mode container will share the host’s network stack and all interfaces from the host will be available to the container. The container’s host name will match the host name on the host system
  - Even the IP configuration is same as the host system's IP configuration
```bash=
docker run -it --net=host ubuntu:latest /bin/bash
```
- None
  - This mode will not configure any IP for the container and doesn’t have any access to the external network as well as for other containers.
```bash=
docker run -it --network=none ubuntu:latest /bin/bash
```
#### Create network(subnet) in your docker host
- Create your own docker network
```dockerfile=
docker network create --subnet=172.20.0.0/16 mynet
```
- Run the image
```dockerfile=
docker run --name testnet --net mynet --ip 172.20.0.11 -it ubuntu /bin/bash
```
## DIY - K8s
- Check nodes already connected to master
```bash=
kubectl get nodes
```
### Pod
```yaml=
apiVersion: v1
kind: Pod
metadata:
  name: kubernetes-demo-pod
  labels:
    app: demoApp
spec:
  containers:
    - name: kubernetes-demo-container
      image: hcwxd/kubernetes-demo
      ports:
        - containerPort: 3000
```
> apiVersion: 元件版本號
> kind: 元件類型
> metadata: 元件基本設定(名稱 name、分類 labels)
> spec: 元件組成(容器設置 containers)
>> containerPort: 容器開放外部訪問的端口
- Create pod with yaml file
```bash=
kubectl create -f demo_pod.yaml
```
- Check created pods
```bash=
kubectl get pods
```
- Port mapping between localhost and container
```bash=
kubectl port-forward kubernetes-demo-pod 3000:3000
```
### Service
```yaml=
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec: 
  selector:
    app: demoApp
  ports:
    - protocol: TCP
      port: 3000
	  nodePort: 30390
      targetPort: 3000
```
> port: service's port，會 mapping 到 targetPort。 `<ClusterIP>:<port>`
> nodePort: 端口範圍只能是30000-32767，不建議手動設定，由系統自行分配。 `<NodeIP>:<nodePort>`
> targetPort: pod's port
- Create service with yaml file
```bash=
kubectl create -f demo_service.yaml
```
- Check created pods
```bash=
kubectl get services
```
### Ingress
```yaml=
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: web
spec:
  rules:
    - host: demo.com
      http:
        paths:
          - backend:
              serviceName: my-service
              servicePort: 80
```
> name: must be a valid **DNS subdomain name**(contains no more than 253 characters, only lowercase alphanumeric characters, '-' or '.').
> rules: define routing rules
- Create ingress with yaml file
```bash=
kubectl create -f demo_ingress.yaml
```
- Check created ingress
```bash=
kubectl get ingress
```