[Docker] Create a Docker image with Dockerfile

### 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`
posted @   Zhentiw  阅读(105)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2021-01-31 [Docker] Allow Containers to Communicate through Docker Networks
2019-01-31 [Functional Programming Monad] Refactor Stateful Code To Use A State Monad
2019-01-31 [Functional Programming Monad] Apply Stateful Computations To Functions (.ap, .liftA2)
2019-01-31 [Tools] Batch Create Markdown Files from a Template with Node.js and Mustache
2019-01-31 [Angular] Extract Implementation Details of ngrx from an Angular Application with the Facade Pattern
2018-01-31 [React] Refactor a Stateful List Component to a Functional Component with React PowerPlug
2018-01-31 [MST] Use Volatile State and Lifecycle Methods to Manage Private State
点击右上角即可分享
微信分享提示