首页 > 其他分享 >[Docker] Handcrafting a Container Image

[Docker] Handcrafting a Container Image

时间:2023-01-25 15:23:17浏览次数:33  
标签:ago httpd widgetfactory Container Image example docker root Docker

Pull httpd image and run the container

[cloud_user@ip-10-0-1-78 ~]$ docker pull httpd:2.4
[cloud_user@ip-10-0-1-78 ~]$ docker run --name webtemplate -d httpd:2.4
b33977ff66d330fa6354f7d23fb4d96d891fffcbe7c270a889b08a6437d222f0
[cloud_user@ip-10-0-1-78 ~]$ docker ps
CONTAINER ID   IMAGE       COMMAND              CREATED         STATUS         PORTS     NAMES
b33977ff66d3   httpd:2.4   "httpd-foreground"   4 seconds ago   Up 3 seconds   80/tcp    webtemplate

Exec into container and download the code

We want to ssh into container

docker exec -it webtemplate bash

We want to make sure system is up to date and install git

apt update && apt install git -y

After installing git, we can clone the repo to get the code. We want to put the code into /tmp/widget-factory-inc folder

git clone https://github.com/linuxacademy/content-widget-factory-inc.git /tmp/widget-factory-inc

Serve the webstie

Before we serve the website, we need to remove the default index.html file come alone with apache2.

root@b33977ff66d3:/usr/local/apache2# ls -l htdocs/
total 4
-rw-r--r--. 1 501 staff 45 Jun 11  2007 index.html
root@b33977ff66d3:/usr/local/apache2# rm htdocs/index.html

After cleaning the file, we can copy our code into htdocs/

root@b33977ff66d3:/usr/local/apache2# cp -r /tmp/widget-factory-inc/web/* htdocs/
root@b33977ff66d3:/usr/local/apache2# ls -l htdocs/
total 16
drwxr-xr-x. 2 root root   76 Jan 25 06:46 img
-rw-r--r--. 1 root root 3059 Jan 25 06:46 index.html
-rw-r--r--. 1 root root 2910 Jan 25 06:46 quote.html
-rw-r--r--. 1 root root 2611 Jan 25 06:46 support.html
-rw-r--r--. 1 root root 2645 Jan 25 06:46 widgets.html

Now we have our code into the container, next we can create the image, so exit container first.

exit

Create image

First we need to get contianer id

docker ps

CONTAINER ID   IMAGE       COMMAND              CREATED          STATUS          PORTS     NAMES
b33977ff66d3   httpd:2.4   "httpd-foreground"   12 minutes ago   Up 12 minutes   80/tcp    webtemplate

Create a image named example/widgetfactory

docker commit b33977ff66d3 example/widgetfactory:v1

Now let's see our images

[cloud_user@ip-10-0-1-78 ~]$ docker images
REPOSITORY              TAG       IMAGE ID       CREATED          SIZE
example/widgetfactory   v1        c301f990ab9c   26 seconds ago   243MB
httpd                   2.4       6e794a483258   7 days ago       145MB

We can notice the image example/widgetfactory size is much larger than the base image, that's because we forgot the clear out extra build tools.

Cleanup

Let's back into the container to clear the extra resource

docker exec -it webtemplate bash

First, let's delete the code from /tmp/widget-factory-inc/

rm -rf /tmp/widget-factory-inc/

Second, we also need to remove git, its related tools and cleanup the apt packages

apt remove git -y && apt autoremove -y && apt clean

Now image has been cleanup

exit

Build image again

[cloud_user@ip-10-0-1-78 ~]$ docker ps
CONTAINER ID   IMAGE       COMMAND              CREATED          STATUS          PORTS     NAMES
b33977ff66d3   httpd:2.4   "httpd-foreground"   19 minutes ago   Up 19 minutes   80/tcp    webtemplate
[cloud_user@ip-10-0-1-78 ~]$ docker commit b33977ff66d3 example/widgetfactory:v2
sha256:85eec7c693f9bab0563ff3e8e5096a7ac8099c8f3a274e39d8feb2a1360872d0
[cloud_user@ip-10-0-1-78 ~]$ docker images
REPOSITORY              TAG       IMAGE ID       CREATED         SIZE
example/widgetfactory   v2        85eec7c693f9   4 seconds ago   164MB
example/widgetfactory   v1        c301f990ab9c   6 minutes ago   243MB
httpd                   2.4       6e794a483258   7 days ago      145MB

Now, v2 size is much smaller.

Let's also remove v1 image, since we don't need it anymore.

