docker_practice/image/build.md

227 lines
15 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 使 Dockerfile
`docker commit` Dockerfile
Dockerfile **(Instruction)**
`nginx` 使 Dockerfile
`Dockerfile`
```bash
$ mkdir mynginx
$ cd mynginx
$ touch Dockerfile
```
```docker
FROM nginx
RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html
```
Dockerfile `FROM` `RUN`
## FROM
`nginx` `FROM` **** `Dockerfile` `FROM`
[Docker Hub](https://hub.docker.com/search?q=&type=image&image_filter=official) 上有非常多的高质量的官方镜像,有可以直接拿来使用的服务类的镜像,如 [`nginx`](https://hub.docker.com/_/nginx/)、[`redis`](https://hub.docker.com/_/redis/)、[`mongo`](https://hub.docker.com/_/mongo/)、[`mysql`](https://hub.docker.com/_/mysql/)、[`httpd`](https://hub.docker.com/_/httpd/)、[`php`](https://hub.docker.com/_/php/)、[`tomcat`](https://hub.docker.com/_/tomcat/) 等;也有一些方便开发、构建、运行各种语言应用的镜像,如 [`node`](https://hub.docker.com/_/node)、[`openjdk`](https://hub.docker.com/_/openjdk/)、[`python`](https://hub.docker.com/_/python/)、[`ruby`](https://hub.docker.com/_/ruby/)、[`golang`](https://hub.docker.com/_/golang/) 等。可以在其中寻找一个最符合我们最终目标的镜像为基础镜像进行定制。
[`ubuntu`](https://hub.docker.com/_/ubuntu/)、[`debian`](https://hub.docker.com/_/debian/)、[`centos`](https://hub.docker.com/_/centos/)、[`fedora`](https://hub.docker.com/_/fedora/)、[`alpine`](https://hub.docker.com/_/alpine/) 等,这些操作系统的软件库为我们提供了更广阔的扩展空间。
Docker `scratch`
```docker
FROM scratch
...
```
`scratch`
Linux `FROM scratch` 使 [Go ](https://golang.google.cn/) 开发的应用很多会使用这种方式来制作镜像,这也是为什么有人认为 Go 是特别适合容器微服务架构的语言的原因之一。
## RUN
`RUN` `RUN`
* *shell* `RUN <命令>` Dockerfile `RUN`
```docker
RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html
```
* *exec* `RUN ["可执行文件", "参数1", "参数2"]`
`RUN` Shell Shell RUN
```docker
FROM debian:stretch
RUN apt-get update
RUN apt-get install -y gcc libc6-dev make wget
RUN wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz"
RUN mkdir -p /usr/src/redis
RUN tar -xzf redis.tar.gz -C /usr/src/redis --strip-components=1
RUN make -C /usr/src/redis
RUN make -C /usr/src/redis install
```
Dockerfile `RUN` `RUN` `commit`
7 西
Docker
*Union FS AUFS 42 127 *
`Dockerfile`
```docker
FROM debian:stretch
RUN set -x; buildDeps='gcc libc6-dev make wget' \
&& apt-get update \
&& apt-get install -y $buildDeps \
&& wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz" \
&& mkdir -p /usr/src/redis \
&& tar -xzf redis.tar.gz -C /usr/src/redis --strip-components=1 \
&& make -C /usr/src/redis \
&& make -C /usr/src/redis install \
&& rm -rf /var/lib/apt/lists/* \
&& rm redis.tar.gz \
&& rm -r /usr/src/redis \
&& apt-get purge -y --auto-remove $buildDeps
```
redis 使 `RUN` 使 `RUN` 使 `&&` 7 1 Dockerfile Shell
Dockerfile Shell `\` `#`
`apt` 西西西
Docker
##
nginx Dockerfile Dockerfile
`Dockerfile`
```bash
$ docker build -t nginx:v3 .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM nginx
---> e43d811ce2f4
Step 2 : RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html
---> Running in 9cdc27646c7b
---> 44aa4490ce2c
Removing intermediate container 9cdc27646c7b
Successfully built 44aa4490ce2c
```
`Step 2` `RUN` `9cdc27646c7b` `44aa4490ce2c` `9cdc27646c7b`
使 `docker build`
```bash
docker build [选项] <上下文路径/URL/->
```
`-t nginx:v3` `nginx:v2` `nginx:v2`
## Context
`docker build` `.``.` `Dockerfile` `Dockerfile` ****
`docker build` Docker Docker Docker REST API [Docker Remote API](https://docs.docker.com/develop/sdk/),而如 `docker` 命令这样的客户端工具,则是通过这组 API 与 Docker 引擎交互,从而完成各种功能。因此,虽然表面上我们好像是在本机执行各种 `docker` 功能但实际上一切都是使用的远程调用形式在服务端Docker 引擎)完成。也因为这种 C/S 设计,让我们操作远程服务器的 Docker 引擎变得轻而易举。
`RUN` `COPY` `ADD` `docker build` Docker /
`docker build` Docker Docker
`Dockerfile`
```docker
COPY ./package.json /app/
```
`docker build` `package.json` `Dockerfile` `package.json` **context** `package.json`
`COPY` ** `COPY ../package.json /app` `COPY /opt/xxxx /app` Docker
`docker build -t nginx:v3 .` `.``docker build` Docker
`docker build`
```bash
$ docker build -t nginx:v3 .
Sending build context to Docker daemon 2.048 kB
...
```
`COPY /opt/xxxx /app` `Dockerfile` `docker build` GB 西 `docker build` 使
`Dockerfile` 西 Docker `.gitignore` `.dockerignore` Docker
`.` `Dockerfile` `Dockerfile` `Dockerfile` Dockerfile
`Dockerfile` `Dockerfile` `-f ../Dockerfile.php` `Dockerfile`
使 `Dockerfile`
## `docker build`
### Git repo
`docker build` URL Git repo
```bash
# $env:DOCKER_BUILDKIT=0
# export DOCKER_BUILDKIT=0
$ docker build -t hello-world https://github.com/docker-library/hello-world.git#master:amd64/hello-world
Step 1/3 : FROM scratch
--->
Step 2/3 : COPY hello /
---> ac779757d46e
Step 3/3 : CMD ["/hello"]
---> Running in d2a513a760ed
Removing intermediate container d2a513a760ed
---> 038ad4142d2b
Successfully built 038ad4142d2b
```
Git repo `master` `/amd64/hello-world/` Docker `git clone`
### tar
```bash
$ docker build http://server/context.tar.gz
```
URL Git repo `tar` Docker
### Dockerfile
```bash
docker build - < Dockerfile
```
```bash
cat Dockerfile | docker build -
```
`Dockerfile` Dockerfile `COPY`
###
```bash
$ docker build - < context.tar.gz
```
`gzip``bzip2` `xz` 使