栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

SAAS 开发技术项目实验(二)

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SAAS 开发技术项目实验(二)

文章目录
  • Docker 快速入门
    • Ubuntu 镜像操作
    • 构建在 Ubuntu 系统上运行 Nginx 的镜像
      • 使用 docker commit 命令基于容器构建镜像
      • 使用 docker build 命令基于 Dockerfile 构建镜像
      • 测试镜像构建的缓存

Docker 快速入门 Ubuntu 镜像操作

拉取最新的Ubuntn官方镜像

[root@host xwk]# docker pull ubuntu:latest
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete 
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

查看该镜像的详细信息

docker inspect ubuntu

查看该镜像的构建历史

[root@host xwk]# docker history ubuntu
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
ba6acccedd29        3 weeks ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
           3 weeks ago         /bin/sh -c #(nop) ADD file:5d68d27cc15a80653…   72.8MB              
[root@host xwk]# 

删除该镜像

[root@host xwk]# docker rmi ubuntu
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Deleted: sha256:ba6acccedd2923aee4c2acc6a23780b14ed4b8a5fa4e14e252a23b846df9b6c1
Deleted: sha256:9f54eef412758095c8079ac465d494a2872e02e90bf1fb5f12a1641c0d1bb78b
[root@host xwk]# 
构建在 Ubuntu 系统上运行 Nginx 的镜像 使用 docker commit 命令基于容器构建镜像

以交互方式启动 ubuntu 容器

[root@host xwk]# docker history centos
Error response from daemon: No such image: centos:latest
[root@host xwk]#  docker run -it ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete 
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest

在该容器中执行以下命令安装 nginx

 更新软件包
apt update
安装 Nginx
apt install -y nginx
过程中需要选择亚洲和上海
检查nginx服务版本
nginx -v
nginx version: nginx/1.18.0 (Ubuntu)

基于该容器生成新的镜像。
先退出容器,再执行 docker commit 命令将该容器提交并在本地生成新的镜像。

exit
[root@host xwk]# docker commit d7aaabf0105b ubuntu-with-nginx
sha256:a4d027ba7f66f4c93cfa8a9aa4f4bbfe8391a1e9be6be218cbd253ee6d358d58
[root@host xwk]# 
d7aaabf0105b可以在命令行root@d7aaabf0105b看到,每个人应该都不一样

基于新的镜像启动新容器
以交互方式启动容器之后,在容器中执行 nginx 命令启动 Nginx 服务,然后使用 ps –aux
查看相关的进程,结果表明已成功运行 Nginx 服务

[root@host xwk]# docker run -it ubuntu-with-nginx /bin/bash
root@e839944e18c1:/#
root@e839944e18c1:/# nginx
root@e839944e18c1:/# ps -aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.2   4104  2168 pts/0    Ss   19:56   0:00 /bin/bash
root          9  0.0  0.1  55276  1444 ?        Ss   19:57   0:00 nginx: master process nginx
www-data     10  0.0  0.2  55600  2100 ?        S    19:57   0:00 nginx: worker process
root         11  0.0  0.1   5892  1412 pts/0    R+   19:57   0:00 ps -aux
root@e839944e18c1:/# 

[root@host bin]# docker run --rm -it -p 8000:80 ubuntu-with-nginx /bin/bash
root@afaa559381c6:/# nginx

exit
[root@host bin]# docker stop ubuntu-with-nginx
ubuntu-with-nginx
[root@host bin]#
使用 docker build 命令基于 Dockerfile 构建镜像

准备构建 Dockerfile 构建上下文。
建立一个目录用作 Dockerfile 的构建上下文并准备所需的文件

[root@host bin]# mkdir ubuntu-nginx && cd ubuntu-nginx
[root@host ubuntu-nginx]# 

编写 Dockerfile 文件。
可以使用 nano 进行编辑

nano Dockerfile

# 从基础镜像 ubuntu 开始构建
FROM ubuntu:latest
# 安装 nginx
RUN apt update && apt install -y nginx
# 修改 nginx 首页信息
RUN echo "Hello! This is nginx server " > /usr/share/nginx/html/index.html
# 对外暴露 80 端口
EXPOSE 80
# 启动 nginx
CMD ["nginx", "-g", "daemon off;"]
不要忘了后面的点号,代表当前路径
docker build -t ubuntu-with-nginx:1.0 .
等待运行完毕,出现下面语句代表安装成功
Successfully built 7175ab8a67d3
Successfully tagged ubuntu-with-nginx:1.0

基于该镜像启动容器进行测试

[root@host ubuntu-nginx]# docker run --rm -d -p 8000:80 --name test-nginx ubuntu-with-nginx:1.0
03a07c0ffa98e8ee63abf0a5aafd6d6ae3a57b30829a0ca2df5082568b336ca4

[root@host ubuntu-nginx]# curl 127.0.0.1:8000



Welcome to nginx!

    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to "http://nginx.org/">nginx.org.
Commercial support is available at "http://nginx.com/">nginx.com.

Thank you for using nginx.

[root@host ubuntu-nginx]#

不过这里发现首页修改并未成功。这在下面来解决。
实验完毕,停止该容器,该容器会被自动删除。

[root@host ubuntu-nginx]# docker stop test-nginx
test-nginx
[root@host ubuntu-nginx]# 
测试镜像构建的缓存

Ubuntu 操作系统中使用 apt 安装 Nigix 服务器,默认的首页位于/var/www/html 目录中。继
续修改 Dockerfile,将首页修改语句改为:

vim Dockerfile 

RUN echo "Hello! This is nginx server " > /usr/share/nginx/html/index.html && cp /usr/share/nginx/html/index.html /var/www/html
[root@host ubuntu-nginx]# docker build -t ubuntu-with-nginx:2.0 .
Sending build context to Docker daemon  2.048kB
Step 1/5 : FROM ubuntu:latest
 ---> ba6acccedd29
Step 2/5 : RUN apt update && apt install -y nginx
 ---> Using cache
 ---> 1387c9ed6b73
Step 3/5 : RUN echo "Hello! This is nginx server " > /usr/share/nginx/html/index.html && cp /usr/share/nginx/html/index.html /var/www/html
 ---> Running in e123fd673fd7
Removing intermediate container e123fd673fd7
 ---> badf623cb613
Step 4/5 : EXPOSE 80
 ---> Running in 0b7c77fdf090
Removing intermediate container 0b7c77fdf090
 ---> 05f6b9452b59
Step 5/5 : CMD ["nginx", "-g", "daemon off;"]
 ---> Running in eee74e4f7018
Removing intermediate container eee74e4f7018
 ---> 5b554fcd0896
Successfully built 5b554fcd0896
Successfully tagged ubuntu-with-nginx:2.0
[root@host ubuntu-nginx]# 
[root@host ubuntu-nginx]# docker run --rm -d -p 8000:80 --name test-nginx ubuntu-with-nginx:2.0
03c213dc4abee9a8404c8ad4147f1d90ff44cabcae56fd6db95deea7f23ce5f4
[root@host ubuntu-nginx]# 
[root@host ubuntu-nginx]# curl 127.0.0.1:8000
Hello! This is nginx server 
[root@host ubuntu-nginx]# 


Nginx 网站首页已经修改成功

实验完毕,停止该容器,该容器会被自动删除。

[root@host ubuntu-nginx]# docker stop test-nginx
test-nginx
[root@host ubuntu-nginx]# 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/467864.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号