docker

Docker学习(8) 应用的容器化

2019-10-27  本文已影响0人  August________

Docker学习(8) 应用的容器化

应用的容器化——简介

  1. 编写应用代码
  2. 创建一个Dockerfile,其中包括对应用的描述和依赖以及如何运行依赖。
  3. 对Dockerfile执行docker image build 命令。
  4. 等待docker将应用程序部署到docker镜像中

应用容器化——详解

单体应用容器化

  1. 获取应用代码
  2. 分析Dockerfile
  3. 构建应用应用镜像
  4. 运行该应用
  5. 测试应用
  6. 容器应用化细节
  7. 生产环境中多阶段构建
  8. 最近实践

获取应用代码

lhf@lhf-virtual-machine:~/docker/psweb-master$ ls -l
总用量 28
-rw-rw-r-- 1 lhf lhf  341 10月 23 23:11 app.js
-rw-rw-r-- 1 lhf lhf  216 10月 23 23:11 circle.yml
-rw-rw-r-- 1 lhf lhf  338 10月 23 23:11 Dockerfile
-rw-rw-r-- 1 lhf lhf  421 10月 23 23:11 package.json
-rw-rw-r-- 1 lhf lhf  370 10月 23 23:11 README.md
drwxrwxr-x 2 lhf lhf 4096 10月 23 23:11 test
drwxrwxr-x 2 lhf lhf 4096 10月 23 23:11 views

分析Dockerfile

lhf@lhf-virtual-machine:~/docker/psweb-master$ cat Dockerfile 
# Test web-app to use with Pluralsight courses and Docker Deep Dive book
# Linux x64
FROM alpine

LABEL maintainer="nigelpoulton@hotmail.com"

# Install Node and NPM
RUN apk add --update nodejs nodejs-npm

# Copy app to /src
COPY . /src

WORKDIR /src

# Install dependencies
RUN  npm install

EXPOSE 8080


FROM alpine
LABEL maintainer="nigelpoulton@hotmail.com"
RUN apk add --update nodejs nodejs-npm
COPY . /src
WORKDIR /src
RUN npm install
EXPOSE 8080
ENTRYPOINT ["node", "./app.js"]

容器化当前应用/构建具体的镜像

lhf@lhf-virtual-machine:~/docker/psweb-master$ docker image build -t web:latest .
Sending build context to Docker daemon  10.24kB
Step 1/8 : FROM alpine
 ---> 965ea09ff2eb
Step 2/8 : LABEL maintainer="nigelpoulton@hotmail.com"
 ---> Running in dbeaf3fc9a29
Removing intermediate container dbeaf3fc9a29
 ---> 8f89a8028659
Step 3/8 : RUN apk add --update nodejs nodejs-npm
 ---> Running in 5f223f358f2b
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/community/x86_64/APKINDEX.tar.gz
(1/8) Installing ca-certificates (20190108-r0)
(2/8) Installing c-ares (1.15.0-r0)
(3/8) Installing libgcc (8.3.0-r0)
(4/8) Installing http-parser (2.9.2-r0)
(5/8) Installing libstdc++ (8.3.0-r0)
(6/8) Installing libuv (1.29.1-r0)
(7/8) Installing nodejs (10.16.3-r0)
(8/8) Installing npm (10.16.3-r0)
Executing busybox-1.30.1-r2.trigger
Executing ca-certificates-20190108-r0.trigger
OK: 59 MiB in 22 packages
Removing intermediate container 5f223f358f2b
 ---> 717eea061e67
Step 4/8 : COPY . /src
 ---> be52499f2ad8
Step 5/8 : WORKDIR /src
 ---> Running in 9773587f150e
Removing intermediate container 9773587f150e
 ---> 9ec49b1b00b7
Step 6/8 : RUN  npm install
 ---> Running in 30f2281b50d9
npm WARN deprecated superagent@3.8.3: Please note that v5.0.1+ of superagent removes User-Agent header by default, therefore you may need to add it yourself (e.g. GitHub blocks requests without a User-Agent header).  This notice will go away with v5.0.2+ once it is released.
npm WARN deprecated json3@3.3.2: Please use the native JSON object instead of JSON 3

> core-js@2.6.10 postinstall /src/node_modules/core-js
> node postinstall || echo "ignore"

Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!

The project needs your help! Please consider supporting of core-js on Open Collective or Patreon: 
> https://opencollective.com/core-js 
> https://www.patreon.com/zloirock 

Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)

npm notice created a lockfile as package-lock.json. You should commit this file.
added 163 packages from 460 contributors and audited 262 packages in 12.855s
found 5 vulnerabilities (2 low, 2 moderate, 1 critical)
  run `npm audit fix` to fix them, or `npm audit` for details
Removing intermediate container 30f2281b50d9
 ---> bc36c1226008
