zl程序教程

您现在的位置是:首页 >  工具

当前栏目

[Docker] Create a Docker image with Dockerfile

Docker with create Image Dockerfile
2023-09-14 08:59:11 时间
### 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`