# DOCKER NOTES
###### tags: `SA`

## WHAT'S DOCKER
https://www.youtube.com/watch?v=rOTqprHv1YE
### Conatiners :
The industry standard today is to use Virtual Machines (VMs) to run software applications. VMs run applications inside a guest Operating System, which runs on virtual hardware powered by the server’s host OS.
VMs are great at providing full process isolation for applications: there are very few ways a problem in the host operating system can affect the software running in the guest operating system, and vice-versa. But this isolation comes at great cost — the computational overhead spent virtualizing hardware for a guest OS to use is substantial.
Containers take a different approach: by leveraging the low-level mechanics of the host operating system, containers provide most of the isolation of virtual machines at a fraction of the computing power.
## DOCKER INSTALL
* Install
```
sudo apt install curl
curl -fsSL "https://get.docker.com/" | sh
```
* Start docker service
```
sudo systemctl start docker
```
* Optional: To use Docker as a non-root user, you should add your user to the "docker" group
```
sudo usermod -aG docker [your_username]
```
<br>
## DOCKER USAGE
### run ubuntu cotainer ( the begining )
```
docker run -ti ubuntu bash
```
### detach docker (can still be attach later)
```
press Ctrl+P and Ctrl+Q
```
### attach docker
(id is the first six or seven predix)
```
docker exec -ti [id] bash
```
### start container
```
docker start [id]
```
### stop container
```
docker stop [id]
```
### list all container
```
docker ps
```
### remove specific container
```
docker rm [id]
```
### remove specific image
```
docker rmi [id]
```
### rename a conatiner
```
sudo docker rename oldname newname
```
### copy a file from container to host
```
docker cp <containerId>:/file/path/within/container /host/path/target
```
<br>
## DOCKER FILE TUTORIAL
1. set up a file name : Dockerfile (not folder)
2. edit the Dockerfile
- Basic Format of Dockerfile :
- FROM : always start by FROM. Specify which basic docker image (parent image)
- RUN : run the command in the client
- COPY: copy executes on the host into container (client)
- CMD : command to run the final program (intial command)
- more reference : [docker offical website](https://docs.docker.com/engine/reference/builder/#environment-replacement)
3. bult the docker image
```
docker build -t image_name:version location
```
4. Run the image
```
docker run -ti image bash
```
Example :
```dockerfile
FROM ubuntu:18.04
RUN mkdir -p /home/app
RUN apt-get update && apt-get install -y --no-install-recommends \
python3.5 \
python3-pip \
&& \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
COPY . /home/app
CMD ["python3","main.py"]
```
## DOCKER HUB
Docker Hub 如同 Github。Github上 有許多的專案,每個人都可以上傳自己的專案,也可以下載別人的專案,有時也可以在某些開源專案,提問或提交自己的程式碼。同時,Github 也提供私有與公開的專案類型,如果專案不想被公開,則需要申請付費帳號。而 Docker Hub 則提供免費帳號一個私有 Repository 的 quota。
## PUSH IMAGE TO DOCKER HUB
[reference](https://ithelp.ithome.com.tw/articles/10191139)
```
docker tag ${Image Name:tag} DockerHub_account/Image_Name
docker login
docker push DockerHub_account/Image_Name
```
## CMD FOR GUI
```
docker run --rm -ti --runtime=nvidia --net=host -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY --volume="$HOME/.Xauthority:/root/.Xauthority:rw" -e XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR chentzj/open_pose:01
```