Docker实战

简介: Docker实战

一、准备工具


云服务器


我用的是腾讯云的Centos7.5 64位不到一百块一年的云服务器,你也可以用虚拟机


1.png


二、登录服务器工具


MobaXterm


1、之所以用这个作为ssh客户端,主要原因就是基于MobaXterm强大且丰富的功能,并且还都是免费的


MobaXterm主要功能:


  • 支持各种连接 SSH,X11,RDP,VNC,FTP,MOSH
  • 支持 Unix 命令(bash,ls,cat,sed,grep,awk,rsync,…)
  • 连接 SSH 终端后支持 SFTP 传输文件
  • 各种丰富的插件(git/dig/aria2…)
  • 可运行 Windows 或软件


下载地址:https://mobaxterm.mobatek.net/download-home-edition.html


2.png


2、登录服务器


3.png


三、Centos Docker安装


1、使用官方安装脚本安装


curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun


2、使用国内 daocloud 一键安装命令


curl -sSL https://get.daocloud.io/docker | sh


3、检查是否安装成功(docker -v)


[root@VM-0-11-centos ~]# docker -v
 Docker version 20.10.0, build 7287ab3
 [root@VM-0-11-centos ~]#


4、启动docker


sudo systemctl start docker


5、通过hello world映像验证是否正确安装Docker


[root@VM-0-11-centos ~]# sudo docker run hello-world
 Unable to find image 'hello-world:latest' locally
 latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
 Digest: sha256:1a523af650137b8accdaed439c17d684df61ee4d74feac151b5b337bd29e7eec
 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/
 [root@VM-0-11-centos ~]#


四、Docker实战开始


1、Docker Hello World


Docker 允许你在容器内运行应用程序, 使用 docker run 命令来在容器内运行一个应用程序


[root@VM-0-11-centos ~]# docker run ubuntu:15.10 /bin/echo "Hello world"
 Unable to find image 'ubuntu:15.10' locally
15.10: Pulling from library/ubuntu
7dcf5a444392: Pull complete
759aa75f3cee: Pull complete
3fa871dc8a2b: Pull complete
224c42ae46e7: Pull complete
 Digest: sha256:02521a2d079595241c6793b2044f02eecf294034f31d6e235ac4b2b54ffc41f3
 Status: Downloaded newer image for ubuntu:15.10
 Hello world
 [root@VM-0-11-centos ~]#


各个参数解析:


  • docker: Docker 的二进制执行文件。
  • run: 与前面的 docker 组合来运行一个容器。
  • ubuntu:15.10 指定要运行的镜像,Docker 首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库 Docker Hub 下载公共镜像。
  • /bin/echo "Hello world": 在启动的容器里执行的命令


以上命令完整的意思可以解释为:Docker 以 ubuntu15.10 镜像创建一个新容器,然后在容器里执行 bin/echo "Hello world",然后输出结果。


2、交互式容器


[root@VM-0-11-centos ~]# docker run -i -t ubuntu:15.10 /bin/bash
 root@752fdf5d7764:/# cat /proc/version
 Linux version 3.10.0-1127.19.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) ) #1 SMP Tue Aug 25 17:23:54 UTC 2020
 root@752fdf5d7764:/# exit
exit
 [root@VM-0-11-centos ~]#


各个参数解析:


  • -t: 在新容器内指定一个伪终端或终端。
  • -i: 允许你对容器内的标准输入 (STDIN) 进行交互。


第二行我们已经进入了一个Ubuntu15.10的容器


第五行可以通过exit或CTRL + D来退出容器


3、后台运行容器


1、后台运行


[root@VM-0-11-centos ~]# docker ps
 CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
 [root@VM-0-11-centos ~]# docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
292152f399f3c2fa1fbac80edaffe2721f6948eba8c0821c652508805b82f2b3
 [root@VM-0-11-centos ~]# docker ps
 CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS     NAMES
292152f399f3   ubuntu:15.10   "/bin/sh -c 'while t…"   About a minute ago   Up About a minute             blissful_lumiere
 [root@VM-0-11-centos ~]#


