上一篇安装docker并校验了是否安装成功,接下来我们构建镜像运行容器,进一步感受docker的魅力吧!
下面展示两种镜像构建,一种是从仓库拉取,另一种是本地构建镜像。
我们可以从 Docker Hub 网站来搜索镜像。
我们也可以使用 docker search 命令来搜索镜像。比如我们需要一个 hello-world的镜像。
docker search hello-world
效果展示:
[root@iZhp33j6fklnmhbf0lz2obZ ~]# docker search hello-world
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
hello-world Hello World! (an example of minimal Dockeriz… 1810 [OK]
kitematic/hello-world-nginx A light-weight nginx container that demonstr… 152
tutum/hello-world Image to test docker deployments. Has Apache… 89 [OK]
dockercloud/hello-world Hello World! 19 [OK]
crccheck/hello-world Hello World web server in under 2.5 MB 15 [OK]
vad1mo/hello-world-rest A simple REST Service that echoes back all t… 5 [OK]
rancher/hello-world 2
ppc64le/hello-world Hello World! (an example of minimal Dockeriz… 2
ansibleplaybookbundle/hello-world-db-apb An APB which deploys a sample Hello World! a… 2 [OK]
thomaspoignant/hello-world-rest-json This project is a REST hello-world API to bu… 1
ansibleplaybookbundle/hello-world-apb An APB which deploys a sample Hello World! a… 1 [OK]
infrastructureascode/hello-world A tiny "Hello World" web server with a healt… 1 [OK]
armswdev/c-hello-world Simple hello-world C program on Alpine Linux… 0
businessgeeks00/hello-world-nodejs 0
strimzi/hello-world-producer 0
freddiedevops/hello-world-spring-boot 0
koudaiii/hello-world 0
tacc/hello-world 0
garystafford/hello-world Simple hello-world Spring Boot service for t… 0 [OK]
strimzi/hello-world-streams 0
tsepotesting123/hello-world 0
kevindockercompany/hello-world 0
dandando/hello-world-dotnet 0
okteto/hello-world 0
strimzi/hello-world-consumer 0
[root@iZhp33j6fklnmhbf0lz2obZ ~]#
NAME: 镜像仓库源的名称
DESCRIPTION: 镜像的描述
OFFICIAL: 是否 docker 官方发布
stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。
AUTOMATED: 自动构建。
#拉取镜像命令
docker pull [OPTIONS] NAME[:TAG]
OPTIONS 是指拉取的时候参数
NAME 是必须的,是指镜像的名称
[:TAG] 是指镜像的版本,如果不写,则默认是最新版本
Hello-world 镜像是从docker提供的默认仓库中获取。
效果展示:
[root@iZhp33j6fklnmhbf0lz2obZ ~]# docker pull hello-world
Using default tag: latest
Error response from daemon: Get "https://registry-1.docker.io/v2/library/hello-world/manifests/sha256:53f1bbee2f52c39e41682ee1d388285290c5c8a76cc92b42687eecf38e0af3f0": net/http: TLS handshake timeout
如果出现以上超时错误,加上镜像加速器,执行以下命令:
Docker 中国官方镜像加速:https://registry.docker-cn.com
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
EOF
效果如下:
[root@iZhp33j6fklnmhbf0lz2obZ ~]# mkdir -p /etc/docker
[root@iZhp33j6fklnmhbf0lz2obZ ~]# tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors": ["https://registry.docker-cn.com"]
> }
> EOF
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
[root@iZhp33j6fklnmhbf0lz2obZ ~]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:53f1bbee2f52c39e41682ee1d388285290c5c8a76cc92b42687eecf38e0af3f0
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
docker images [OPTIONS] [REPOSITORY[:TAG]]
REPOSITORY[:TAG] 指定镜像的名称 和版本
效果展示:
[root@iZhp33j6fklnmhbf0lz2obZ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test latest 90cb9f03bf34 18 hours ago 465MB
ubuntu 22.04 df5de72bdb3b 3 days ago 77.8MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
docker run [OPTIONS] IMAGE[:TAG] [COMMAND][ARG...]
IMAGE[:TAG] 指的是镜像的名字和版本
COMMAND 指的是镜像运行起来要执行什么样的命令
[ARG…] 指的是前面的命令所依赖的参数
效果展示:
[root@iZhp33j6fklnmhbf0lz2obZ ~]# docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
...

【docker pull】命令:首先Client 发起一个 【docker pull】命令,然后Docker_host(本机)的Docker daemon会去查看是否在本地的Images 已经存在,如果不存在,则从 【Registry】远程库中拉取到本地。
【docker run】命令:首先Client 发起一个 【docker run】命令,然后Docker_host(本机)的Docker daemon会去查看是否在本地的Images已经存在,如果存在则运行变成【Containers】docker容器,如果不存在,则先去【Registry】远程库拉取到本地,然后在变成【Containers】docker容器。
核心步骤如下,下面将详细讲解。
1.创建一个文件 hello.py。
2.创建一个名为 Dockerfile 的不带扩展名的文件。
3.Docker 实例构建它docker build -t test:latest .
4.运行您新建的图像docker run -p 8000:8000 test:latest
建立一个简单的 Web 服务器,其中包含一个显示“Hello World”的页面,添加文件hello.py。
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
同级目录创建Dockerfile,Docker Build 可以使用它来为我们的应用程序创建映像:
# syntax=docker/dockerfile:1
FROM ubuntu:22.04
# install app dependencies
RUN apt-get update && apt-get install -y python3 python3-pip
RUN pip install flask==2.1.*
# install app
COPY hello.py /
# final configuration
ENV FLASK_APP=hello
EXPOSE 8000
CMD flask run --host 0.0.0.0 --port 8000
syntax=docker/dockerfile:1
固定 dockerfile 语法的确切版本。在所有 Dockerfile中的第一行,因为它通知 Buildkit 要使用的 Dockerfile 的正确版本。
FROM ubuntu:22.04
这里的FROM指令将我们的基础镜像设置为 Ubuntu 的 22.04 版本。所有代码都在此基础映像上执行。
install app dependencies
dockerfile 中的注释以#符号开头。
RUN apt-get update && apt-get install -y python3 python3-pip
该RUN指令在构建上下文中执行一个 shell 命令。构建的上下文是位于指定 PATH 或 URL中的文件集。在这个例子中,我们的上下文是一个完整的 Ubuntu 操作系统,所以我们可以访问它的包管理器 apt。提供的命令会更新我们的包列表,然后在成功之后安装 python3 和 pip,这是 Python 的包管理器。
RUN pip install flask
第二RUN条指令要求我们之前在层中安装了 pip。应用前面的指令后,我们可以使用 pip 命令安装flask web框架。这是我们用来从上面编写基本的“Hello World”应用程序的框架,所以要在 Docker 中运行它,我们需要确保它已安装。
COPY hello.py /
这个 COPY指令将我们的hello.py文件从构建的上下文本地目录复制到我们图像的根目录中。执行此操作后,我们将在图像内部得到一个名为的文件/hello.py,其中包含我们本地副本的所有内容!
ENV FLASK_APP=hello
这个 ENV 指令设置了我们稍后需要的 Linux环境变量。
EXPOSE 8000
这条 EXPOSE 指令标志着我们的最终镜像在端口 8000 上有一个服务侦听。可选项
CMD flask run --host 0.0.0.0 --port 8000
此 CMD 指令设置用户基于此映像启动容器时运行的命令。在这种情况下,我们将启动flask开发服务器,侦听端口 8000 上的所有主机。
docker build -t test:latest .
[root@iZhp33j6fklnmhbf0lz2obZ ~]# docker build -t test:latest .
Sending build context to Docker daemon 28.76MB
Step 1/7 : FROM ubuntu:22.04
---> df5de72bdb3b
Step 2/7 : RUN apt-get update && apt-get install -y python3 python3-pip
---> Using cache
---> eee89b706a8b
Step 3/7 : RUN pip install flask==2.1.*
---> Using cache
---> d7b00194a3bd
Step 4/7 : COPY hello.py /
---> Using cache
---> eaf08a89e93f
Step 5/7 : ENV FLASK_APP=hello
---> Using cache
---> 981fa111d188
Step 6/7 : EXPOSE 8000
---> Using cache
---> a6ef7a1dc59d
Step 7/7 : CMD flask run --host 0.0.0.0 --port 8000
---> Using cache
---> 90cb9f03bf34
Successfully built 90cb9f03bf34
# 启动容器
docker run -p 8000:8000 test:latest
#启动并进入容器
docker run -it test:latest /bin/bash
效果展示:
[root@iZhp33j6fklnmhbf0lz2obZ ~]# docker run -p 8000:8000 test:latest
* Serving Flask app 'hello' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on all addresses (0.0.0.0)
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://127.0.0.1:8000
* Running on http://172.17.0.3:8000 (Press CTRL+C to quit)
docker ps
效果展示:
[root@iZhp33j6fklnmhbf0lz2obZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3d6dd07e9e8e test:latest "/bin/sh -c 'flask r…" 54 seconds ago Up 53 seconds 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp recursing_keldysh
docker exec -it recursing_keldysh /bin/bash
效果展示:
[root@iZhp33j6fklnmhbf0lz2obZ ~]# docker exec -it recursing_keldysh /bin/bash
root@3d6dd07e9e8e:/#
curl localhost:8000
[root@iZhp33j6fklnmhbf0lz2obZ ~]# curl localhost:8000
Hello World![root@iZhp33j6fklnmhbf0lz2obZ ~]#
镜像删除使用 docker rmi 命令,比如我们删除 hello-world 镜像:
#删除镜像
docker rmi hello-world
#强制删除图像
docker rmi -f [镜像id] [镜像id]
#删除所有镜像
docker rmi `docker images -qa`
效果展示:
[root@iZhp33j6fklnmhbf0lz2obZ ~]# docker rmi hello-world:latest
Untagged: hello-world:latest
Untagged: hello-world@sha256:53f1bbee2f52c39e41682ee1d388285290c5c8a76cc92b42687eecf38e0af3f0
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359

青山常伴绿水 燕雀已是南飞