Docker quick start 3-docker image

Docker quick start 3-docker image

The Docker image contains the file system and its contents needed to start the container, so it is used to create and start the docker container.

The docker image adopts a layered construction mechanism, the lowest layer is bootfs, followed by rootfs.

  • Bootfs is a file system used for system boot, including bootloader and kernel. After the container is started, it will be unloaded to save memory resources.
  • rootfs is mounted by the kernel as a 只读mode, and then 联合挂载an additional 可写layer is mounted through technology

Docker image layer

Docer's mirrors are stacked layer by layer, the lower layer is called the parent mirror, and the bottom layer is called the base mirror.

The top layer is the 可读写layer, and the bottom 只读layer is the layer

on 和 overlayfs

aufs is an advanced multi-layered unification filesystem.

Both aufs and overlayfs are used for Linux file system implementation 联合挂载. In addition to these two, docker also supports file systems such as btrfs, devicemapper and vfs.

In the early version of Docer, aufs was used under ubuntu and devicemapper was used on CentOS7. The newer version of Docer now uses overlayfs, which has now been developed to the overlay2 version.

Docer image acquisition sequence: When a container is started, docker daemon will try to obtain the relevant image from the local. When the local image does not exist, it will download the image from the Registry and save it locally.

Very famous third-party mirror warehouse: https://quay.io/

Making images based on containers

root@node01:~# docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
nginx                    stable-alpine       8c1bfa967ebf        7 days ago          21.5MB
busybox                  latest              c7c37e472d31        2 weeks ago         1.22MB
quay.io/coreos/flannel   v0.12.0-amd64       4e9f801d2217        4 months ago        52.8MB

# 创建容器,并在容器内创建文件
root@node01:~# docker container run -i -t --name bbox-01 busybox:latest
/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ # mkdir -pv /data/html
created directory: '/data/'
created directory: '/data/html'
/ # echo "<h1>Hello Word!</h1>" > /data/html/index.html
/ # cat /data/html/index.html
<h1>Hello Word!</h1>

Create another terminal and make a mirror based on the container

root@node01:~# docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
787e4b3b2516        busybox:latest      "sh"                4 minutes ago       Up 4 minutes                            bbox-01
root@node01:~# docker container commit -p -m "Create /data/html/index.html" bbox-01 busybox-httpd:v0.1
sha256:806601ab556504829ff18d36ff308becab6ae351c7ae8f7b2adaecace52787bc
root@node01:~# docker image ls busybox*
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox-httpd       v0.1                806601ab5565        44 seconds ago      1.22MB
busybox             latest              c7c37e472d31        2 weeks ago         1.22MB

Exit the container bbox-01and delete it, and run a container with the created image

root@node01:~# docker container rm bbox-01

root@node01:~# docker container run -t -i --name bbox-http-01 busybox-httpd:v0.1
/ # ls data/html/
index.html
/ # cat data/html/index.html
<h1>Hello Word!</h1>
/ #

The new image /data/html/index.htmlis retained, so we made a new image based on the original image, which has more /data/html/index.htmlfiles than the original image .

An httpd service can be run in the busybox mirror. Now that the /data/html/index.htmlfile is created , the mirror can be made into a mirror that provides httpd services. The original image has a default startup shcommand, and you only need to modify the command that is running at startup to the httpdrelevant command.

root@node01:~# docker container ls
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS               NAMES
6d6e33d7acc4        busybox-httpd:v0.1   "sh"                5 minutes ago       Up 5 minutes                            bbox-http-01
root@node01:~# docker container commit -p -m "run httpd" -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' bbox-http-01 busybox-httpd:v0.2
sha256:985f056d206d3b3c3e1b62c66bcda672c5daf902245d9cf62e27a5bdd1de5b37
root@node01:~# docker container run -d --name bbox-httpd-02 busybox-httpd:v0.2
97ca3d6118ecbbb8ad5d4ec611b7ec425b66a959b663ea8f3dd689931e8930f4
root@node01:~# docker container ls
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS               NAMES
97ca3d6118ec        busybox-httpd:v0.2   "/bin/httpd -f -h /d…"   8 seconds ago       Up 6 seconds                            bbox-httpd-02
6d6e33d7acc4        busybox-httpd:v0.1   "sh"                     11 minutes ago      Up 11 minutes                           bbox-http-01

docker container commit -p -m "run httpd" -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' bbox-http-01 busybox-httpd:v0.2, Through -coptions, you can modify the running command when the container starts.