docker rmi example/widgetfactory:v1

[cloud_user@ip-10-0-1-78 ~]$ docker images
REPOSITORY              TAG       IMAGE ID       CREATED         SIZE
example/widgetfactory   v2        85eec7c693f9   2 minutes ago   164MB
httpd                   2.4       6e794a483258   7 days ago      145MB
[cloud_user@ip-10-0-1-78 ~]$

Let's run our container

docker run --name web1 -p 8081:80 -d example/widgetfactory:v2
docker run --name web2 -p 8082:80 -d example/widgetfactory:v2
docker run --name web3 -p 8083:80 -d example/widgetfactory:v2

Now we have 3 copies of container running:

[cloud_user@ip-10-0-1-78 ~]$ docker ps
CONTAINER ID   IMAGE                      COMMAND              CREATED          STATUS          PORTS                                   NAMES
589e6c2ce3c3   example/widgetfactory:v2   "httpd-foreground"   9 seconds ago    Up 8 seconds    0.0.0.0:8083->80/tcp, :::8083->80/tcp   web3
1bf1ba11d5ae   example/widgetfactory:v2   "httpd-foreground"   15 seconds ago   Up 14 seconds   0.0.0.0:8082->80/tcp, :::8082->80/tcp   web2
dbd54fe50ef4   example/widgetfactory:v2   "httpd-foreground"   54 seconds ago   Up 53 seconds   0.0.0.0:8081->80/tcp, :::8081->80/tcp   web1
b33977ff66d3   httpd:2.4  

Stop httpd container, which we don't need it

docker stop webtemplate

Now if goes to the website https:<public-ip>:8081, https:<public-ip>:8082, https:<public-ip>:8083, we can see the same site host on different port of the same container image.

If we stop one of the continer: docker stop web2, we will see the site is offline.

Congras!

标签:ago,httpd,widgetfactory,Container,Image,example,docker,root,Docker
From: https://www.cnblogs.com/Answer1215/p/17066979.html

相关文章

  • Docker 解决 `denied: requested access to the resource is denied`
    背景由于不可描述的原因,相对于以前,最近在更加频繁的迁移服务器,简单的Shell脚本已经不能满足需求了,于是将所有的项目Docker化。部分不含敏感配置的项目准备放到Docker......
  • Docker 解决 `denied: requested access to the resource is denied`
    背景由于不可描述的原因,相对于以前,最近在更加频繁的迁移服务器,简单的Shell脚本已经不能满足需求了,于是将所有的项目Docker化。部分不含敏感配置的项目准备放到Docke......
  • Docker 基础 - 2
    容器操作系统类型Busybox集成了一百多个最常用Linux命令和工具的软件工具箱.包含catechogrepfindmounttelnet等Busybox是Linux系统的瑞士军刀Debian/Ubun......
  • docker内的中文乱码
    docker内终端命令行中文乱码在Dockerfile内添加环境变量方法1#在Dockerfile中修改ENVLANG=C.UTF-8方法2#在Dockerfile中修改echo"LANG=C.UTF-8">>/root/.bas......
  • [Docker] Working With Prebuilt Docker Images
    Pullhttpd:latestimageNormallyyouwanttoattachtheversionhttpd:2.4,becauseeverytimeyouruntheimage,wewiththesameversionisrunning.buthere,......
  • docker磁盘
    docker容器跑久了,其产生的日志可能会占用比较多的空间[root@server1~]#df-h文件系统容量已用可用已用%挂载点devtmpfs475M......
  • Docker 容器添加自定义root ca
    比如如果我们基于了step-ca工具做为我们的ca机制,就会有不可信的问题,业务使用就特别不方便了,以下是一个参考配置实际上很简单就是使用update-ca-certificates更新信息......
  • 【ARIXV2209】Multi-Scale Attention Network for Single Image Super-Resolution
    【ARIXV2209】Multi-ScaleAttentionNetworkforSingleImageSuper-Resolution代码:https://github.com/icandle/MAN这是来自南开大学的工作,将多尺度机制与大核注意机......
  • docker容器日志清理
    最近发现linux(Centos7)虚拟机的空间不够了,想创建新的容器都失败。剩下不到100M。之前还有好几个G。然后每天不定期查看磁盘空间,发现不断被蚕食。今天比昨天就少了100M;然后下......
  • Docker 基础 - 1
    镜像获取镜像dockerpull查看镜像信息dockerimagesdockerinspect<imagesid>#获取镜像的详细信息搜寻镜像dockersearch删除镜像dockerrmi当一个镜像拥......