Docker入门到精通(一)基础命令 - 开发说
当前位置: 主页 » Docker » Docker入门到精通(一)基础命令

Docker入门到精通(一)基础命令

      2020年06月21日   阅读 836 次     0 评论   Tags:

Docker基于Go语言开发的!开源项目!
docker官网:https://www.docker.com/
文档:https://docs.docker.com/ Docker的文档是超级详细的!
仓库:https://hub.docker.com/

1、Docker安装

Docker的基本组成

镜像(image):

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

容器(container):

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

仓库(repository):

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

1、安装Docker 帮助文档: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
#6. 使用docker version查看是否按照成功
docker version
#7. 测试
docker run hello-world

#8、测试hello-world
[root@mil data]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
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/


#9、查看一下下载的镜像
[root@mil data]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        5 months ago        13.3kB

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

2、docker run 流程图

3、底层原理!

Docker是怎么工作的?

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

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

docker和VM虚拟机对比

4、Docker的常用命令


### 帮助命令

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

### 镜像命令
docker images #查看所有本地主机上的镜像 可以使用docker image ls代替
docker search 搜索镜像
docker pull 下载镜像 docker image pull
docker rmi 删除镜像 docker image rm

### docker images 查看所有本地的主机上的镜像
[root@mil data]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        5 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

### docker images -aq #显示所有镜像的id
### docker search 搜索镜像
[root@mil data]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9647                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3510                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   704                                     [OK]
            

# --filter=STARS=3000 #搜索出来的镜像就是STARS大于3000的

[root@mil data]# docker search mysql --filter=STARS=1800
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql               MySQL is a widely used, open-source relation…   9647                [OK]                
mariadb             MariaDB is a community-developed fork of MyS…   3510                [OK] 

### docker pull 下载镜像

# 下载镜像 docker pull 镜像名[:tag]
[root@mil data]# docker pull tomcat:8
8: Pulling from library/tomcat #如果不写tag,默认就是latest
90fe46dd8199: Already exists   #分层下载: docker image 的核心 联合文件系统
35a4f1977689: Already exists 
bbc37f14aded: Already exists 
74e27dc593d4: Already exists 
93a01fbfad7f: Already exists 
1478df405869: Pull complete 
64f0dd11682b: Pull complete 
68ff4e050d11: Pull complete 
f576086003cf: Pull complete 
3b72593ce10e: Pull complete 
Digest: sha256:0c6234e7ec9d10ab32c06423ab829b32e3183ba5bf2620ee66de866df640a027  # 签名 防伪
Status: Downloaded newer image for tomcat:8
docker.io/library/tomcat:8 #真实地址

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

### docker rmi 删除镜像
docker rmi -f 镜像id #删除指定的镜像
docker rmi -f 镜像id 镜像id 镜像id 镜像id#删除指定的镜像
docker rmi -f $(docker images -aq) #删除全部的镜像

5、容器命令



docker run 镜像id 新建容器并启动
docker ps 列出所有运行的容器 docker container list
docker rm 容器id 删除指定容器

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

### 新建容器并启动
docker run [可选参数] image | docker container run [可选参数] image 
#参书说明
--name="Name"		容器名字 tomcat01 tomcat02 用来区分容器
-d			后台方式运行
-it 			使用交互方式运行,进入容器查看内容
-p			指定容器的端口 -p 8080(宿主机):8080(容器)
		-p ip:主机端口:容器端口
		-p 主机端口:容器端口(常用)
		-p 容器端口
		容器端口
-P(大写) 				随机指定端口
# 测试、启动并进入容器
[root@mil data]# docker run -it centos /bin/bash
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
6910e5a164f7: Pull complete 
Digest: sha256:4062bbdd1bb0801b0aa38e0f83dece70fb7a5e9bce223423a68de2d8b784b43b
Status: Downloaded newer image for centos:latest
[root@65d2ab696761 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@65d2ab696761 /]# exit
exit

### 列出所有运行的容器
#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
  
[root@mil data]# docker ps   
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                    NAMES
68729e9654d4        portainer/portainer   "/portainer"             14 hours ago        Up About a minute   0.0.0.0:8088->9000/tcp   funny_curie
d506a017e951        nginx                 "nginx -g 'daemon of…"   15 hours ago        Up 15 hours

### 推出容器
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	#强制停止当前容器

###常用其他命令
后台启动命令

# 命令 docker run -d 镜像名
[root@mil data]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@mil data]# docker run -d centos
4684c8bae3ebe468a66dc09bd190979786beec661582350a1ed66b106e9aa54e
[root@mil data]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

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

###查看日志

[root@mil data]# docker logs --help

Usage:	docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
      --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
➜  ~ docker run -d centos /bin/sh -c "while true;do echo 6666;sleep 1;done" #模拟日志      
#显示日志
-tf		#显示日志信息(一直更新)
--tail number #需要显示日志条数
docker logs -t --tail n 容器id #查看n行日志
docker logs -ft 容器id #跟着日志

[root@mil data]# docker logs -t --tail 10 909
2020-06-20T21:12:57.651434268Z 6666
2020-06-20T21:12:58.654422581Z 6666
2020-06-20T21:12:59.657570624Z 6666
2020-06-20T21:13:00.662156083Z 6666
2020-06-20T21:13:01.665756239Z 6666
2020-06-20T21:13:02.668838363Z 6666
2020-06-20T21:13:03.673977354Z 6666
2020-06-20T21:13:04.676988013Z 6666
2020-06-20T21:13:05.679979027Z 6666
2020-06-20T21:13:06.682976124Z 6666

