【docker】docker的安装和简单入门使用

在这里插入图片描述

安装docker

# step 1: 安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3: 更新并安装 Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce
# Step 4: 开启Docker服务
sudo service docker start

注意:其他注意事项在下面的注释中
# 官方软件源默认启用了最新的软件,您可以通过编辑软件源的方式获取各个版本的软件包。例如官方并没有将测试版本的软件源置为可用,你可以通过以下方式开启。同理可以开启各种测试版本等。
# vim /etc/yum.repos.d/docker-ce.repo
#   将 [docker-ce-test] 下方的 enabled=0 修改为 enabled=1
#
# 安装指定版本的Docker-CE:
# Step 1: 查找Docker-CE的版本:
# yum list docker-ce.x86_64 --showduplicates | sort -r
#   Loading mirror speeds from cached hostfile
#   Loaded plugins: branch, fastestmirror, langpacks
#   docker-ce.x86_64            17.03.1.ce-1.el7.centos            docker-ce-stable
#   docker-ce.x86_64            17.03.1.ce-1.el7.centos            @docker-ce-stable
#   docker-ce.x86_64            17.03.0.ce-1.el7.centos            docker-ce-stable
#   Available Packages
# Step2 : 安装指定版本的Docker-CE: (VERSION 例如上面的 17.03.0.ce.1-1.el7.centos)
# sudo yum -y install docker-ce-[VERSION]
# 注意:在某些版本之后,docker-ce安装出现了其他依赖包,如果安装失败的话请关注错误信息。例如 docker-ce 17.03 之后,需要先安装 docker-ce-selinux。
# yum list docker-ce-selinux- --showduplicates | sort -r
# sudo yum -y install docker-ce-selinux-[VERSION]

# 通过经典网络、VPC网络内网安装时,用以下命令替换Step 2中的命令
# 经典网络:
# sudo yum-config-manager --add-repo http://mirrors.aliyuncs.com/docker-ce/linux/centos/docker-ce.repo
# VPC网络:
# sudo yum-config-manager --add-repo http://mirrors.could.aliyuncs.com/docker-ce/linux/centos/docker-ce.repo

安装校验

root@iZbp12adskpuoxodbkqzjfZ:$ docker version
Client:
 Version:      17.03.0-ce
 API version:  1.26
 Go version:   go1.7.5
 Git commit:   3a232c8
 Built:        Tue Feb 28 07:52:04 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.03.0-ce
 API version:  1.26 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   3a232c8
 Built:        Tue Feb 28 07:52:04 2017
 OS/Arch:      linux/amd64
 Experimental: false

配置镜像加速器

可在阿里云免费注册,xxxxx换成自己的

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

docker指令

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides
                           DOCKER_HOST env var and default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal")
                           (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  app*        Docker App (Docker Inc., v0.9.1-beta3)
  builder     Manage builds
  buildx*     Build with BuildKit (Docker Inc., v0.6.1-docker)
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  scan*       Docker Scan (Docker Inc., v0.8.0)
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a 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
  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
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  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

Run 'docker COMMAND --help' for more information on a command.

拉取/删除tomcat镜像

#默认拉取tag:latest的镜像
docker pull tomcat
#删除镜像
docker rmi tomcat
#强制删除
docker rmi -f tomcat
#删除多个
docker rmi -f tomcat nginx
#删除全部
docker rmi -f $(docker images -qa)

在这里插入图片描述

实例化一个容器,并启动

#参数列表
--name="容器新名字" 指定一个别名
-d 后台运行容器,并返回容器ID,即启动守护式容器
-i 以交互模式运行容器,通常与-t同时使用
-t 为容器重新分配一个伪输入终端,通常与-i同时使用
-P 随机端口映射
-p 指定端口映射

#实例化
[root@Dragon ~]# dk run -it --name mycentos 5d0da3dc9764
[root@5a0b6a6c5e06 /]# ls
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@5a0b6a6c5e06 /]# pwd
/
[root@5a0b6a6c5e06 /]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 03:46 pts/0    00:00:00 /bin/bash
root         16      1  0 03:46 pts/0    00:00:00 ps -ef

退出容器

  • exit 关闭退出容器
[root@5a0b6a6c5e06 /]# exit
exit
[root@Dragon ~]# dk ps
CONTAINER ID   IMAGE          COMMAND                  CREATED       STATUS       PORTS                                                  NAMES
1ae5e7e69adb   redis:latest   "docker-entrypoint.s…"   3 weeks ago   Up 2 hours   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp              c_redis
3a796708dfa9   mysql:8.0.17   "docker-entrypoint.s…"   3 weeks ago   Up 2 hours   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   c_mysql

#-l last
[root@Dragon ~]# dk ps -l
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                      PORTS     NAMES
5a0b6a6c5e06   5d0da3dc9764   "/bin/bash"   5 minutes ago   Exited (0) 13 seconds ago             mycentos

#-a all
[root@Dragon ~]# dk ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS                      PORTS                                                  NAMES
5a0b6a6c5e06   5d0da3dc9764   "/bin/bash"              5 minutes ago   Exited (0) 20 seconds ago                                                          mycentos
1ae5e7e69adb   redis:latest   "docker-entrypoint.s…"   3 weeks ago     Up 2 hours                  0.0.0.0:6379->6379/tcp, :::6379->6379/tcp              c_redis
3a796708dfa9   mysql:8.0.17   "docker-entrypoint.s…"   3 weeks ago     Up 2 hours                  0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   c_mysql
  • ctrl+p+q 退出不关闭容器
