mirror of
https://github.com/yeasy/docker_practice.git
synced 2025-08-02 14:01:49 +00:00
@@ -80,7 +80,7 @@ RUN make -C /usr/src/redis install
|
||||
```docker
|
||||
FROM debian:stretch
|
||||
|
||||
RUN buildDeps='gcc libc6-dev make wget' \
|
||||
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" \
|
||||
@@ -177,17 +177,23 @@ Sending build context to Docker daemon 2.048 kB
|
||||
或许你已经注意到了,`docker build` 还支持从 URL 构建,比如可以直接从 Git repo 中构建:
|
||||
|
||||
```bash
|
||||
$ docker build https://github.com/twang2218/gitlab-ce-zh.git#:11.1
|
||||
# $env:DOCKER_BUILDKIT=0
|
||||
# export DOCKER_BUILDKIT=0
|
||||
|
||||
Sending build context to Docker daemon 2.048 kB
|
||||
Step 1 : FROM gitlab/gitlab-ce:11.1.0-ce.0
|
||||
11.1.0-ce.0: Pulling from gitlab/gitlab-ce
|
||||
aed15891ba52: Already exists
|
||||
773ae8583d14: Already exists
|
||||
...
|
||||
$ 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` 分支,构建目录为 `/11.1/`,然后 Docker 就会自己去 `git clone` 这个项目、切换到指定分支、并进入到指定目录后开始构建。
|
||||
这行命令指定了构建所需的 Git repo,并且指定分支为 `master`,构建目录为 `/amd64/hello-world/`,然后 Docker 就会自己去 `git clone` 这个项目、切换到指定分支、并进入到指定目录后开始构建。
|
||||
|
||||
### 用给定的 tar 压缩包构建
|
||||
|
||||
|
@@ -16,7 +16,7 @@ $ docker run --name webserver -d -p 80:80 nginx
|
||||
|
||||
这条命令会用 `nginx` 镜像启动一个容器,命名为 `webserver`,并且映射了 80 端口,这样我们可以用浏览器去访问这个 `nginx` 服务器。
|
||||
|
||||
如果是在 Linux 本机运行的 Docker,或者如果使用的是 Docker Desktop for Mac/Windows,那么可以直接访问:<http://localhost>;如果使用的是 Docker Toolbox,或者是在虚拟机、云服务器上安装的 Docker,则需要将 `localhost` 换为虚拟机地址或者实际云服务器地址。
|
||||
如果是在本机运行的 Docker,那么可以直接访问:`http://localhost` ,如果是在虚拟机、云服务器上安装的 Docker,则需要将 `localhost` 换为虚拟机地址或者实际云服务器地址。
|
||||
|
||||
直接用浏览器访问的话,我们会看到默认的 Nginx 欢迎页面。
|
||||
|
||||
@@ -116,7 +116,7 @@ e43d811ce2f4 4 weeks ago /bin/sh -c #(nop) CMD ["nginx" "-g" "da
|
||||
docker run --name web2 -d -p 81:80 nginx:v2
|
||||
```
|
||||
|
||||
这里我们命名为新的服务为 `web2`,并且映射到 `81` 端口。如果是 Docker Desktop for Mac/Windows 或 Linux 桌面的话,我们就可以直接访问 <http://localhost:81> 看到结果,其内容应该和之前修改后的 `webserver` 一样。
|
||||
这里我们命名为新的服务为 `web2`,并且映射到 `81` 端口。访问 `http://localhost:81` 看到结果,其内容应该和之前修改后的 `webserver` 一样。
|
||||
|
||||
至此,我们第一次完成了定制镜像,使用的是 `docker commit` 命令,手动操作给旧的镜像添加了新的一层,形成新的镜像,对镜像多层存储应该有了更直观的感觉。
|
||||
|
||||
@@ -124,7 +124,7 @@ docker run --name web2 -d -p 81:80 nginx:v2
|
||||
|
||||
使用 `docker commit` 命令虽然可以比较直观的帮助理解镜像分层存储的概念,但是实际环境中并不会这样使用。
|
||||
|
||||
首先,如果仔细观察之前的 `docker diff webserver` 的结果,你会发现除了真正想要修改的 `/usr/share/nginx/html/index.html` 文件外,由于命令的执行,还有很多文件被改动或添加了。这还仅仅是最简单的操作,如果是安装软件包、编译构建,那会有大量的无关内容被添加进来,如果不小心清理,将会导致镜像极为臃肿。
|
||||
首先,如果仔细观察之前的 `docker diff webserver` 的结果,你会发现除了真正想要修改的 `/usr/share/nginx/html/index.html` 文件外,由于命令的执行,还有很多文件被改动或添加了。这还仅仅是最简单的操作,如果是安装软件包、编译构建,那会有大量的无关内容被添加进来,将会导致镜像极为臃肿。
|
||||
|
||||
此外,使用 `docker commit` 意味着所有对镜像的操作都是黑箱操作,生成的镜像也被称为 **黑箱镜像**,换句话说,就是除了制作镜像的人知道执行过什么命令、怎么生成的镜像,别人根本无从得知。而且,即使是这个制作镜像的人,过一段时间后也无法记清具体的操作。这种黑箱镜像的维护工作是非常痛苦的。
|
||||
|
||||
|
@@ -10,7 +10,7 @@ docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签]
|
||||
|
||||
具体的选项可以通过 `docker pull --help` 命令看到,这里我们说一下镜像名称的格式。
|
||||
|
||||
* Docker 镜像仓库地址:地址的格式一般是 `<域名/IP>[:端口号]`。默认地址是 Docker Hub。
|
||||
* Docker 镜像仓库地址:地址的格式一般是 `<域名/IP>[:端口号]`。默认地址是 Docker Hub(docker.io)。
|
||||
* 仓库名:如之前所说,这里的仓库名是两段式名称,即 `<用户名>/<软件名>`。对于 Docker Hub,如果不给出用户名,则默认为 `library`,也就是官方镜像。
|
||||
|
||||
比如:
|
||||
@@ -40,9 +40,7 @@ Status: Downloaded newer image for ubuntu:18.04
|
||||
有了镜像后,我们就能够以这个镜像为基础启动并运行一个容器。以上面的 `ubuntu:18.04` 为例,如果我们打算启动里面的 `bash` 并且进行交互式操作的话,可以执行下面的命令。
|
||||
|
||||
```bash
|
||||
$ docker run -it --rm \
|
||||
ubuntu:18.04 \
|
||||
bash
|
||||
$ docker run -it --rm ubuntu:18.04 bash
|
||||
|
||||
root@e7009c6ce357:/# cat /etc/os-release
|
||||
NAME="Ubuntu"
|
||||
|
Reference in New Issue
Block a user