Docker基础命令

只要学,不死就往死里学

Docker概述

Docker是基于Go语言开发的,是一个开源项目!

官网:https://www.docker.com/

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3qJKnR9V-1650007879005)(D:\千锋磊哥笔记\docker\docker介绍用的图片\image-20220310214724952.png)]

文档地址:https://docs.docker.com/ docker超级详细的步骤

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

比较Docker和虚拟机技术的不同:
● 传统虚拟机,虚拟出一条硬件,运行一个完整的操作系统,然后在这个系统上安装和运行软件
● 容器内的应用直接运行在宿主机的内容,容器是没有自己的内核的,也没有虚拟我们的硬件,所以就轻便了
● 每个容器间是互相隔离,每个容器内都有一个属于自己的文件系统,互不影响。

Docker安装

docker的组成
	镜像(image)
		docker镜像就好比一个模板,可以通过这个模板来创建容器服务,tomcat镜像===>run==>tomcat01容器(提供服务器),通过这个镜像可以创建多个容器(最终服务运行或者项目运行就是在容器中的)。
	容器(container)
		docker利用容器技术,独立运行一个或者一个组应用,通过镜像来创建的。
		启动,停止,删除,基本命令!
		目前就看把这个容器理解为就是一个简易的Linux系统		
	仓库(repository)
		仓库就是存放镜像的地方!
		仓库分为公有仓库和私有仓库!
		docker hub (默认是国外的)
		阿里云...都有容器服务器(配置镜像加速!)


安装docker
	环境准备:
		1.会linux的基础
		2.CenOS7镜像
		3.xshell远程连接
环境查看:
	#系统内核时3.10以上的
	[root@localhost ~]# uname -r
	3.10.0-862.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"

帮助文档:
	1.卸载旧服务版本
		sudo 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 \
    	http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo  #阿里云

#更新yum软件包的索引
yum makecache fast

	4.安装docker相关的内容(docker-ce社区版)
		yum install -y docker-ce docker-ce-cli containerd.io

	5.启动docker
		systemctl start docker

	6.使用docker version是否安装成功
		docker version
[root@localhost ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.13
 API version:       1.41
 Go version:        go1.16.15
 Git commit:        a224086
 Built:             Thu Mar 10 14:09:51 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.13
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.15
  Git commit:       906f57f
  Built:            Thu Mar 10 14:08:16 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.5.10
  GitCommit:        2a1d4dbdb2a1030dc5b01e96fb110a9d9f150ecc
 runc:
  Version:          1.0.3
  GitCommit:        v1.0.3-0-gf46b6ba
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

	7.测试
		docker run hello-world
[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:4c5f3db4f8a54eb1e017c385f683a2de6e06f75be442dc32698c9bbe6c861edd
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/

	8.查看下载的halo-world的镜像
		docker images
		REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
		hello-world   latest    feb5d9fea6a5   5 months ago   13.3kB

	9.卸载docker
		#卸载依赖
		yum remove  docker-ce docker-ce-cli containerd.io
		#删除资源
		sudo rm -rf /var/lib/docker
 		sudo rm -rf /var/lib/containerd
#/var/lib/docker:docker默认的工作路径
阿里云镜像加速
sudo mkdir -p /etc/docker

sudo tee /etc/ docker/daemon. json <<- 'EOF'
{
"registry-mirrors": ["https://qiyb9988.mirror.aliyuncs.com"]
EOF

sudo systemctl daemon-reload
sudo systemctl restart docker

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BlcNmnJT-1650007879009)(D:\千锋磊哥笔记\docker\docker介绍用的图片\image-20220312155332412.png)]

底层原理
docker是怎么工作的
Docker是一个Client-Server结构的系统, Docker的守护进程运行在主机上。通过Socket从客户端访问 !
DockerServer接收到Docker-Client的指令,就会执行这个命令!|
docker为什么比vm快
1.Docker有着比虚拟机更少的抽象层。
2.docker利用的是宿主机的内核,vm需要的是GuestOS。
所以说,新建一个容器的时候, docker不需要想虚拟机一样重新加载一个操作系统内核 ,避免引导。虚拟机是加载GuestOS ,分钟级别的,而docker是利用宿主机的操作系统,省略了这个复杂的过程,秒级!

Docker命令

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

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

镜像命令

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

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

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


#可选项
  -a, --all             #列出所有镜像
  -q, --quiet           #只显示镜像的id

docker search //搜索镜像

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


#可选项,通过收藏来过滤
--filter=STARS=3000        #搜索出来的镜像就是STARS大于3000
[root@localhost ~]# docker search mysql --filter=STARS=3000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   12405     [OK]       
mariadb   MariaDB Server is a high performing open sou…   4780      [OK]

docker pull //下载镜像

#下载镜像 docker pull 镜像名[:tag]
[root@localhost ~]# docker pull mysql
Using default tag: latest     #如果不写 tag,默认就是latest
latest: Pulling from library/mysql
72a69066d2fe: Already exists  #分层下载,docker images的核心,联合文件系统
93619dbc5b36: Already exists 
99da31dd6142: Pull complete 
626033c43d70: Pull complete 
37d5d7efb64e: Pull complete 
ac563158d721: Pull complete 
d2ba16033dad: Pull complete 
688ba7d5c01a: Pull complete 
00e060b6d11d: Pull complete 
1c04857f594f: Pull complete 
4d7cfa90e6ea: Pull complete 
e0431212d27d: Pull complete 
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709#签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest   #真实地址

#等价于它
docker pull mysql
docker pull docker.io/library/mysql:latest

#指定版本下载
[root@localhost ~]# docker pull  mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists 
93619dbc5b36: Already exists 
99da31dd6142: Already exists 
626033c43d70: Already exists 
37d5d7efb64e: Already exists 
ac563158d721: Already exists 
d2ba16033dad: Already exists 
0ceb82207cd7: Pull complete 
37f2405cae96: Pull complete 
e2482e017e53: Pull complete 
70deed891d42: Pull complete 
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sUfiY9MY-1650007879011)(D:\千锋磊哥笔记\docker\docker介绍用的图片\image-20220414113354105.png)]