Step 7/8 : EXPOSE 8080
 ---> Running in 1c3b6c00bc53
Removing intermediate container 1c3b6c00bc53
 ---> 3520f6d7fa42
Step 8/8 : ENTRYPOINT ["node", "./app.js"]
 ---> Running in 579d7cd7499c
Removing intermediate container 579d7cd7499c
 ---> 983497a6f68f
Successfully built 983497a6f68f
Successfully tagged web:latest
lhf@lhf-virtual-machine:~/docker/psweb-master$ docker image ls
REPOSITORY                           TAG                 IMAGE ID            CREATED              SIZE
web                                  latest              983497a6f68f        About a minute ago   71.4MB
alpine                               latest              965ea09ff2eb        5 days ago           5.55MB
ubuntu                               latest              cf0f3ca922e0        8 days ago           64.2MB
nigelpoulton/pluralsight-docker-ci   latest              07e574331ce3        4 years ago          557MB

推送镜像到仓库

lhf@lhf-virtual-machine:~/docker/psweb-master$ 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: lhfdocker
Password: 

Login Succeeded

lhf@lhf-virtual-machine:~/docker/psweb-master$ docker image tag web:latest lhfdocker/web:latest
lhf@lhf-virtual-machine:~/docker/psweb-master$ docker image ls
REPOSITORY                           TAG                 IMAGE ID            CREATED             SIZE
web                                  latest              983497a6f68f        17 minutes ago      71.4MB
lhfdocker/web                        latest              983497a6f68f        17 minutes ago      71.4MB
alpine                               latest              965ea09ff2eb        5 days ago          5.55MB
ubuntu                               latest              cf0f3ca922e0        8 days ago          64.2MB
nigelpoulton/pluralsight-docker-ci   latest              07e574331ce3        4 years ago         557MB
lhf@lhf-virtual-machine:~/docker/psweb-master$ docker image push lhfdocker/web:latest
The push refers to repository [docker.io/lhfdocker/web]
682d0d0165f0: Pushed 
c49e4ca7e0e1: Pushed 
5bffe4c78afd: Pushed 
77cae8ab23bf: Mounted from library/alpine 
latest: digest: sha256:6bbab65900a19963b1611db15aa11ddacb1ff03803b716e11372adf23c74c6c5 size: 1159
8.png

运行程序应用

lhf@lhf-virtual-machine:~/docker/psweb-master$ docker container run -d --name c1 -p 80:8080 lhfdocker/web:latest
eefa35f2eba5d28ec3776cdd525629f3fd99594cddc87be17458c674ef84c268
lhf@lhf-virtual-machine:~/docker/psweb-master$ docker container  ls
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS              PORTS                  NAMES
eefa35f2eba5        lhfdocker/web:latest   "node ./app.js"     7 seconds ago       Up 6 seconds        0.0.0.0:80->8080/tcp   c1

详述

lhf@lhf-virtual-machine:~/docker/psweb-master$ docker image history lhfdocker/web:latest
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
983497a6f68f        35 minutes ago      /bin/sh -c #(nop)  ENTRYPOINT ["node" "./app…   0B                  
3520f6d7fa42        35 minutes ago      /bin/sh -c #(nop)  EXPOSE 8080                  0B                  
bc36c1226008        35 minutes ago      /bin/sh -c npm install                          20.6MB              
9ec49b1b00b7        36 minutes ago      /bin/sh -c #(nop) WORKDIR /src                  0B                  
be52499f2ad8        36 minutes ago      /bin/sh -c #(nop) COPY dir:8abfedb4af4f697c8…   2.29kB              
717eea061e67        36 minutes ago      /bin/sh -c apk add --update nodejs nodejs-npm   45.3MB              
8f89a8028659        36 minutes ago      /bin/sh -c #(nop)  LABEL maintainer=nigelpou…   0B                  
965ea09ff2eb        5 days ago          /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B                  
<missing>           5 days ago          /bin/sh -c #(nop) ADD file:fe1f09249227e2da2…   5.55MB 
lhf@lhf-virtual-machine:~/docker/psweb-master$ docker image inspect lhfdocker/web:latest
。。。。。。。
<snip>
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:77cae8ab23bf486355d1b3191259705374f4a11d483b24964d2f729dd8c076a0",
                "sha256:5bffe4c78afdb55eb39eb33c8956498ce3a18aa500747ece811e2afbf9c7a59f",
                "sha256:c49e4ca7e0e188876dc450dffc3087fd188988bedc41236243fd7354e3d9de34",
                "sha256:682d0d0165f04005b9475623c79dea3606ee42d7307bbd33b35d72db91fe09d8"
            ]
        },
        "Metadata": {
            "LastTagTime": "2019-10-27T15:05:46.793024217+08:00"
        }
    }

容器应用化的——命令

上一篇 下一篇

猜你喜欢

热点阅读