输出详情介绍:


CONTAINER ID: 容器 ID。


IMAGE: 使用的镜像。


COMMAND: 启动容器时运行的命令。


CREATED: 容器的创建时间。


STATUS: 容器状态。


状态有7种:


  • created(已创建)
  • restarting(重启中)
  • running 或 Up(运行中)
  • removing(迁移中)
  • paused(暂停)
  • exited(停止)
  • dead(死亡)


PORTS: 容器的端口信息和使用的连接类型(tcpudp)。


NAMES: 自动分配的容器名称。


2、查看容器的标准输出以及停止容器


[root@VM-0-11-centos ~]# docker logs 292152f399f3
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
[root@VM-0-11-centos ~]# docker stop 292152f399f3
292152f399f3
[root@VM-0-11-centos ~]#


4、运行一个web应用


在docker容器中运行一个 Python Flask 应用来运行一个web应用。


[root@VM-0-11-centos ~]# docker pull training/webapp
Using default tag: latest
latest: Pulling from training/webapp
Image docker.io/training/webapp:latest uses outdated schema1 manifest format. Please upgrade to a schema2 image for better future compatibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/
e190868d63f8: Pull complete
909cd34c6fd7: Pull complete
0b9bfabab7c1: Pull complete
a3ed95caeb02: Pull complete
10bbbc0fc0ff: Downloading [===================>                               ]  7.883MB/20.71MB
10bbbc0fc0ff: Downloading [==========================>                        ]  11.08MB/20.71MB
fca59b508e9f: Downloading [====================>                              ]  20.85MB/50.25MB
10bbbc0fc0ff: Downloading [==============================================>    ]  19.38MB/20.71MB
fca59b508e9f: Downloading [==========================>                        ]  26.43MB/50.25MB
10bbbc0fc0ff: Pull complete
fca59b508e9f: Downloading [================================>                  ]  33.03MB/50.25MB
fca59b508e9f: Downloading [==================================>                ]  35.06MB/50.25MB
fca59b508e9f: Pull complete
e7ae2541b15b: Pull complete
9dd97ef58ce9: Pull complete
a4c1b0cb7af7: Pull complete
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Downloaded newer image for training/webapp:latest
docker.io/training/webapp:latest
[root@VM-0-11-centos ~]#
[root@VM-0-11-centos ~]# docker run -d -p 8080:5000 training/webapp python app.py
06fc6dc03d8ecf32f34fd7a687b6b3dbba1510e7f94e0c835aa2b210bffdf5eb
[root@VM-0-11-centos ~]#
[root@VM-0-11-centos ~]# docker ps
CONTAINER ID   IMAGE             COMMAND           CREATED         STATUS         PORTS                    NAMES
06fc6dc03d8e   training/webapp   "python app.py"   3 seconds ago   Up 3 seconds   0.0.0.0:8080->5000/tcp   busy_brahmagupta
[root@VM-0-11-centos ~]#


docker run -d -P training/webapp python app.py 参数说明:


  • -d:让容器在后台运行。
  • -P:将容器内部使用的网络端口随机映射到我们使用的主机上。


Docker 开放了 5000 端口(默认 Python Flask 端口)用-p指定映射到主机端口8080上。


这时候我们需要查看云服务器是否开放8080端口,没有看到端口


[root@VM-0-11-centos ~]# firewall-cmd --list-ports    //查看端口列表
You're performing an operation over default zone ('public'),
but your connections/interfaces are in zone 'docker' (see --get-active-zones)
You most likely need to use --zone=docker option.
[root@VM-0-11-centos ~]#


开放8080端口


[root@VM-0-11-centos ~]# firewall-cmd --add-port=8080/tcp --permanent    //开放8081端口
You're performing an operation over default zone ('public'),
but your connections/interfaces are in zone 'docker' (see --get-active-zones)
You most likely need to use --zone=docker option.
success
[root@VM-0-11-centos ~]# firewall-cmd --reload    //重新加载防火墙
success
[root@VM-0-11-centos ~]# firewall-cmd --list-ports//查看端口列表
You're performing an operation over default zone ('public'),
but your connections/interfaces are in zone 'docker' (see --get-active-zones)
You most likely need to use --zone=docker option.
8080/tcp    //开放的端口
[root@VM-0-11-centos ~]#