docker rmi

[root@localhost ~]# docker rmi -f 容器id  #删除指定的容器
[root@localhost ~]# docker rmi -f 容器id 容器id 容器id  #删除多个容器
[root@localhost ~]# docker rmi -f $(docker images -aq) #删除全部容器
容器命令

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

docker pull centos

新建容器并启动

docker run [可选参数] image

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



#测试,启动并进入容器
[root@localhost ~]# docker run -it centos /bin/bash
[root@52f61304d145 /]# ls    #查看容器内的centos版本,基础版本,很多命令是不完善的
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

列出所有运行的容器

#docker ps
	#列出当前正在运行的容器
-a	#列出当店正在运行的容器+带出历史运行过的容器
-n=?	#显示最近创建的容器
-q	#只显示容器的编号

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED         STATUS                      PORTS     NAMES
52f61304d145   centos        "/bin/bash"   2 minutes ago   Exited (0) 12 seconds ago             nice_banach
58d095a97df2   hello-world   "/hello"      23 hours ago    Exited (0) 23 hours ago               festive_jepsen
[root@localhost ~]# docker ps -a -n=1
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                          PORTS     NAMES
52f61304d145   centos    "/bin/bash"   4 minutes ago   Exited (0) About a minute ago             nice_banach

退出容器

exit	#直接容器停止退出
ctrl+p+q	#不停止退出

删除容器

docker rm  容器id					#删除指定的容器,不能删除正在运行的容器,如果要强制删除需要 rm -f
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		#强制停止当前容器
常用其他的命令

后台启动

#命令docker run -d 镜像名
[root@localhost ~]# docker run -d centos

#问题docker ps 发现centos停止了

#常见的坑:docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
#nginx,容器启动后,发现直接没有提供服务,就会立刻停止,就是没有程序了

查看日志

docker logs -f -t --tail 容器,没有日志


#可以直接写个脚本
[root@localhost ~]# docker run -d centos /bin/bash -c "while true;do echo erha;sleep 1;done"         #while的意思,每隔零一秒输出一个二哈

#[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE
499d99aedc59   centos

#显示日志
-tf			#显示日志
--tail number	#要显示日志条数
[root@localhost ~]# docker logs -tf --tail 10 499d99aedc59

查看容器中进程信息

#命令 docker top   容器id
[root@localhost ~]# docker top 499d99aedc59
UID                 PID                 PPID                C                   STIME               TTY
root                5195                5172                0                   11:31               ? 
root                5519                5195                0                   11:34               ? 

查看镜像元数据

#命令  docker inspec  容器id


#测试
[root@localhost ~]# docker inspect 499d99aedc59

进入当前正在运行的容器

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


#命令
docker exec -it 容器id bashshell(打开一个新的命令行终端)

#测试
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
499d99aedc59   centos    "/bin/bash -c 'while…"   9 minutes ago   Up 9 minutes             reverent_keller
[root@localhost ~]# docker exec -it 499d99aedc59 /bin/bash
[root@499d99aedc59 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@499d99aedc59 /]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 03:31 ?        00:00:00 /bin/bash -c while true;do echo erha;sleep 1;done
root        582      0  1 03:40 pts/0    00:00:00 /bin/bash
root        605    582  0 03:40 pts/0    00:00:00 ps -ef
root        606      1  0 03:40 ?        00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1


#方式二
docker attach 容器id(直接进入)
#测试
[root@localhost ~]# docker attach   499d99aedc59
正在执行当前的代码



#docker exec		#进入容器后开启一个新的终端,可以在里面操作(常用)
#docker attach		#进入容器正在执行的终端,不会启动新的进程!

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

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

#查看当前主机目录下
[root@localhost ~]# docker run -it centos /bin/bash
[root@6dbce13bdcd2 /]# [root@localhost ~]# 

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
6dbce13bdcd2   centos    "/bin/bash"   16 seconds ago   Up 12 seconds             sharp_joliot
[root@localhost ~]# cd /home
[root@localhost home]# ll
总用量 0
drwx------. 3 dy dy 78 4月  11 2018 dy

#进入docker容器内部
[root@localhost home]# docker attach 6dbce13bdcd2
[root@6dbce13bdcd2 /]# cd /home
#在容器内新建一个文件
[root@6dbce13bdcd2 home]# touch test.jav
[root@6dbce13bdcd2 home]# exit
exit

[root@localhost home]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS                      PORTS     NAMES
6dbce13bdcd2   centos    "/bin/bash"   About a minute ago   Exited (0) 15 seconds ago             sharp_joliot
#将这个文件考出来到主机上
[root@localhost home]# docker cp 6dbce13bdcd2:/home/test.jav /home
[root@localhost home]# ll
总用量 0
drwx------. 3 dy   dy   78 4月  11 2018 dy
-rw-r--r--  1 root root  0 4月  15 11:57 test.jav
[root@localhost home]#

#拷贝是一个手动的过程,未来我们使用-v卷的技术,可以实现
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值