首页 > 其他分享 >[Docker] Create a Docker image with Dockerfile

[Docker] Create a Docker image with Dockerfile

时间:2023-01-31 20:12:52浏览次数:37  
标签:widgetfactory root Create apt image docker sha256 Dockerfile Docker

### Create a docker file

1. Cd to the project: `cd widget-factory-inc/`

2. create a dockerfile: `vim dockerfile`

```bash
FROM httpd:2.4
RUN apt update -y && apt upgrade -y && apt autoremove -y && apt clean && rm -rf /var/lib/apt/lists/*
```

3. After have our Dockerfile, let's build the image

`docker build -t widgetfactory:0.1 .`

4. Setup some variables to help us inspect docker image
For each layer, we print it's value

```bash
export showLayers='{{ range .RootFS.Layers }}{{ println . }}{{end}}'
export showSize='{{ .Size }}'
```

5. `docker images`

6. Inspect the image

```bash
$ docker inspect -f "$showSize" widgetfactory:0.1
146940154
$ docker inspect -f "$showLayers" widgetfactory:0.1
sha256:67a4178b7d47beb6a1f697a593bd0c6841c67eb0da00f2badefb05fd30671490
sha256:9d113bfab823c45c316ee21491b7a96789e43d1128c74ac8301c2797caecda34
sha256:67c72336bd783517b29792ddc7b56c973a3f762a1d48f0ed89b645c36d79623c
sha256:8067b7092a5b840345a9e4874d5ba8d2bc272e28cedc3279c1de4f0cccbd29b8
sha256:a8b657e74a9ec0e3db41099f5410a1266663e66ed510cd79057a68b5755d385e
sha256:c7855b5e5105974bfd5fd60604cfd2b479c82fdd9e403060bcc320bf33288f42
$ docker inspect -f "$showLayers" httpd:2.4
sha256:67a4178b7d47beb6a1f697a593bd0c6841c67eb0da00f2badefb05fd30671490
sha256:9d113bfab823c45c316ee21491b7a96789e43d1128c74ac8301c2797caecda34
sha256:67c72336bd783517b29792ddc7b56c973a3f762a1d48f0ed89b645c36d79623c
sha256:8067b7092a5b840345a9e4874d5ba8d2bc272e28cedc3279c1de4f0cccbd29b8
sha256:a8b657e74a9ec0e3db41099f5410a1266663e66ed510cd79057a68b5755d385e
```

As we can see, the `widgetfactory` image has 6 layers, but the base images has 5 layers. Which means that `widgetfactory` add 1 layer on top of base images. 

The `RUN` command add one layer to the image.


### Add project website into container

1. Update Dockerfile

```bash
FROM httpd:2.4
RUN apt update -y && apt upgrade -y && apt autoremove -y && apt clean && rm -rf /var/lib/apt/lists/*

# remove default index.html
RUN rm -rf /usr/local/apache2/htdocs/index.html

WORKDIR /usr/local/apache2/htdocs

# Copy the file from local to workdir
# ./web: relative to current dir for local
# .: the workdir we have setup in previous cmd
COPY ./web .
```

2. Build docker image

`docker build -t widgetfactory:0.2 .`

3. Check the size and layers

```bash
$ docker inspect -f "$showSize" widgetfactory:0.2

$ docker inspect -f "$showLayers" widgetfactory:0.2

```

4. Bash into the container

```bash
docker run --rm -it widgetfactory:0.2 bash
root@2bb9c12b706d:/usr/local/apache2/htdocs# ls -l
total 16
drwxr-xr-x. 2 root root   76 Jan 31 08:47 img
-rw-r--r--. 1 root root 3059 Jan 31 08:47 index.html
-rw-r--r--. 1 root root 2910 Jan 31 08:47 quote.html
-rw-r--r--. 1 root root 2611 Jan 31 08:47 support.html
-rw-r--r--. 1 root root 2645 Jan 31 08:47 widgets.html
```

### Run the container

Run the container: 

`docker run --name web1 -p 80:80 widgetfactory:0.2`

Exit the container:

`CTRL + C`

Start the container:

`docker start web1`

标签:widgetfactory,root,Create,apt,image,docker,sha256,Dockerfile,Docker
From: https://www.cnblogs.com/Answer1215/p/17080335.html

相关文章

  • docker仓库登录出错
    Errorsavingcredentials:errorstoringcredentials-err:exitstatus1,CannotautolaunchD-BuswithoutX11$DISPLAY`出错提示:errorstoringcredentials-er......
  • It's possible to create a function auto generator this special test case binary
    It'spossibletocreateafunctionautogeneratorthisspecialtestcasebinarytreefromarrayinjavascript?Iwanttoautogeneratethosetestcasesinmy......
  • CoCreateGuid 找不到标识符 解决方案
    场景   将Boost库的头文件前置于Windows.h头文件前,调用CoCreateGuid提示出错如上解决方案1通过VX插件跳转到函数头文件,添加该头文件即可:#include<combaseapi.h>2通......
  • Docker-consul的容器服务更新与发现
    一、Consul概述1.1什么是服务注册与发现服务注册与发现是微服务架构中不可或缺的重要组件。起初服务都是单节点的,不保障高可用性,也不考虑服务的压力承载,服务之间调用单纯......
  • Docker的基本概念
    Docker的基本概念......
  • Docker 安装 MySQL5.7
    1.拉取数据库镜像dockerpullmysql:5.7 dockerimages命令查看镜像是否下载成功dockerimages2.配置mysql创建mysql目录,用于存放mysql相关配置及数据mkdir-p......
  • 关于Docker-Docker引擎
    详解:http://c.biancheng.net/view/3137.html下载与使用:https://blog.csdn.net/qq_44074697/article/details/118569644?ops_request_misc=%257B%2522request%255Fid%2522%......
  • Jenkins pipeline 使用agent docker编译构建
    Jenkins使用agentdocker构建pipeline此处用于记录,使用jenkinspipeline构建时,使用docker启动一个agent来构建编译环境。//需要在jenkins的Credentials设置中配置......
  • ubuntu 非root用户启动docker
    启动运行minikubeminikubestart--image-mirror-country='cn'......
  • docker-mysql cmd
    version:'3'services:db:#构建mysql镜像image:mysqlnetworks:network1:ipv4_address:172.16.238.10ip......