Docker For Desktop のセットアップとエラー対応事例

# 概要

* Linux以外でDockerを使ったことがなかったのでWindows版の導入メモ
* 少し前まではDockerForWindowsという名称だった
* 所感として結構インストールつまづく印象
* インストールするとWindowsがすっごい重くなるので注意
* 使用できるようになるまでエラーいくつかエラーに遭遇したので対応メモも記載
* WindowsDockerはLinuxに比べて不安定な気がしてならない。エラーが多い。

# サインイン

* 以下からexeをダウンロード
https://www.docker.com/products/docker-desktop

* dockerhubのIDは作成済みの前提

<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/57350/3aff6269-eea6-8e9e-f0c8-c7583b61f9aa.png" width=50%>

<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/57350/4228e628-8ec0-1f8e-58ca-4871829e3137.png" width=40%>


# ダウンロード

<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/57350/2eec79ad-c2e7-b350-8c42-55f4de78e88f.png" width=80%>

# インストール

<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/57350/3379fedb-54b6-89b3-9ef5-a42471b5b021.png" width=80%>

<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/57350/ff6e150b-b14a-9c75-6203-b3611568f576.png" width=80%>

<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/57350/1401823b-f508-7238-906c-e08678a5b026.png" width=80%>

# セットアップ

* ステップに従って設定
* 画面の案内通りgit cloneでリソースをダウンロードしてくる

<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/57350/3cce93f2-3895-e51b-0c27-0496904da995.png" width=50%>


```
$ ls -l
total 8
drwxr-xr-x 1 ezaki 1049089 0 5月  24 17:44 birthday2019/
drwxr-xr-x 1 ezaki 1049089 0 5月  24 17:44 cheers2019/
```

* 画面の案内通りビルドをする
* ここで結構つまづく

<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/57350/c30a11d3-deac-1e35-6902-9e377ac254be.png" width=50%>

## 遭遇したエラー

### エラー：1

```
$ docker build -t hoge/cheers2019 .
unable to prepare context: unable to evaluate symlinks in Dockerfile path: GetFileAttributesEx C:\repo\doodle\Dockerfile: The system cannot find the file specif                                     ied.
```

### エラー：2

```
$ docker build -t hoge/cheers2019 .
error during connect: Post http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.39/buildbuildargs=%7B%7D&cachefrom=%5B%5D&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&
cpusetmems=&cpushares=0&dockerfile=Dockerfile&labels=%7B%7D&memory=0&memswap=0&networkmode=default&rm=1&session=nvkpxkntbjfq7qlbs19i0jtm5&shmsize=0&t=hoge%2Fcheers2019&target=&ulimits=null&version=1: open //./pipe/docker_engine:The system cannot find the file specified. In the default daemon configuration on Windows, the docker client must be run elevated to connect. This error may also indicate that the docker daemon is not running.
```

* 以下の記事などを参考

https://qiita.com/k_akimoto/items/182418e2d200616e7db6

https://qiita.com/itrec0626/items/aa4d1521b5a7cc20ffad

#　ビルド出力

* ビルドが成功すると以下のような出力

```
$ cd /c/repo/doodle/cheers2019/;docker build -t hoge/cheers2019 .
Sending build context to Docker daemon  13.31kB
Step 1/9 : FROM golang:1.11-alpine AS builder
1.11-alpine: Pulling from library/golang
e7c96db7181b: Pulling fs layer
5297bd381816: Pulling fs layer
3a664477889c: Pulling fs layer
f235158035b8: Pulling fs layer
0ebee1528520: Pulling fs layer
f235158035b8: Waiting
0ebee1528520: Waiting
3a664477889c: Verifying Checksum
3a664477889c: Download complete
5297bd381816: Verifying Checksum
5297bd381816: Download complete
e7c96db7181b: Verifying Checksum
e7c96db7181b: Download complete
0ebee1528520: Verifying Checksum
0ebee1528520: Download complete
e7c96db7181b: Pull complete
5297bd381816: Pull complete
3a664477889c: Pull complete
f235158035b8: Verifying Checksum
f235158035b8: Download complete
f235158035b8: Pull complete
0ebee1528520: Pull complete
Digest: sha256:ef839362daebf5e5b03113d48a7f02f42f5090ccf4eb5f2e915db3fc1307bdbd
Status: Downloaded newer image for golang:1.11-alpine
 ---> 419986b2b50a
Step 2/9 : RUN apk add --no-cache git
 ---> Running in 34d76c6cd0b6
fetch http://dl-cdn.alpinelinux.org/alpine/v3.9/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.9/community/x86_64/APKINDEX.tar.gz
(1/6) Installing nghttp2-libs (1.35.1-r0)
(2/6) Installing libssh2 (1.8.2-r0)
(3/6) Installing libcurl (7.64.0-r1)
(4/6) Installing expat (2.2.6-r0)
(5/6) Installing pcre2 (10.32-r1)
(6/6) Installing git (2.20.1-r0)
Executing busybox-1.29.3-r10.trigger
OK: 20 MiB in 21 packages
Removing intermediate container 34d76c6cd0b6
 ---> 9cb059355c9a
Step 3/9 : RUN go get github.com/pdevine/go-asciisprite
 ---> Running in e62fbe601402
Removing intermediate container e62fbe601402
 ---> d3cc7008df60
Step 4/9 : WORKDIR /project
 ---> Running in ea3a89875300
Removing intermediate container ea3a89875300
 ---> d637b4e5abdc
Step 5/9 : COPY cheers.go .
 ---> e87625cc8a98
Step 6/9 : RUN CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' -o cheers cheers.go
 ---> Running in 46ebb8eae832
Removing intermediate container 46ebb8eae832
 ---> b96addf52dc8
Step 7/9 : FROM scratch
 --->
Step 8/9 : COPY --from=builder /project/cheers /cheers
 ---> 726445b04079
Step 9/9 : ENTRYPOINT ["/cheers"]
 ---> Running in 07124cd1d54f
Removing intermediate container 07124cd1d54f
 ---> 790a722d67bc
Successfully built 790a722d67bc
Successfully tagged hoge/cheers2019:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
```

