Docker入门指南

docker基础

容器、镜像、仓库

安装过程

以CentOS7为例

先更新一下镜像源

sudo yum update

安装docker

sudo yum install docker

镜像配置

https://cr.console.aliyun.com/cn-shenzhen/instances/mirrors 进入该网站,注册阿里云账号,获得镜像服务的地址,如:

https://itweixiang.mirror.aliyuncs.com

获取该地址后,进行docker的镜像配置

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

配置完成后重新加载配置文件并重启

sudo systemctl daemon-reload
sudo systemctl restart docker

hello-world

输入一个命令,查看docker时候正常安装并配置成功

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/

常用命令

查看docker版本和go的版本等

docker version

查看内核和一些硬件信息

docker info

帮助命令

docker --help

镜像命令

image

查看本地所有镜像

docker images

REPOSITIRT:镜像名称

IMAGE ID:镜像ID

TAG:镜像版本,默认latest

CREATED:创建时间

VIRTUAL SIZE:占用硬盘大小

查看本地所有镜像,含中间镜像层,如tomcat的镜像中,还含有jdk的镜像

docker images -a

查看本地所有镜像的ID

docker images -q

查看本地镜像的摘要说明

docker images --digests

查看本地镜像的完整信息,如原来的IMAGE ID是截取一部分

docker images --no-trunc

search

在镜像仓库中查找镜像,以tomcat为例

docker search tomcat

查找结果中,显示镜像的完整信息

docker search --no-trunc tomcat

查找不低于指定收藏值的镜像

docker search -s 30 tomcat

pull

从镜像仓库中下载镜像

docker pull tomcat

下载指定版本号的镜像

docker pull tomcat:latest

rmi

删除镜像

docker rmi tomcat

强制删除镜像

docker rmi -f tomcat

删除指定版本的镜像

docker rmi tomcat:latest

删除多个镜像

docker rmi tomcat hello-world

组合删除所有

docker rmi ${docker images -qa}

容器命令

run

新建并启动容器,以centos为例

docker run centos

相关参数

  • –name:指定容器的名称,没有指定则随机分配

  • -d:后台启动

  • -i:交互模式运行容器

  • -t:为容器重新分配一个伪终端,与-i配合使用

  • -P:大写字母P,随机端口映射

  • -p:小写字母p,指定断后映射,本机端口:容器端口

如:以终端交互的模式运行名为centos-container-service的centos容器

docker run -it --name centos-container-service centos

注意:一些容器在后台启动后,可能会因为没有前台进程而马上关闭,可以将容器以前台进程的方式运行

如:

ps

列出当前运行的所有容器

docker ps

相关参数:

  • -n:-n + 3,前三次运行的容器

  • -a:显示当前运行和历史运行的容器

  • -l:显示最近创建的容器

  • -q:只显示容器编号

  • –no-trunc:显示详细信息

如:删除所有容器

docker rm -f ${docker ps -q}

rm

根据容器ID删除容器

docker rm containerID

该命令只能删除已经停止的容器,如需删除正在运行的容器,需要强制删除

相关参数:

  • -f:强制删除

logs

查看容器的日志信息

docker logs containerID

相关参数:

  • -f:跟随最新日志
  • -t:显示时间戳
  • –tail:显示最后打印的行数

如:查看tomcat最新的一百条日志

docker logs -f --tail=100 tomcat

注意:在一些日志框架中,已经加入了时间戳,所以不常用到-t参数

cp

在容器内进行文件的拷入拷出

docker cp containerID:容器路径 主机路径

如:将容器内的/tmp/yum.log拷贝到主机的/docker/tmp目录

docker cp containerID:/tmp/yum.log  /dokcer/tmp/

也可以将主机上的文件拷贝到容器中,如,新建一个demo.txt文件,并拷贝到容器的根目录中

touch demo.tet
docker cp demo.text containerID:/

top & inspect

  • top

查看容器内的进程信息

docker top containerID
  • inspect

查看容器内的详情,以json数据的形式描述容器

docker top containerID

exec & attach

  • exec

打开新的终端,并执行命令,返回结果

docker exec -t containerID Commond

如:打开新的终端

docker exec -it containerID /bin/bash

如:执行ls 命令

docker exec -t containerID ls

返回的结果:

bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
  • attach

进入上一次的终端

docker attach containerID
  • 区别

attach不会启动新的进程,exec会启动新的进程,并且输入命令可以直接返回结果

stop、kill & start、restart

  • 启动
docker start containerID
  • 重启
docker restart containerID

  • 强制停止
docker stop contaninerID
  • 杀死
docker kill containerID

exit & ctrl + P+Q

  • exit

容器停止并推出

exit

  • ctrl + P + Q

容器不停止,只退出控制台

commit

将已经修改好的容器进行提交,生成新的镜像文件

docker commit -m "summary of commit" containerID imageName:version

相关参数

  • -m:提交的信息
  • -a:作者

容器数据卷

  • 数据卷可在容器之间共享数据
  • 卷中的修改直接生效
  • 卷的更改不会包含在镜像的更新中
  • 卷的生命周期为没有容器使用

启动时添加数据卷

docker run -it -v 主机目录:容器目录 imageName

注意:该命令只能使用镜像名,不能使用容器ID,其中的-v,v是volume的缩写

如:在根目录下新建abc目录,将abc目录下的文件作为centos的数据卷

cd /
mkdir abc
docker run -it -v /abc:/abc centos

注意:这种方式添加的数据卷,不利于纵向的扩展,只能在特定的主机上使用,如其它主机可能没有对应的目录


查看数据是否挂载成功

可以通过inspect进行查看

docker inspect containerID

如:在返回的结果中,在Mounts有Source和Destination的信息,在HostConfig的的Binds中,也有相关信息

"Mounts": [
            {
                "Type": "bind",
                "Source": "/root/abc",
                "Destination": "/cba",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ]
 "HostConfig": {
            "Binds": [
                "/root/abc:/cba"
            ]
 		}

容器只读数据卷

docker run -it -v 主机目录:容器目录:ro imageName

主机可以修改数据卷,容器只能读取数据卷,不能进行修改,其中ro的意思为read only

数据卷容器

在多个容器之间共享数据

docker run -it --volume-from containID imageName

容器停止后,不会影响其它容器的数据共享,容器启动后会自动加载最新的数据。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值