# 在 Ubuntu 20.04 上使用 Docker 的筆記
###### tags: `Ubuntu` `Docker` `Registry`
## 安裝 Docker
使用 [rootless mode](https://docs.docker.com/engine/security/rootless/) 來安裝
```shell=
$ curl -fsSL https://get.docker.com/rootless | sh
...
# Docker binaries are installed in /home/testuser/bin
# WARN: dockerd is not in your current PATH or pointing to /home/testuser/bin/dockerd
# Make sure the following environment variables are set (or add them to ~/.bashrc):
export PATH=/home/testuser/bin:$PATH
export PATH=$PATH:/sbin
export DOCKER_HOST=unix:///run/user/1000/docker.sock
#
# To control docker service run:
# systemctl --user (start|stop|restart) docker
```
如果需要使用 insecure-registries 就去修改 ~/.config/systemd/user/docker.service
```
ExecStart=/home/testuser/bin/dockerd-rootless.sh --insecure-registry=localhost:5000 --insecure-registry=192.168.111.111:9527
```
然後設定一下自動啟動
```shell=
$ systemctl --user daemon-reload
$ systemctl --user start docker
$ systemctl --user enable docker
$ sudo loginctl enable-linger $(whoami)
```
接著要設定一下使用參數
```shell=
$ docker context create rootless --description "for rootless mode" --docker "host=unix://$XDG_RUNTIME_DIR/docker.sock"
$ docker context use rootless
```
然後應該就可以使用 docker 來跑點東西
```shell=
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:e7c70bb24b462baa86c102610182e3efcb12a04854e8c582838d92970a09f323
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
```
## 安裝 Docker Registry
首先參考 [Install Docker Compose
](https://docs.docker.com/compose/install/) 把 Docker Compose 安裝好
```shell
$ curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o ~/bin/docker-compose
$ chmod +x ~/bin/docker-compose
```
然後準備一個 docker-compose.yml 的檔案
```yaml=
version: '3'
services:
registry:
image: registry:2
ports: ["5000:5000"]
environment:
REGISTRY_HTTP_ADDR: 0.0.0.0:5000
```
然後啟動它
```shell=
$ docker-compose up -d
Creating network "testuser_default" with the default driver
Pulling registry (registry:2)...
2: Pulling from library/registry
cbdbe7a5bc2a: Pull complete
47112e65547d: Pull complete
46bcb632e506: Pull complete
c1cc712bcecd: Pull complete
3db6272dcbfa: Pull complete
Digest: sha256:8be26f81ffea54106bae012c6f349df70f4d5e7e2ec01b143c46e2c03b9e551d
Status: Downloaded newer image for registry:2
Creating testuser_registry_1 ... done
```
接下來將剛剛抓到的 hello-world 推上去
```shell=
$ docker image tag hello-world localhost:5000/hello-world
$ docker image push localhost:5000/hello-world
Using default tag: latest
The push refers to repository [localhost:5000/hello-world]
9c27e219663c: Pushed
latest: digest: sha256:90659bf80b44ce6be8234e6ff90a1ac34acbeb826903b02cfa0da11c82cbc042 size: 525
```