* コンテナ作成

```
$ docker run -it --rm hoge/cheers2019
the input device is not a TTY.  If you are using mintty, try prefixing the command with 'winpty'

$ winpty docker run -it --rm hoge/cheers2019
```

* 成功すると以下のような出力

<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/57350/deb73306-3e40-78b6-b27c-fcd501f1e279.png" width=50%>

* ログイン

<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/57350/dd20f8b0-b7b7-ef3d-2c40-40d9651e23b4.png" width=50%>


```
$ docker login ; docker push hoge/cheers2019
Authenticating with existing credentials...
Login Succeeded
The push refers to repository [docker.io/hoge/cheers2019]
83bd27c95501: Preparing
83bd27c95501: Pushed
latest: digest: sha256:a2680c711706d56aad980e7d5ae8a1714e62f06bf02e7544d51fa7b740d7c3a1 size: 528

```

# 終わり

* 導入成功

<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/57350/c4f6264c-f097-d7a4-e1ae-befa678d21b0.png" width=50%>


# 動作確認

## 遭遇したエラー

### エラー1

```
>docker run hello-world
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).
See 'docker run --help'.

```

* どうやらDNSのエラーが有力らしい
* 初期設定では8.8.8.8に設定されている
* ipconfig /allで出力されるDNSに変更する
* この場合172.22.21.21に変更したところエラー解決

<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/57350/b0fb0003-27d0-b7e1-074a-cc8db0d7b18a.png" width=80%>

```
>docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:0e11c388b664df8a27a901dce21eb89f11d8292f7fca1b3e3c4321bf7897bffe
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/
```

### エラー2

```
$ docker login
Authenticating with existing credentials...
Login did not succeed, error: Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
Error: Cannot perform an interactive login from a non TTY device
```

* これもしばしば遭遇する
* 認証に失敗しているのかなと思いきや違うらしい
* いろいろ試したが右下dockerアイコンからRestartしたら治った。。なぜなのかは不明。

### エラー3

* Gitbashでやる場合はホスト側のパス先頭にスラッシュをもう一つつける必要があるらしい

```
$ docker run --name test-nginx -v /c/Users/hoge/docker-test/html:/usr/share/nginx/html:ro -d -p 8080:80 nginx
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Mount denied:
The source path "C:/Users/hoge/docker-tutrial/html;C"
doesn't exist and is not known to Docker.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
```

```
$ docker run --name test-nginx -v //c/Users/hoge/docker-test/html:/usr/share/nginx/html:ro -d -p 8080:80 nginx
7c40c6103be5ad1257d846cf6f349c2e6de1ee29fa008a167556e023c11534a9
```

### エラー4


```
$ docker create --name test -it alpine /bin/sh
aed6850f35d5a4237abf0768a4f0be9f56737d1930c602a293ab0122b392a9ba

$ docker ps -a
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS                  PORTS               NAMES
aed6850f35d5        alpine               "C:/Program Files/Gi…"   20 seconds ago      Created                                     test

$ docker start test
Error response from daemon: OCI runtime create failed: container_linux.go:344: starting container process caused "exec: \"C:/Program Files/Git/usr/bin/sh\": stat C:/Program Files/Git/usr/bin/sh: no such file or directory": unknown
Error: failed to start containers: test
```

* システムデフォルトのshではなく、意図しないGitBash内のshを指定しまうためエラーになるっぽい。。
* 引数にshを指定せず実行すると成功する

```
$ which sh
/usr/bin/sh

$ docker create --name test-2 -it alpine
9997f001960caa16326c6a0e2e543660f141b2e913b14efcf4f31916346be3c9

$ docker ps -a
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS                  PORTS               NAMES
9997f001960c        alpine               "/bin/sh"                10 seconds ago      Created                                     test-2
aed6850f35d5        alpine               "C:/Program Files/Gi…"   24 minutes ago      Created                                     test

$ docker start test-2
test-2

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
9997f001960c        alpine              "/bin/sh"           About a minute ago   Up 5 seconds                            test-2



```