Docker容器基础命令

Docker容器基础命令

  • 一、安装部署环境
    • 1.依赖环境
    • 2.镜像加速
    • 3.网络优化
  • 二、docker镜像操作
      • 镜像重命名和标签
      • 删除镜像
      • 导入导出镜像
  • 三、docker容器操作
      • 容器的创建
      • 查看所有运行的容器状态
      • 启动容器
      • 如果想创建并启动容器,可以执行docker run
      • 进入正在运行的容器
      • 容器的导入和导出
      • 批量删除停止状态的容器

一、安装部署环境

1.依赖环境

[root@localhost ~]# yum -y install yum-utils device-mapper-persistent-data lvm2
[root@localhost ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost yum.repos.d]# yum -y install docker-ce	#需要现网源
[root@localhost ~]# mkdir -p /etc/docker

2.镜像加速

[root@localhost ~]# tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://caffndpq.mirror.aliyuncs.com"]		#此加速地址可以在阿里云申请一个自己的专网地址,每个阿里云账号都可申请一个
}
EOF
{
  "registry-mirrors": ["https://caffndpq.mirror.aliyuncs.com"]
}
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker

3.网络优化

[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1										#开启路由转发
[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1

二、docker镜像操作

[root@localhost ~]# docker version	#查看版本
[root@localhost ~]# docker search nginx		#搜索镜像
[root@localhost ~]# docker pull nginx		#下载镜像,下载后存放在/var/lib/docker
[root@localhost ~]# docker images		#查看镜像列表
[root@localhost yum.repos.d]# docker inspect c39a868aad02		#查看镜像详细信息,c39a868aad02为镜像id号

镜像重命名和标签

docker tag 名称:[标签] 新名称:[新标签]

[root@localhost yum.repos.d]# docker tag nginx:latest newnginx:newtag
[root@localhost yum.repos.d]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
newnginx            newtag              c39a868aad02        3 days ago          133MB
nginx               latest              c39a868aad02        3 days ago          133MB

删除镜像

docker rmi 仓库名称:标签

[root@localhost ~]# docker rmi newnginx:newtag		#一个镜像有多个标签时,删除其中一个不会删除镜像文件
Untagged: newnginx:newtag
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              c39a868aad02        3 days ago          133MB
[root@localhost ~]# docker rmi nginx:latest		#将镜像文件的所有标签都删除后,镜像文件删除
Untagged: nginx:latest
Untagged: nginx@sha256:aeade65e99e5d5e7ce162833636f692354c227ff438556e5f3ed0335b7cc2f1b
Deleted: sha256:c39a868aad02a383c7e490e0fc4a5b0217f667f2de764bc2755e315a5adf64a1
Deleted: sha256:1af47386c12cb24b362bd785e71058ab675fbdcbda8e7a7ba0dd10c3cff75a2a
Deleted: sha256:25ee02f9e42f8cdca7ca8bc522cb69ac4a86a55401818a13c7a01e52a81894fe
Deleted: sha256:b1d2bc3292aa3d64794f99d2885d35b454993b4af24ad350a969d27925db7ebb
Deleted: sha256:4216e20d59f9fc90bccdd14493452532806d4803a25514366de2a9c5560624d0
Deleted: sha256:d0fe97fa8b8cefdffcef1d62b65aba51a6c87b6679628a2b50fc6a7a579f764c

导入导出镜像

[root@localhost ~]# docker save -o /opt/nginx nginx:latest		#导出镜像到/opt/nginx
[root@localhost ~]# ls -h /opt
containerd  nginx
[root@localhost ~]# docker load < /opt/nginx			#导入镜像
d0fe97fa8b8c: Loading layer  72.49MB/72.49MB
2baf69a23d7a: Loading layer  64.54MB/64.54MB
2f57e21e4365: Loading layer  3.072kB/3.072kB
aee208b6ccfb: Loading layer  4.096kB/4.096kB
7b5417cae114: Loading layer  3.584kB/3.584kB
Loaded image: nginx:latest

三、docker容器操作

容器的创建

docker create [选项] 镜像 运行的程序
-i 让容器的输入保持打开
-t 让docker 分配一个伪终端

[root@localhost ~]# docker create -it nginx /bin/bash
b260e91665e14779650368db982fc56789f2b82bce9ac465879820a2387b08c2

查看所有运行的容器状态

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
b260e91665e1        nginx               "/docker-entrypoint.…"   4 minutes ago       Created                                 nifty_joliot

启动容器

[root@localhost ~]# docker start b260e91665e1
b260e91665e1
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
b260e91665e1        nginx               "/docker-entrypoint.…"   5 minutes ago       Up 11 seconds       80/tcp              nifty_joliot

如果想创建并启动容器,可以执行docker run

[root@localhost ~]# docker run centos /usr/bin/bash -c ls
Unable to find image 'centos:latest' locally		#若本地有镜像,则直接创建容器,没有镜像就会自动去下载
latest: Pulling from library/centos
3c72a8ed6814: Pull complete 
Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd
Status: Downloaded newer image for centos:latest
bin
dev
...
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
6219fcdbeede        centos              "/usr/bin/bash -c ls"    6 minutes ago       Exited (0) 6 minutes ago                       upbeat_hopper
b260e91665e1        nginx               "/docker-entrypoint.…"   14 minutes ago      Up 9 minutes               80/tcp              nifty_joliot

正常容器任务结束后,系统会自动释放资源,容器状态为Exited (0)
若非正常自动释放而停止的容器,状态码为非0

[root@localhost ~]# docker run -d centos /usr/bin/bash -c "while true;do echo hello;done"
74dd59ac7e7b12ecbacd9f7f030a83fae5282ed241d6966e69271e97a0e9037a
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
74dd59ac7e7b        centos              "/usr/bin/bash -c 'w…"   42 seconds ago      Up 41 seconds                                  heuristic_matsumoto
b260e91665e1        nginx               "/docker-entrypoint.…"   25 minutes ago      Exited (0) 8 minutes ago                       nifty_joliot
[root@localhost ~]# docker stop 74dd59ac7e7b
74dd59ac7e7b
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
74dd59ac7e7b        centos              "/usr/bin/bash -c 'w…"   2 minutes ago       Exited (137) 58 seconds ago                       heuristic_matsumoto
b260e91665e1        nginx               "/docker-entrypoint.…"   27 minutes ago      Exited (0) 10 minutes ago                         nifty_joliot

总结:若容器内有命令正在运行,手动stop,容器状态码为非0.若容器内空闲状态而手动stop,状态码为0

进入正在运行的容器

docker exec -it 容器id /bin/bash
[root@localhost ~]# docker exec -it 74dd59ac7e7b /bin/bash
[root@74dd59ac7e7b /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

容器的导入和导出

docker export 容器id > 文件名
cat 文件名 | docker import

docker export 74dd59ac7e7b > /opt/centos_docker	#导出
docker rm 74dd59ac7e7b				#删除容器
docker rm -f b260e91665e1				#强制删除正在运行的容器
cat /opt/centos_docker | docker import - centos:test	#导入,只能导入成镜像,不能成容器

批量删除停止状态的容器

docker ps -a | awk '{print "docker rm "$1}' | bash

你可能感兴趣的:(容器,docker)