root@node01:~# docker container inspect bbox-httpd-02 | grep -i ipaddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.3",
                    "IPAddress": "172.17.0.3",
root@node01:~# curl http://172.17.0.3
<h1>Hello Word!</h1>

The busyboxmirror has been successfully made into a mirror that provides httpdservices.

Mirror upload and download

The locally made mirror can be uploaded to docker hub, aliyun的镜像平台or a private mirror warehouse, etc., so that the mirror can be shared with others.

Then REPOSITORYthis name should correspond to the name of the remote warehouse. Take Push to docker hubas an example. First, you need to register an docker hubaccount and create the corresponding warehouse, as shown below

Docker quick start 3-docker image

That REPOSITORYit should be zhaochj/httpd, that the offer httpdof busyboxa mirror to uploaddocker hub

Re-tag the corresponding image to meet the uploaded name requirements

root@node01:~# docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
busybox-httpd            v0.2                985f056d206d        32 minutes ago      1.22MB
busybox-httpd            v0.1                806601ab5565        47 minutes ago      1.22MB
nginx                    stable-alpine       8c1bfa967ebf        7 days ago          21.5MB
busybox                  latest              c7c37e472d31        2 weeks ago         1.22MB
quay.io/coreos/flannel   v0.12.0-amd64       4e9f801d2217        4 months ago        52.8MB
root@node01:~# docker image tag busybox-httpd:v0.2 zhaochj/httpd:v0.1
root@node01:~# docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
busybox-httpd            v0.2                985f056d206d        35 minutes ago      1.22MB
zhaochj/httpd            v0.1                985f056d206d        35 minutes ago      1.22MB
...

You need to log in before uploading

root@node01:~# docker login -u zhaochj
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
root@node01:~# docker image push zhaochj/httpd:v0.1
The push refers to repository [docker.io/zhaochj/httpd]
aba5952cfa04: Pushed
8b84b15db17e: Pushed
50761fe126b6: Mounted from library/busybox
v0.1: digest: sha256:63ecaa4990fb60f8aab701d690e5ef12cb664491753fee0208d080ca9ee8c28c size: 941

docker hubYou can view it on the top after the push is completed

Docker quick start 3-docker image

If it is used in mainland China, it is recommended to use the mirroring service in aliyun's development platform.

Image import and export

The local image can be exported as a compressed package, which can be imported after copying the compressed package to other hosts.

root@node01:~# docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
busybox-httpd            v0.2                985f056d206d        4 hours ago         1.22MB
zhaochj/httpd            v0.1                985f056d206d        4 hours ago         1.22MB
busybox-httpd            v0.1                806601ab5565        4 hours ago         1.22MB
nginx                    stable-alpine       8c1bfa967ebf        7 days ago          21.5MB
busybox                  latest              c7c37e472d31        2 weeks ago         1.22MB
quay.io/coreos/flannel   v0.12.0-amd64       4e9f801d2217        4 months ago        52.8MB

# 将多个镜像打包导出
root@node01:~# docker image save -o myimages.gz busybox:latest busybox-httpd:v0.1
root@node01:~# ls
myimages.gz

root@node01:~# docker image rm busybox:latest busybox-httpd:v0.1
Untagged: busybox:latest
Untagged: busybox@sha256:9ddee63a712cea977267342e8750ecbc60d3aab25f04ceacfa795e6fce341793
Untagged: busybox-httpd:v0.1
root@node01:~# docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
busybox-httpd            v0.2                985f056d206d        4 hours ago         1.22MB
zhaochj/httpd            v0.1                985f056d206d        4 hours ago         1.22MB
nginx                    stable-alpine       8c1bfa967ebf        7 days ago          21.5MB
quay.io/coreos/flannel   v0.12.0-amd64       4e9f801d2217        4 months ago        52.8MB
# 导入已打包的镜像
root@node01:~# docker image load -i myimages.gz
Loaded image: busybox:latest
Loaded image: busybox-httpd:v0.1
root@node01:~# docker image ls
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
busybox-httpd            v0.2                985f056d206d        4 hours ago         1.22MB
zhaochj/httpd            v0.1                985f056d206d        4 hours ago         1.22MB
busybox-httpd            v0.1                806601ab5565        4 hours ago         1.22MB
nginx                    stable-alpine       8c1bfa967ebf        7 days ago          21.5MB
busybox                  latest              c7c37e472d31        2 weeks ago         1.22MB
quay.io/coreos/flannel   v0.12.0-amd64       4e9f801d2217        4 months ago        52.8MB

Guess you like

Origin blog.51cto.com/zhaochj/2536160