Docker容器入门_学习笔记(狂神说Java)- 雁迟

视频源:【狂神说Java】Docker最新超详细版教程通俗易懂 https://www.bilibili.com/video/BV1og4y1q7M4/

Docker的基本组成

镜像(image):

docker镜像就好比是一个目标,可以通过这个目标来创建容器服务,tomcat镜像==>run==>容器(提供服务器),通过这个镜像可以创建多个容器(最终服务运行或者项目运行就是在容器中的)。

容器(container):

Docker利用容器技术,独立运行一个或者一组应用,通过镜像来创建的.
启动,停止,删除,基本命令
目前就可以把这个容器理解为就是一个简易的 Linux系统。

仓库(repository):

仓库就是存放镜像的地方!
仓库分为公有仓库和私有仓库。(很类似git)
Docker Hub是国外的。
阿里云…都有容器服务器(配置镜像加速!)

安装Docker

环境准备

Linux要求内核3.0以上

# 查看内核
[root@localhost ~]# uname -r
3.10.0-1160.el7.x86_64
# 查看配置
[root@localhost ~]# 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"

安装

帮助文档:https://docs.docker.com/engine/install/

#1.卸载旧版本
yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
#2.需要的安装包
yum install -y yum-utils
#3.设置镜像的仓库
yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
#默认是从国外的,不推荐
#推荐使用国内的
yum-config-manager \
    --add-repo \
    https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#更新yum软件包索引
yum makecache fast
#4.安装docker相关的 docker-ce 社区版 而ee是企业版
yum install docker-ce docker-ce-cli containerd.io
#5、启动docker
systemctl start docker
#6. 使用docker version查看是否安装成功
docker version
#7. 测试
docker run hello-world
#7. 测试
[root@localhost ~]# 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/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/
# 8.查看一下下载的镜像
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   3 months ago   13.3kB

了解:卸载docker

#1. 卸载依赖
yum remove docker-ce docker-ce-cli containerd.io
#2. 删除资源
rm -rf /var/lib/docker
# /var/lib/docker 是docker的默认工作路径!

阿里云镜像加速

1、登录阿里云找到容器服务

https://cr.console.aliyun.com/cn-shenzhen/instances/mirrors

2、配置使用

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://i9xw4n3w.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

复制,一次性执行完毕

回顾HelloWorld流程

img

docker run 流程图

image-20200515102637246

底层原理

Docker是怎么工作的

Docker是一个Client-Server结构的系统,Docker的守护进程运行在主机上。通过Socket从客户端访问!

Docker-Server接收到Docker-Client的指令,就会执行这个命令!

为什么Docker比Vm快
1、docker有着比虚拟机更少的抽象层。由于docker不需要Hypervisor实现硬件资源虚拟化,运行在docker容器上的程序直接使用的都是实际物理机的硬件资源。因此在CPU、内存利用率上docker将会在效率上有明显优势。
2、docker利用的是宿主机的内核,而不需要Guest OS。

GuestOS: VM(虚拟机)里的的系统(OS);

HostOS:物理机里的系统(OS);

img

因此,当新建一个容器时,docker不需要和虚拟机一样重新加载一个操作系统内核。仍而避免引导、加载操作系统内核返个比较费时费资源的过程,当新建一个虚拟机时,虚拟机软件需要加载GuestOS,返个新建过程是分钟级别的。而docker由于直接利用宿主机的操作系统,则省略了这个复杂的过程,因此新建一个docker容器只需要几秒钟

Docker的常用命令

帮助命令

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

帮助文档的地址:https://docs.docker.com/engine/reference/commandline/build/

镜像命令

docker images #查看所有本地主机上的镜像 可以使用docker image ls代替

docker search # 搜索镜像

docker pull # 下载镜像 docker image pull

docker rmi # 删除镜像 docker image rm

docker images 查看所有本地的主机上的镜像