这时我们可以通过浏览器访问WEB应用


4.png


5、Docker镜像使用


1、列出镜像列表


[root@VM-0-11-centos ~]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED         SIZE
ubuntu            latest    f643c72bc252   3 weeks ago     72.9MB
httpd             latest    0a30f4c29d25   4 weeks ago     138MB
hello-world       latest    bf756fb1ae65   11 months ago   13.3kB
ubuntu            15.10     9b9cb95443b5   4 years ago     137MB
training/webapp   latest    6fae60ef3446   5 years ago     349MB
[root@VM-0-11-centos ~]#


各个选项说明:


  • REPOSITORY:表示镜像的仓库源
  • TAG:镜像的标签
  • IMAGE ID:镜像ID
  • CREATED:镜像创建时间
  • SIZE:镜像大小


2、获取镜像


docker pull + 镜像名字


3、查找镜像


docker search + 镜像名字


4、删除镜像


/***docker rmi + 镜像名字***/
[root@VM-0-11-centos ~]# docker rmi hello-world
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container 75d905f3f65d is using its referenced image bf756fb1ae65
[root@VM-0-11-centos ~]#


删除镜像错误需要先用docker rm + 运行过的镜像ID进行删除后才能删除镜像


[root@VM-0-11-centos ~]# docker rm 75d905f3f65d
9e3a0d6881fc
[root@VM-0-11-centos ~]# docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:1a523af650137b8accdaed439c17d684df61ee4d74feac151b5b337bd29e7eec
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
Deleted: sha256:9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63
[root@VM-0-11-centos ~]#


6、Docker容器连接


1、容器命名


当我们创建一个容器的时候,docker 会自动对它进行命名。另外,我们也可以使用 --name 标识来命名容器


使用 docker ps 命令来查看容器名称


[root@VM-0-11-centos ~]# docker run -d -P --name hackett training/webapp python app.py
9ac60cb23bd0fb5cb9be39de648f17bc9702883cef1a42682105316c6b32d104
[root@VM-0-11-centos ~]# docker ps
CONTAINER ID   IMAGE             COMMAND           CREATED          STATUS          PORTS                     NAMES
9ac60cb23bd0   training/webapp   "python app.py"   5 seconds ago    Up 4 seconds    0.0.0.0:49154->5000/tcp   hackett
ef0567701e23   training/webapp   "python app.py"   21 minutes ago   Up 21 minutes   0.0.0.0:8080->5000/tcp    vigilant_cerf
[root@VM-0-11-centos ~]#


2、连接容器


新建网络test-net网络


[root@VM-0-11-centos ~]# docker network create -d bridge test-ne
830af66ce29f8ab71f7d4e298a46d678a07ee8ba2abe50513d5d9000093e4990
[root@VM-0-11-centos ~]#


新建两个容器并连接到test-net网络


[root@VM-0-11-centos ~]# docker run -itd --name test1 --network test-net ubuntu /bin/bash
4cb1423808b71139ae43afad21dd0333c7ac6c6524d77e6abec7bf758c2ab2b0
[root@VM-0-11-centos ~]# docker run -itd --name test2 --network test-net ubuntu /bin/bash
b11dbe9adb83985dac98f7d60aa9e8d4c0c86babbb2d22d7d52ee748fa85a2ed
[root@VM-0-11-centos ~]# docker ps


进入test1并测试ping test2(需要先安装ping命令)


apt-get update
apt install iputils-ping
[root@VM-0-11-centos ~]# docker exec -it test1 /bin/bash
root@4cb1423808b7:/#
root@4cb1423808b7:/# ping test2
PING test2 (172.19.0.3) 56(84) bytes of data.
64 bytes from test2.test-net (172.19.0.3): icmp_seq=1 ttl=64 time=0.089 ms
64 bytes from test2.test-net (172.19.0.3): icmp_seq=2 ttl=64 time=0.071 ms