[root@Dragon ~]# dk run -it --name mycentos0152 centos
[root@ca40c9c29850 /]# [root@Dragon ~]# 
[root@Dragon ~]# dk ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
ca40c9c29850   centos         "/bin/bash"              41 seconds ago   Up 40 seconds                                                          mycentos0152
1ae5e7e69adb   redis:latest   "docker-entrypoint.s…"   3 weeks ago      Up 3 hours      0.0.0.0:6379->6379/tcp, :::6379->6379/tcp              c_redis
3a796708dfa9   mysql:8.0.17   "docker-entrypoint.s…"   3 weeks ago      Up 3 hours      0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   c_mysql

启动容器

#查看上两个容器
[root@Dragon ~]# dk ps -n 2
CONTAINER ID   IMAGE          COMMAND       CREATED             STATUS                      PORTS     NAMES
ca40c9c29850   centos         "/bin/bash"   2 minutes ago       Up 2 minutes                          mycentos0152
5a0b6a6c5e06   5d0da3dc9764   "/bin/bash"   About an hour ago   Exited (0) 56 minutes ago             mycentos

#启动刚才关闭的容器
[root@Dragon ~]# dk start mycentos
mycentos
[root@Dragon ~]# dk ps
CONTAINER ID   IMAGE          COMMAND                  CREATED             STATUS         PORTS                                                  NAMES
ca40c9c29850   centos         "/bin/bash"              5 minutes ago       Up 5 minutes                                                          mycentos0152
5a0b6a6c5e06   5d0da3dc9764   "/bin/bash"              About an hour ago   Up 6 seconds                                                          mycentos
1ae5e7e69adb   redis:latest   "docker-entrypoint.s…"   3 weeks ago         Up 3 hours     0.0.0.0:6379->6379/tcp, :::6379->6379/tcp              c_redis
3a796708dfa9   mysql:8.0.17   "docker-entrypoint.s…"   3 weeks ago         Up 3 hours     0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   c_mysql

#重启
[root@Dragon ~]# dk restart mycentos

关闭容器

[root@Dragon ~]# dk stop mycentos
mycentos
[root@Dragon ~]# dk ps
CONTAINER ID   IMAGE          COMMAND                  CREATED       STATUS       PORTS                                                  NAMES
ca40c9c29850   centos         "/bin/bash"              2 hours ago   Up 2 hours                                                          mycentos0152
1ae5e7e69adb   redis:latest   "docker-entrypoint.s…"   3 weeks ago   Up 5 hours   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp              c_redis
3a796708dfa9   mysql:8.0.17   "docker-entrypoint.s…"   3 weeks ago   Up 5 hours   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   c_mysql

#强制关闭
[root@Dragon ~]# dk kill mycentos0152
mycentos0152
[root@Dragon ~]# dk ps
CONTAINER ID   IMAGE          COMMAND                  CREATED       STATUS       PORTS                                                  NAMES
1ae5e7e69adb   redis:latest   "docker-entrypoint.s…"   3 weeks ago   Up 5 hours   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp              c_redis
3a796708dfa9   mysql:8.0.17   "docker-entrypoint.s…"   3 weeks ago   Up 5 hours   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   c_mysql

删除已关闭的容器

[root@Dragon ~]# dk rm mycentos0152
mycentos0152
[root@Dragon ~]# dk ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED       STATUS                     PORTS                                                  NAMES
5a0b6a6c5e06   5d0da3dc9764   "/bin/bash"              3 hours ago   Exited (0) 3 minutes ago                                                          mycentos
1ae5e7e69adb   redis:latest   "docker-entrypoint.s…"   3 weeks ago   Up 5 hours                 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp              c_redis
3a796708dfa9   mysql:8.0.17   "docker-entrypoint.s…"   3 weeks ago   Up 5 hours                 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   c_mysql

#强制删除
dk rm -f contanername
#批量删除
dk rm -f $(dk ps -a -q)
#xargs 前一个命令的结果集作为参数传递给下一个命令
dk ps -a -q | xargs dk rm

守护式启动

#直接后台启动容器,没有前台进程容器会立刻“自杀”
[root@Dragon ~]# dk run -d centos
88aa1dabfe357a623569f60293e44384fd0aa4ac3c75f1db968f907c10b865d6
[root@Dragon ~]# dk ps
CONTAINER ID   IMAGE          COMMAND                  CREATED       STATUS       PORTS                                                  NAMES
1ae5e7e69adb   redis:latest   "docker-entrypoint.s…"   3 weeks ago   Up 5 hours   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp              c_redis
3a796708dfa9   mysql:8.0.17   "docker-entrypoint.s…"   3 weeks ago   Up 5 hours   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   c_mysql

#添加前台进程,循环打印hello Zzzz
[root@Dragon ~]# dk run -d centos /bin/sh -c "while true;do echo hello Zzzz;sleep 2;done"
ed8556f67abe6009b96676c07a24f43133df5a6e1d43f6e138a4f5bb95f8e0df
[root@Dragon ~]# dk ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
ed8556f67abe   centos         "/bin/sh -c 'while t…"   21 seconds ago   Up 19 seconds                                                          goofy_ellis
1ae5e7e69adb   redis:latest   "docker-entrypoint.s…"   3 weeks ago      Up 6 hours      0.0.0.0:6379->6379/tcp, :::6379->6379/tcp              c_redis
3a796708dfa9   mysql:8.0.17   "docker-entrypoint.s…"   3 weeks ago      Up 6 hours      0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   c_mysql

#查看容器日志 -t 加上时间戳 -f 持续看
[root@Dragon ~]# dk logs -t ed8556f67abe
2021-10-15T07:02:21.415960686Z hello Zzzz
2021-10-15T07:02:23.439496483Z hello Zzzz
2021-10-15T07:02:25.452270883Z hello Zzzz
2021-10-15T07:02:27.460080942Z hello Zzzz

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值