首页 > 其他分享 >44docker

44docker

时间:2022-12-23 23:44:26浏览次数:48  
标签:容器 12 centos 44docker docker root Docker

笔记docker

docker
  • Docker概述

  • Docker安装

  • Docker命令

    • 镜像命令
    • 容器命令
    • 操作命令
    • ....
  • Docker镜像

  • 容器数据卷

  • DockerFile

  • Docker网络原理

  • IDEA整合Docker

  • Docker 集群

  • Docker Swarm

  • CI\CD Jenkins

Docker概述

Docker为什么出现?

一款产品:开发到上线 两套环境! 应用环境,应用配置

开发 ---- 运维。 问题:在我电脑上可以运行!版本更新,导致服务不可用!

环境配置十分麻烦,每一个机器都要去部署环境(集群Redis、ES、Hadoop...) 费时费力。

发布一个项目(jar+(redis MySQL jdk ES)),项目能不能带上环境安装打包!

之前在服务器配置一个应用的环境 Redis MySQL jdk ES Hadoop,配置超麻烦,不能跨平台。windows,最后发布到Linux!

传统:开发jar,运维来做!

现在:开发打包部署上线,一套流程做完!

java -- apk --发布(应用商店)--王二使用apk--安装即可用

java --jar(环境)--打包项目带上环境(镜像)--(Docker仓库:商店)--下载发布的镜像---直接运行即可!

Docker给以上的问题,退出了解决方案!

image-20221223125317315

Docker的思想来自于集装箱!

JRE --多个应用,常见的端口冲突--原来都是交叉的

隔离:Docker核心思想!打包装箱,每个箱子之间互相隔离

举例:水果 生化武器

Docker通过隔离机制,可以将服务器利用到极致。

Docker与Vmware对比

虚拟机:在window中装一个Vmware,通过这个软件可以虚拟出一台或多太电脑 !笨重!

虚拟机也是属于虚拟化技术,Docker容器技术,也是一种 虚拟化技术!

vm:  Linux centos原生镜像(一个电脑)  隔离:需要开启多个虚拟机!   几个G   几分钟
docker: 隔离, 镜像(最核心的环境  4m + jdk + mysql)十分小巧,  运行镜像就可以了!  小巧! 几个M KB  秒级启动!

image-20221223131855131

Docker能干嘛

之前的虚拟机技术

image-20221223132452202

虚拟机技术缺点

1.资源占用十分多

2.冗余步骤多

3.启动很慢!

容器化技术

容器化技术不是模拟的一个完整的操作系统

image-20221223133113655

比较Docker和虚拟机技术的不同

  • 传统虚拟机,虚拟出一条硬件,运行一个完整的操作系统,然后在这个系统上安装和运行软件
  • 容器内的应用直接运行在 宿主机的内,容器是没有自己的内核,也没有虚拟我们的硬件,所以就轻便
  • 每个容器是互相隔离,每个容器内都有一个属于自己的文件系统,互不影响。
DevOps(开发、运维)

应用更快速的交付和部署

传统:一堆帮助文档,安装程序

Docker:打包镜像发布测试,一键运行

更便捷的升级和扩缩容

更简单的系统运维

在容器化之后,我们的开发,测试环境都是高度一致

更高效的计算资源利用:

Docker是 内核 级别的虚拟化,可以在一个物理机上运行很多的容器实例!服务器的性能可以被压榨到极致

Docker安装

Docker基本组成

image-20221223135951109

安装Docker

环境准备

1.需要会点Linux基础

2.Centos7

3.使用Xshell连接远程服务器进行操作

环境查看
#系统内核是3.10以上的
[root@VM-12-9-centos /]# uname -r
3.10.0-1160.71.1.el7.x86_64
# 系统版本
[root@VM-12-9-centos /]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
2.移除旧版本
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine

安装一些必要的系统工具:

sudo yum install -y yum-utils device-mapper-persistent-data lvm2
添加软件源信息:sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

更新yum缓存:

sudo yum makecache fast

安装 Docker-ce:

sudo yum -y install docker-ce

启动 Docker 后台服务

sudo systemctl start docker

测试运行 hello-world

docker run hello-world

image-20221223145000211