###查看容器中进程信息 ps

docker top 容器id
[root@mil data]# docker top 909
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                4057                4041                0                   21:12               ?                   00:00:00            /bin/sh -c while true;do echo 6666;sleep 1;done
root                4169                4057                0                   21:13               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1

### 查看镜像的元数据
# 命令
docker inspect 容器id

[root@mil data]# docker inspect 909
[
    {
        "Id": "909de3fe0e673abda09b4dd4d4cd07fd64b25ed551ecb222df35afc72b189d98",
        "Created": "2020-06-20T21:12:24.932422987Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true;do echo 6666;sleep 1;done"
        ],
]

进入当前正在运行的容器

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

# 命令 
docker exec -it 容器id bashshell

#测试
[root@mil data]# docker exec -it 909de3fe0e67 /bin/bash
[root@909de3fe0e67 /]# ll

# 方式二
docker attach 容器id
#测试
docker attach 909de3fe0e67 
正在执行当前的代码...
区别
#docker exec #进入当前容器后开启一个新的终端,可以在里面操作。(常用)
#docker attach # 进入容器正在执行的终端

### 从容器内拷贝到主机上

[root@mil data]# docker exec -it 909 /bin/bash
[root@909de3fe0e67 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@909de3fe0e67 /]# echo "hello" >hello.txt
[root@909de3fe0e67 /]# ls
bin  dev  etc  hello.txt  home	lib  lib64  lost+found	media  mnt  opt  proc  root  run  sbin	srv  sys  tmp  usr  var
[root@909de3fe0e67 /]# cat hello.txt 
hello
[root@909de3fe0e67 /]# exit
exit
[root@mil data]# docker cp 909de3fe0e67:/hello.txt ./
[root@mil data]# ll
total 4
drwx--x--x 14 root root 182 Jun 20 20:45 docker
-rw-r--r--  1 root root   6 Jun 20 21:18 hello.txt


小结:



  attach      Attach local standard input, output, and error streams to a running container
  #当前shell下 attach连接指定运行的镜像
  build       Build an image from a Dockerfile # 通过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 #查看docker容器的变化
  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 #导出容器文件系统作为一个tar归档文件[对应import]
  history     Show the history of an image # 展示一个镜像形成历史
  images      List images #列出系统当前的镜像
  import      Import the contents from a tarball to create a filesystem image #从tar包中导入内容创建一个文件系统镜像
  info        Display system-wide information # 显示全系统信息
  inspect     Return low-level information on Docker objects #查看容器详细信息
  kill        Kill one or more running containers # kill指定docker容器
  load        Load an image from a tar archive or STDIN #从一个tar包或标准输入中加载一个镜像[对应save]
  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


启动两个nginx容器


[root@mil data]# docker run -d --name nginx01 -p 83:80 nginx
09f4e62b7ff7a7dec1dacc773b6d1ca73d60f9f9a94a16de14cacb44bbfaa54d
[root@mil data]# docker run -d --name nginx02 -p 82:80 nginx
0127b0c3c96eacf28c755de242fc58b3c17c2b63d7c94ac81f085d4ef67f6bd3
[root@mil data]# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
0127b0c3c96e        nginx               "/docker-entrypoint.…"   10 seconds ago      Up 8 seconds        0.0.0.0:82->80/tcp   nginx02
09f4e62b7ff7        nginx               "/docker-entrypoint.…"   26 seconds ago      Up 24 seconds       0.0.0.0:83->80/tcp   nginx01

### docker stats # 查看docker容器使用内存情况
[root@mil data]# docker stats

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
0127b0c3c96e        nginx02             0.00%               1.387MiB / 1.795GiB   0.08%               656B / 0B           0B / 0B             2
09f4e62b7ff7        nginx01             0.00%               1.391MiB / 1.795GiB   0.08%               656B / 0B           0B / 0B             2


部署es+kibana

### 添加内存的限制,修改配置文件 -e 环境配置修改



[root@mil data]# docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2

[root@mil data]# curl localhost:9200
{
  "name" : "ecdd8358b8fb",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "oPXkZxgjQaqNWVsgdH7PWA",
  "version" : {
    "number" : "7.6.2",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
    "build_date" : "2020-03-26T06:34:37.794943Z",
    "build_snapshot" : false,
    "lucene_version" : "8.4.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}


Docker 可视化:portainer



docker run -d -p 8080:9000 \
--restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer

  • 版权声明:本文版权归开发说和原作者所有,未经许可不得转载。文章部分来源于网络仅代表作者看法,如有不同观点,欢迎进行交流。除非注明,文章均由 开发说 整理发布,欢迎转载,转载请带版权。

  • 来源:开发说 ( https://www.kaifashuo.com/ ),提供主机优惠信息深度测评和服务器运维编程技术。
  • 链接:https://www.kaifashuo.com/2013.html
  • 评论(0

    1. 还没有任何评论,你来说两句吧

    发表回复

    您的电子邮箱地址不会被公开。 必填项已用 * 标注