Docker Images (メモ)

## What is Docker Images?
- Metadata about the image data and how to run the image
- Not a complete OS. No kernel, kernel modules
- Small as one file 


## Image layers
```bash:understanding-docker-images-layers
vagrant@bionic64:~$ docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
nginx                   latest              881bd08c0b08        13 months ago       109MB

# history of the image layers
# every image starts from the very beginning with a blank layer known as scratch
# every set of changes that happens after that on the file system, in the images, is another layer
vagrant@bionic64:~$ docker history nginx:latest
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
881bd08c0b08        13 months ago       /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B
<missing>           13 months ago       /bin/sh -c #(nop)  STOPSIGNAL SIGTERM           0B
<missing>           13 months ago       /bin/sh -c #(nop)  EXPOSE 80                    0B
<missing>           13 months ago       /bin/sh -c ln -sf /dev/stdout /var/log/nginx…   22B
<missing>           13 months ago       /bin/sh -c set -x  && apt-get update  && apt…   54MB
<missing>           13 months ago       /bin/sh -c #(nop)  ENV NJS_VERSION=1.15.9.0.…   0B
<missing>           13 months ago       /bin/sh -c #(nop)  ENV NGINX_VERSION=1.15.9-…   0B
<missing>           13 months ago       /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B
<missing>           13 months ago       /bin/sh -c #(nop)  CMD ["bash"]                 0B
<missing>           13 months ago       /bin/sh -c #(nop) ADD file:5ea7dfe8c8bc87ebe…   55.3MB


# <missing> : the other layers inside the image (aren't actually images themselves), so they wouldn't necessarily get their own image ID
```

It's not need to download the layers that already have, and it uses SHA for each layer so it's guaranteed to be the exact layer it needs.
When uploading and downloading images, we don't need to upload and download the same layers that we already have on the other side.


Ex: I have my own image and it was custom, i added apache server on top as another layer in my Dockerfile. Then i open up port 80, at the very end, copy in the source code for web A & B. It ends to have 2 Dockerfile, with only copy A & B different.
The actually stored is custom, apache, port, copy A, copy B. It's no storing the entire stack of image layers more than once if it's really the same layers. 

## Re-tag existing images
```bash:understanding-docker-images-layers
vagrant@bionic64:~$ docker image ls
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
nginx                   latest              881bd08c0b08        13 months ago       109MB

vagrant@bionic64:~$ docker image tag --help

Usage:  docker image tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

# added a new tag to an existing image that i didn't make (it shows the same image id)
vagrant@bionic64:~$ docker image ls
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
nginx                   latest              881bd08c0b08        13 months ago       109MB
anherchndr/nginx          latest              881bd08c0b08        13 months ago       109MB

# docker image push : uploads changed layers to an image registry (default is Hub)
vagrant@bionic64:~$ docker push xxx/nginx
The push refers to repository [docker.io/xxx/nginx]
3e9eb35b1c23: Mounted from library/nginx
c59b3ca455e3: Mounted from library/nginx
6744ca1b1190: Mounted from library/nginx
latest: digest: sha256:7734a210432278817f8097acf2f72d20e2ccc7402a0509810c44b3a8bfe0094a size: 948

vagrant@bionic64:~$ docker image tag xxx/nginx xxx/nginx:testing

vagrant@bionic64:~$ docker push xxx/nginx:testing
The push refers to repository [docker.io/xxx/nginx]
3e9eb35b1c23: Layer already exists
c59b3ca455e3: Layer already exists
6744ca1b1190: Layer already exists
testing: digest: sha256:7734a210432278817f8097acf2f72d20e2ccc7402a0509810c44b3a8bfe0094a size: 948
```

## Troubleshooting
- Can't login to dockerhub

```bash:install-gnupg2-pass
vagrant@bionic64:~$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: xxx
Password:
Error saving credentials: error storing credentials - err: exit status 1, out: `Cannot autolaunch D-Bus without X11 $DISPLAY`

vagrant@bionic64:~$ sudo apt -y install gnupg2 pass

vagrant@bionic64:~$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: XXX
Password:
WARNING! Your password will be stored unencrypted in /home/vagrant/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
```