[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   3 months ago   13.3kB

# 解释
#REPOSITORY			# 镜像的仓库源
#TAG				# 镜像的标签
#IMAGE ID			# 镜像的id
#CREATED			# 镜像的创建时间
#SIZE				# 镜像的大小

# 可选项
Options:
  -a, --all             Show all images (default hides intermediate images) #列出所有镜像
  -q, --quiet           Only show numeric IDs # 只显示镜像的id
  
  
# 显示所有镜像的id
[root@localhost ~]# docker images -aq
d1165f221234

docker search 搜索镜像

[root@localhost docker]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   10990     [OK]       
mariadb                           MariaDB Server is a high performing open sou…   4156      [OK] 
......

# 搜索出来的镜像就是STARS大于3000的
[root@localhost docker]# docker search mysql --filter=STARS=3000

docker pull 下载镜像

# 下载镜像 docker pull 镜像名[:tag]
[root@localhost /]# docker pull tomcat:9
9: Pulling from library/tomcat  #如果不写tag,默认就是latest
d960726af2be: Pull complete   #分层下载: docker image 的核心 联合文件系统
e8d62473a22d: Pull complete 
8962bc0fad55: Pull complete 
65d943ee54c1: Pull complete 
da20b77f10ac: Pull complete 
8669a096f083: Pull complete 
e0c0a5e9ce88: Pull complete 
f7f46169d747: Pull complete 
42d8171e56e6: Pull complete 
774078a3f8bb: Pull complete 
Digest: sha256:71703331e3e7f8581f2a8206a612dbeedfbc7bb8caeee972eadca1cc4a72e6b1  # 签名 防伪
Status: Downloaded newer image for tomcat:9
docker.io/library/tomcat:9  #真实地址

#等价于
docker pull tomcat:9
docker pull docker.io/library/tomcat:9

docker rmi 删除镜像

[root@localhost /]# docker rmi -f 镜像id  #删除指定的镜像
[root@localhost /]# docker rmi -f 镜像id 镜像id 镜像id 镜像id  #删除指定的镜像
[root@localhost /]# docker rmi -f $(docker images -aq)   #删除全部的镜像

容器命令

docker run 镜像id # 新建容器并启动

docker ps # 列出所有运行的容器 docker container list

docker rm 容器id # 删除指定容器

docker start 容器id #启动容器

docker restart 容器id #重启容器

docker stop 容器id #停止当前正在运行的容器

docker kill 容器id #强制停止当前容器

说明:我们有了镜像才可以创建容器,Linux,下载centos镜像来学习

新建容器并启动

docker run [可选参数] image | docker container run [可选参数] image 

#参数说明
--name="Name"		容器名字 tomcat01 tomcat02 用来区分容器
-d					后台方式运行
-it 				使用交互方式运行,进入容器查看内容
-p					指定容器的端口 -p 8080(宿主机):8080(容器)
		-p ip:主机端口:容器端口
		-p 主机端口:容器端口(常用)
		-p 容器端口
		容器端口
-P(大写) 				随机指定端口

[root@localhost /]# docker images  # 查看镜像
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
tomcat        9         c43a65faae57   4 weeks ago    667MB
hello-world   latest    d1165f221234   3 months ago   13.3kB
# 后台运行tomcat,-p 表示是把需拟机的8888端口映射到容器中的8080端口
[root@localhost /]# docker run -d -p 8888:8080 c43a65faae57  
f0119f0207f3f5f5db72053ae8915e8e326a4c8bc51c478dd1ef3c80d3c08a1a
[root@localhost /]# docker ps
CONTAINER ID   IMAGE          COMMAND             CREATED          STATUS          PORTS                                       NAMES
f0119f0207f3   c43a65faae57   "catalina.sh run"   16 minutes ago   Up 16 minutes   0.0.0.0:8888->8080/tcp, :::8888->8080/tcp   trusting_shannon


# 启动并进入容器 
# [root@localhost /]# docker run -it c43a65faae57 /bin/bash  

# 进入已启动容器
[root@localhost /]# docker exec -it  f0119f0207f3 /bin/bash 
root@aabc66e4ef0d:/usr/local/tomcat# ls
BUILDING.txt	 LICENSE  README.md	 RUNNING.txt  conf  logs	    temp     webapps.dist
CONTRIBUTING.md  NOTICE   RELEASE-NOTES  bin	      lib   native-jni-lib  webapps  work
root@aabc66e4ef0d:/usr/local/tomcat# exit #从容器退回主机
exit
[root@localhost /]# 

列出所有运行的容器

#docker ps命令 #列出当前正在运行的容器
  -a, --all             Show all containers (default shows just running)
  -n, --last int        Show n last created containers (includes all states) (default -1)
  -q, --quiet           Only display numeric IDs

退出容器

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

删除容器

docker rm 容器id   #删除指定的容器,不能删除正在运行的容器,如果要强制删除 rm -rf
docker rm -f $(docker ps -aq)  #删除指定的容器
docker ps -a -q|xargs docker rm  #删除所有的容器

启动和停止容器的操作

docker start 容器id	#启动容器
docker restart 容器id	#重启容器
docker stop 容器id	#停止当前正在运行的容器
docker kill 容器id	#强制停止当前容器

容器拷贝文件到本机

# 进入容器
[root@localhost /]# docker exec -it f0119f0207f3 /bin/bash 

# cd到根目录
root@f0119f0207f3:/usr/local/tomcat# cd /  
root@f0119f0207f3:/# ls
bin   dev  home  lib64	mnt  proc  run	 srv  tmp  var
boot  etc  lib	 media	opt  root  sbin  sys  usr
# 新建一个文件
root@f0119f0207f3:/# echo "hello" > java.java 
root@f0119f0207f3:/# cat java.java 
hello
root@f0119f0207f3:/# exit
exit

# 从容器f0119f0207f3中根目录拷贝 java.java 文件到当前目录下 .
[root@localhost /]# docker cp f0119f0207f3:/java.java .

image-20210612121315661

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值