由于本地没有hello-world这个镜像,所以会pull一个hello-world的镜像,并在容器内运行。

镜像加速

鉴于国内网络问题,后续拉取 Docker 镜像十分缓慢,我们可以需要配置加速器来解决。

image-20221223150954761

配置文件加入这个即可,然后重启docker

"registry-mirrors": ["https://registry.docker-cn.com"]

删除 Docker CE

sudo yum remove docker-ce
rm -rf /var/lib/docker

run流程

image-20221223151539038

底层原理

image-20221223152005646

Docker的常用命令

帮助命令

docker version            # 显示docker的版本信息
docker info					# 显示docker的系统信息,包括镜像和容器的熟练
docker 命令 --help		# 帮助命令

镜像命令

[root@VM-12-9-centos /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   15 months ago   13.3kB

image-20221223153550528

docker search 搜索镜像

[root@VM-12-9-centos /]# docker search mysql
NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                           MySQL is a widely used, open-source relation…   13607     [OK]       
mariadb                         MariaDB Server is a high performing open sou…   5194      [OK]       
phpmyadmin                      phpMyAdmin - A web interface for MySQL and M…   710       [OK]       
percona                         Percona Server is a fork of the MySQL relati…   597       [OK]       
bitnami/mysql                   Bitnami MySQL Docker Image                      80 

image-20221223154334661

docker pull 下载镜像

例如:下载最新镜像

image-20221223154634783

指定版本下载

image-20221223154826425

image-20221223155352535

image-20221223155438501

docker rmi 删除镜像

(rm 删除 i值images(镜像))

[root@VM-12-9-centos /]# docker rmi 镜像id        # 通过id删除指定的镜像
[root@VM-12-9-centos /]# docker rmi 镜像id 镜像id 镜像id     # 删除多个镜像
[root@VM-12-9-centos /]# docker rmi -f $(docker images -aq)    # 删除所有镜像

image-20221223160050916

容器命令

说明:我们有了镜像就可以创建容器,linux,下载一个centos镜像来测试学习

[root@VM-12-9-centos /]# docker pull centos

新建容器并启动

[root@VM-12-9-centos /]# docker run [可选参数]  images

image-20221223161717213

image-20221223162313004

image-20221223162017969

image-20221223162107395

列出所有的运行的容器

image-20221223162836942

[root@VM-12-9-centos /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@VM-12-9-centos /]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                      PORTS     NAMES
fdeaa9cb4d8d   centos         "/bin/bash"   6 minutes ago   Exited (0) 56 seconds ago             epic_merkle
1691d42a8cb0   feb5d9fea6a5   "/hello"      2 hours ago     Exited (0) 2 hours ago                cranky_goldstine
2a4932baa99a   feb5d9fea6a5   "/hello"      2 hours ago     Exited (0) 2 hours ago                gracious_darwin
[root@VM-12-9-centos /]# 

退出容器

exit  #直接容器停止并退出
ctrl + P + Q   # 容器不停止退出

删除容器

image-20221223163435239

将未启动的容器删除

image-20221223231432536

启动和停止容器的操作

image-20221223163623112

常用其它命令

查看日志

image-20221223165157150

查看容器中进程信息

image-20221223165536963

查看镜像的元数据

image-20221223165609242

进入当前正在运行的容器

# 我们通常容器都是是由后台方式运行的,需要进入容器,修改一些配置

# 命令
方式一:docker exec -it 容器id  bashShell

# 测试
[root@VM-12-9-centos /]# docker exec -it e40f3036a9c0  bin/bash
[root@e40f3036a9c0 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@e40f3036a9c0 /]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 10:13 pts/0    00:00:00 /bin/bash
root        16     0  0 10:16 pts/1    00:00:00 bin/bash
root        31    16  0 10:17 pts/1    00:00:00 ps -ef

    
# 方式二
# docker attach 容器id
[root@VM-12-9-centos /]# docker attach e40f3036a9c0           # 正在执行当前的代码....
[root@e40f3036a9c0 /]#        
 
#docker exec         # 进入容器后开启一个新的终端,可以在里面操作(常用)
#docker attach       # 进入正在执行的终端,不会启动新的进程!

从容器内拷贝文件到主机上

docker cp 容器id:容器内路径 目的的主机路径

    # 测试如下
[root@c1fc971a0ad9 home]# touch a.java      # 在内部新建一个a.java文件
[root@c1fc971a0ad9 home]# ls
a.java
[root@c1fc971a0ad9 home]# exit
exit
[root@VM-12-9-centos /]# docker ps -a   # 查看历史run的容器
CONTAINER ID   IMAGE          COMMAND           CREATED              STATUS                       PORTS     NAMES
c1fc971a0ad9   centos         "/bin/bash"       About a minute ago   Exited (0) 16 seconds ago              tender_ride
e40f3036a9c0   centos         "/bin/bash"       22 minutes ago       Exited (127) 5 minutes ago             dreamy_beaver
c28710f7e355   centos         "-it /bin/bash"   22 minutes ago       Created                                great_mirzakhani
7f47951bc6d9   centos         "-it /bin/bsah"   22 minutes ago       Created                                recursing_wescoff
fdeaa9cb4d8d   centos         "/bin/bash"       2 hours ago          Exited (0) 2 hours ago                 epic_merkle
1691d42a8cb0   feb5d9fea6a5   "/hello"          4 hours ago          Exited (0) 4 hours ago                 cranky_goldstine
2a4932baa99a   feb5d9fea6a5   "/hello"          4 hours ago          Exited (0) 4 hours ago                 gracious_darwin
[root@VM-12-9-centos /]# cd /home
[root@VM-12-9-centos home]# pwd
/home
[root@VM-12-9-centos home]# ls   # 查看当前主机目录下
lighthouse
[root@VM-12-9-centos home]# ll
total 4
drwx------ 3 lighthouse lighthouse 4096 Nov 20 20:26 lighthouse
[root@VM-12-9-centos home]# docker cp c1fc971a0ad9:/home/a.java /home      # 开始拷贝
[root@VM-12-9-centos home]# ls
a.java  lighthouse
[root@VM-12-9-centos home]# 
 
# 拷贝是一个手动过程,未来我们使用 -v 卷的技术,可以实现

小结

image-20221223184805362

熟能生巧 docker --help

attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

练习

https://hub.docker.com/ docker仓库

image-20221223190332471

作业1.docker安装nginx

# 1.搜索镜像 search  可以去docker hub搜索,也可以用命令
# 2.下载镜像 pull
# 3.运行测试
[root@VM-12-9-centos home]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        latest    1403e55ab369   2 days ago      142MB
centos       latest    5d0da3dc9764   15 months ago   231MB
    
# -d 后台运行
# --name  给容器命名
# -p  宿主机端口:容器内部端口
[root@VM-12-9-centos home]# docker run -d --name nginx -p 3344:80 nginx
650396e599becf7d18f89994c043782945507bdc1b38b84f0c95f548ea5d88a0
[root@VM-12-9-centos home]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                           NAMES
650396e599be   nginx     "/docker-entrypoint.…"   10 seconds ago   Up 10 seconds   80/tcp, 0.0.0.0:3344->81/tcp, :::3344->81/tcp   nginx

image-20221223192054274

image-20221223192119277

image-20221223192203120

测试成功!

image-20221223211522449

image-20221223212336234

进入容器

[root@VM-12-9-centos home]# docker exec -it 9ca3a6a8175f /bin/bash
root@9ca3a6a8175f:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@9ca3a6a8175f:/# cd /etc/nginx
root@9ca3a6a8175f:/etc/nginx# ls
conf.d	fastcgi_params	mime.types  modules  nginx.conf  scgi_params  uwsgi_params
root@9ca3a6a8175f:/etc/nginx# 

端口暴露的概念

image-20221223205933488

image-20221223213018760

作业2:docker安装一个tomcat

# 官方的使用
$ docker run -it --rm tomcat:9.0

# 我们之前的启动都是后台,停止了容器,容器还是可以查到   , docker run -it --rm 一般用来测试,用完即删。

# 下载再启动
docker pull tomcat:9.0

# 启动运行
docker run -d -p 3355:8080 --name tomcat01 tomcat

image-20221223225052540

image-20221223214617393

标签:容器,12,centos,44docker,docker,root,Docker
From: https://www.cnblogs.com/socoo-/p/17001851.html

相关文章