Docker基本命令

  • 第一次使用docker,从helle world开始
    • docker run hello-world
    • 镜像的完整写法:[仓库地址/]镜像名[:版本号]

--help 查看帮助

  • 查看镜像
    • docker image ls 或 docker images
  • 查找镜像
    • docker search nginx
  • 拉取镜像
    • docker pull nginx
  • 查看镜像历史信息
    • docker image history nginx
  • 查看镜像详细信息
    • docker image inspect nginx
  • 删除无用镜像
    • docker image prune
  • 移除镜像
    • docker image rm nginx
    • docker rmi feb5d9fea6a5
  • 打包镜像
    • docker image save [OPTIONS] IMAGE [IMAGE...]
    • docker image save hello-world -o hello.tar
  • 加载镜像包
    • docker image load [OPTIONS]
    • docker image load -i "hello.tar"
  • 给镜像打标签
    • docker image tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
    • docker image tag hello-world:latest hello-world:1.0
  • 查看容器
    • docker ps 列出正常运行的容器
    • docker ps -a 参数-a列出所有的容器
  • 移除容器
    • docker rm 2276a5ecfaca
    • -f参数 强制删除
  • 运行容器
    • docker run nginx
    • docker run --name my_nginx nginx 运行一个名为my_nginx的nginx镜像
    • -it 交互 -d 后台运行 --rm 容器挂掉自动移除
docker exec --help

Usage:  docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Run a command in a running container

Options:
  -d, --detach               Detached mode: run command in the background
      --detach-keys string   Override the key sequence for detaching a container
  -e, --env list             Set environment variables
      --env-file list        Read in a file of environment variables
  -i, --interactive          Keep STDIN open even if not attached
      --privileged           Give extended privileges to the command
  -t, --tty                  Allocate a pseudo-TTY
  -u, --user string          Username or UID (format: <name|uid>[:<group|gid>])
  -w, --workdir string       Working directory inside the container
  • 进入容器
    • docker exec -it nginxtest bash
    • nginxtest 容器名称或容器ID
    • exit 退出容器
docker commit --help

Usage:  docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

Options:
  -a, --author string    Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  -c, --change list      Apply Dockerfile instruction to the created image
  -m, --message string   Commit message
  -p, --pause            Pause container during commit (default true)
  • 通过容器生成一个新的镜像
  • docker commit -m 'this is test' mynginx my_nginx
# docker push --help

Usage:  docker push [OPTIONS] NAME[:TAG]

Push an image or a repository to a registry

Options:
  -a, --all-tags                Push all tagged images in the repository
      --disable-content-trust   Skip image signing (default true)
  -q, --quiet                   Suppress verbose output

将镜像推送到远程仓库
  • 首先需要一个带仓库地址的镜像
    • docker commit -m 'this is test' mynginx repositoryname/my_nginx:1.0
  • 然后登录docker.hub账号
    • docker login
  • 最后就可以推送到自己的docker.hub远程仓库
    • docker image push repositoryname/my_nginx:1.0

端口映射

  • docker run --name my_nginx --rm -p 8080:80 nginx
    • -p 8080:80 端口映射 8080-宿主机端口 80-容器端端口 nginx-运行的镜像 --name 给容器起名 my_nginx 容器名称
    • 这样我们就可以在外部使用 ip+端口访问 容器my_nginx

数据卷挂载

  • docker run --name my_nginx --rm -d -v /root/test1:/root/test1 nginx
    • 将目录test1文件夹挂载到容器的test1文件夹下 这样即使容器被销毁了容器里的文件也会保存在我们的宿主机上
//使用exec命令进入容器
[root@iZuf620p8rsr3faul3zsx6Z ~]# docker exec -it my_nginx  bash
root@a87e160ee875:/# ls
bin   dev		   docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
boot  docker-entrypoint.d  etc			 lib   media  opt  root  sbin  sys  usr
root@a87e160ee875:/# cd /root/
root@a87e160ee875:~# ls
test1
root@a87e160ee875:~# cd tst1
bash: cd: tst1: No such file or directory
root@a87e160ee875:~# cd test1
//进入test1 文件夹我们可以看到test1文件夹里的内容和宿主机test1文件夹的内容是一样的
root@a87e160ee875:~/test1# ls
1.txt  2.txt  b.txt
//在容器种创建一个新的文件
root@a87e160ee875:~/test1# touch a.txt
root@a87e160ee875:~/test1# echo aaaaawqewqewq > a.txt
root@a87e160ee875:~/test1# ls
1.txt  2.txt  a.txt  b.txt
root@a87e160ee875:~/test1# cat a.txt
aaaaawqewqewq
root@a87e160ee875:~/test1# exit
exit
[root@iZuf620p8rsr3faul3zsx6Z ~]# cd test1
//退出容器 在宿主机中可以看到一样的文件 内容也是一样的
[root@iZuf620p8rsr3faul3zsx6Z test1]# ls
1.txt  2.txt  a.txt  b.txt
[root@iZuf620p8rsr3faul3zsx6Z test1]# cat a.txt
aaaaawqewqewq
posted @ 2022-03-20 18:35  德乌姆列特  阅读(55)  评论(0编辑  收藏  举报