Docker 浅识

Docker Hello word

1.1 使用docker run 启动一个容器

实验使用vmware cnetos7.6版本虚拟机
docker 安装参考 docker+docker-compose脚本安装

[root@localhost /]#  docker run centos /bin/echo "Hello world"
Hello world

docker : Dcoker 的二进文件
run : 表示启动一个容器
centos: 容器镜像名字,如果不存在会自己去pull下载,不写版本号默认下载最新版本
/bin/echo “Hello world” : 启动容器执行命令输出Hello world

1.2 交互式使用容器

#查找镜像
[root@localhost /]# docker search centos
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                            The official build of CentOS.                   7085      [OK]       
centos/systemd                    systemd enabled base container.                 107                  [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   92                   
centos/postgresql-96-centos7      PostgreSQL is an advanced Object-Relational …   45                   
centos/httpd-24-centos7           Platform for running Apache httpd 2.4 or bui…   43                   
centos/python-35-centos7          Platform for building and running Python 3.539                   
centos/php-56-centos7             Platform for building and running PHP 5.6 ap…   34                   
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   22                   
centos/postgresql-10-centos7      PostgreSQL is an advanced Object-Relational …   19                   
kasmweb/centos-7-desktop          CentOS 7 desktop for Kasm Workspaces            18                   
centos/nginx-112-centos7          Platform for running nginx 1.12 or building …   16     

[root@localhost /]#  docker run -i -t centos /bin/bash
root@0123ce188bd8:/#

参数详解
search : 查找镜像
-i :在新容器启动一个伪终端
-t :允许你对容器的标准输入(STDIN)进行交互
-d :后台运行
当你退出容器之后这个容器就会死掉,需要加-d 参数

1.3 启动一个centos容器

[root@localhost ~]# docker run -it -d --name test centos bash
c2b7517b76811aba072fcf12ede67d9b3502683d9eeeae63d39888ef63e1e96c
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
c2b7517b7681   centos         "bash"                   5 seconds ago    Up 3 seconds                                                           test

参数详解
ps : 查看运行中容器。
–name:指定容器名字。
bash:启动容器后运行bash。
images:查看所有镜像。
CONTAINER ID: 容器 ID。
IMAGE:使用的镜像。
COMMAND: 启动容器时运行的命令。
CREATED: 容器的创建时间。
STATUS: 容器状态。

1.4 进入容器

[root@localhost ~]# docker exec -it test /bin/bash
[root@c2b7517b7681 /]# 
#退出容器
[root@c2b7517b7681 /]# exit
exit
[root@localhost ~]#

1.5删除容器

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS         PORTS     NAMES
c2b7517b7681   centos    "bash"    6 minutes ago   Up 6 minutes             test
[root@localhost ~]# docker rm c2b7517b7681
Error response from daemon: You cannot remove a running container c2b7517b76811aba072fcf12ede67d9b3502683d9eeeae63d39888ef63e1e96c. Stop the container before attempting removal or force remove
# 直接删除他会报错,因为容器还在运行
[root@localhost ~]# docker rm -f c2b7517b7681
c2b7517b7681
#可以加-f 参数强制删除运行中的容器
#不建议加-f参数删除容器,可以先docker stop 容器名/ID

1.6 删除镜像

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
centos       latest    5d0da3dc9764   6 months ago   231MB
[root@localhost ~]# docker rmi centos
Error response from daemon: conflict: unable to remove repository reference "centos" (must force) - container abf8870f4441 is using its referenced image 5d0da3dc9764

#因为还有在使用镜像的容器,需要删除之后才能删除镜像文件

[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
abf8870f4441   centos         "/bin/bash"              14 minutes ago   Exited (0) 6 minutes ago             bold_rhodes
[root@localhost ~]# docker rm abf
abf
[root@localhost ~]# docker rmi centos
Untagged: centos:latest
Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59
[root@localhost ~]# 

命令详解
rm :删除容器
rmi:删除镜像
ps -a :查看所有状态的容器

容器状态有7种:
created(已创建)
restarting(重启中)
running 或 Up(运行中)
removing(迁移中)
paused(暂停)
exited(停止)
dead(死亡
查看帮助
docker --help

[root@localhost ~]# docker --help

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*     Docker Buildx (Docker Inc., v0.8.0-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.17.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.

To get more help with docker, check out our guides at https://docs.docker.com/go/guides/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值