本文开始,我们将要学习了解的是 Docker,这也是使用 DevOps 必备的工具。Docker 通过提供轻量级、可移植的容器化平台,彻底改变了我们构建、发布和运行应用程序的方式。下面,我们就来学习一下 Docker 中的一些基础知识及其它在 DevOps 中的重要性。
Docker 已经成为 DevOps 领域中游戏规则的改变者,彻底改变了应用程序的部署、管理和交付方式。想成为一名 DevOps 工程师,了解 Docker 及其重要性对于简化软件交付、提交可扩展性以及确保开发和部署生命周期各个阶段的环境一致至关重要。
Docker 为容器化和应用程序部署提供了强大的工具,从而彻底改变了 DevOps 的格局。DevOps 工程师可以利用 Docker 实现一致的环境、高效的资源利用、快速的部署、增强的安全性和简化的 CI/CD 管道。通过掌握 Docker,DevOps 工程师可以释放可扩展、可靠和敏捷软件交付的潜力,使企业在当今快节奏的数字环境中茁壮成长。
docker run
命令用于启动一个新容器并通过命令行与之交互。例如,可以通过试运行docker run hello-world
来运行简单的 Hello,World!
容器来验证 Docker 的安装。
root@huang-ubuntu:~# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:c79d06dfdfd3d3eb04cafd0dc2bacab0992ebc243e083cabe208bac4dd7759e0
Status: Downloaded newer image for hello-world:latest
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/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
docker inspect
命令用于查看有关容器或镜像的详细信息。此命令提供对 Docker 对象的配置、网络和运行时详细信息的深入了解。例如,运行docker inspect container_id
将显示对应容器 ID 为 container_id
的容器的详细信息。
root@huang-ubuntu:~# docker inspect 9c7a54a9a43c
[
{
"Id": "sha256:9c7a54a9a43cca047013b82af109fe963fde787f63f9e016fdc3384500c2823d",
"RepoTags": [
"hello-world:latest"
],
"RepoDigests": [
"hello-world@sha256:c79d06dfdfd3d3eb04cafd0dc2bacab0992ebc243e083cabe208bac4dd7759e0"
],
"Parent": "",
"Comment": "",
"Created": "2023-05-04T17:37:03.872958712Z",
"Container": "347ca68872ee924c4f9394b195dcadaf591d387a45d624225251efc6cb7a348e",
"ContainerConfig": {
docker port
命令用于列出容器的端口映射。此命令可以帮助我们识别容器公开的端口并将它们映射到相应的主机端口。例如,我们可以运行 docker port container_name
来查看容器名称为 container_name
的端口映射。
root@huang-ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
66c1681f2201 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp, :::80->80/tcp nginx-test
root@huang-ubuntu:~# docker port nginx-test
80/tcp -> 0.0.0.0:80
80/tcp -> [::]:80
docker stats
命令用于查看一个或多个容器的资源使用的统计信息。此命令提供有关正在运行的容器的 CPU、内存、网络和磁盘使用情况的实时信息。只需运行 docker stats
即可监控查看容器的资源利用率。
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
66c1681f2201 nginx-test 0.00% 2.32MiB / 1.918GiB 0.12% 1.09kB / 0B 0B / 24.6kB 2
docker top
命令用于查看容器内运行的进程。此命令允许我们查看内的活动进程,类似于 Linux 上的 top
命令。例如,执行docker top container_id
查看正在运行的进程。
root@huang-ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
66c1681f2201 nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx-test
root@huang-ubuntu:~# docker top 66c1681f2201
UID PID PPID C STIME TTY TIME CMD
root 2439 2418 0 15:45 ? 00:00:00 nginx: master process nginx -g daemon off;
systemd+ 2482 2439 0 15:45 ? 00:00:00 nginx: worker process
将镜像保存为 tar 压缩文件。使用该命令可以将 Docker 镜像导出到文件中,以便共享或存储。
使用 docker save -o image.tar image_name
将镜像保存为 tar 压缩文件。
root@huang-ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest a6bd71f48f68 9 days ago 187MB
ubuntu latest e4c58958181a 8 weeks ago 77.8MB
hello-world latest 9c7a54a9a43c 6 months ago 13.3kB
root@huang-ubuntu:~# docker save -o nginx-image.tar nginx
root@huang-ubuntu:~# ls
nginx-image.tar
docker load
命令用于从 tar 压缩文件加载镜像。该命令允许我们从 tar 压缩文件中导入 Docker 镜像。
运行 docker load -i image.tar
将镜像加载到 Docker 环境中。
root@huang-ubuntu:~# ls
nginx-image.tar
root@huang-ubuntu:~# docker load -i nginx-image.tar
Loaded image: nginx:latest
以上就是本文的内容,我们了解了 Docker 的重要性以及学习了一些基本命令。