参考链接:https://www.runoob.com/docker/docker-tutorial.html


如果你觉得文章还不错,记得"点赞关注"


关注我的微信公众号【 加班猿 】可以获取更多内容

目录
相关文章
|
1月前
|
运维 监控 安全
构建高效自动化运维体系:Ansible与Docker的协同实战
【5月更文挑战第25天】 在当今快速迭代的软件发布环境中,自动化运维成为确保部署效率和可靠性的关键。本文通过深入分析Ansible和Docker技术,探索它们如何协同工作以构建一个高效的自动化运维体系。文章不仅介绍了Ansible的配置管理功能和Docker容器化的优势,还详细阐述了将两者结合的实践策略,旨在帮助读者理解并实现更智能、更灵活的基础设施管理。
|
3天前
|
jenkins 持续交付 开发者
利用Docker容器化部署应用的实战指南
【6月更文挑战第27天】本文详述Docker应用部署,涵盖Docker基本概念、安装、镜像制作及运行。通过编写Dockerfile构建镜像,使用`docker build`、`run`、`push`及`stop`命令管理。集成CI/CD工具如Jenkins,实现自动化构建、测试和部署,提升开发效率与部署质量。Docker助力轻量级、可移植的微服务架构。
|
4天前
|
NoSQL Redis Docker
Docker再学习 - 实战
Docker再学习 - 实战
6 0
|
1月前
|
前端开发 API 数据库
【Docker专栏】Docker Compose实战:编排多容器应用
【5月更文挑战第7天】Docker Compose是Docker的多容器管理工具,通过YAML文件简化多容器应用部署。它能一键启动、停止服务,保证开发、测试和生产环境的一致性。安装后,创建`docker-compose.yml`文件定义服务,如示例中的web和db服务。使用`docker-compose up -d`启动服务,通过`docker-compose ps`、`stop`、`down`和`logs`命令管理服务。
【Docker专栏】Docker Compose实战:编排多容器应用
|
1月前
|
Cloud Native 测试技术 数据库
【云原生之Docker实战】使用Docker部署flatnotes笔记工具
【5月更文挑战第17天】使用Docker部署flatnotes笔记工具
93 8
|
1月前
|
监控 安全 Cloud Native
【云原生之Docker实战】使用Docker部署Ward服务器监控工具
【5月更文挑战第11天】使用Docker部署Ward服务器监控工具
73 4
|
1月前
|
Cloud Native 安全 Linux
【云原生之Docker实战】使用Docker部署mBlog微博系统
【5月更文挑战第10天】使用Docker部署mBlog微博系统
36 2
|
1月前
|
运维 Linux Docker
Docker详解(十一)——Docker容器CPU资源限额实战Docker详解
Docker详解(十一)——Docker容器CPU资源限额实战
58 5
|
1月前
|
存储 缓存 监控
【Docker 专栏】Docker 容器性能调优实战
【5月更文挑战第8天】本文探讨了Docker容器的性能调优技巧,包括理解容器性能指标(如CPU、内存、网络和磁盘I/O)并进行相应调优。重点讲述了CPU和内存的限制设置,网络配置优化以及磁盘I/O性能提升方法。通过实例展示了如何解决高CPU使用率问题,强调了根据应用需求进行调优的重要性,以实现更高效、稳定的容器运行。
【Docker 专栏】Docker 容器性能调优实战
|
1月前
|
Docker 容器
电子好书发您分享《4天实战 轻松玩转docker4天实战 轻松玩转docker》
📚 《4天实战:轻松玩转Docker》电子书分享🚀。通过4天的学习,快速掌握Docker容器技术。阿里云链接:[阅读全书](https://developer.aliyun.com/ebook/7445/20547?spm=a2c6h.26392459.ebook-detail.4.73e72e17w6wZGj)。书中包含丰富实例和图片,助你轻松上手!PNG图像示意。
40 2