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
</group|gid></name|uid>
  • 进入容器
  • 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)
</hannibal@a-team.com>
  • 通过容器生成一个新的镜像
  • 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文件夹下 这样即使容器被销毁了容器里的文件也会保存在我们的宿主机上
//&#x4F7F;&#x7528;exec&#x547D;&#x4EE4;&#x8FDB;&#x5165;&#x5BB9;&#x5668;
[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
//&#x8FDB;&#x5165;test1 &#x6587;&#x4EF6;&#x5939;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;test1&#x6587;&#x4EF6;&#x5939;&#x91CC;&#x7684;&#x5185;&#x5BB9;&#x548C;&#x5BBF;&#x4E3B;&#x673A;test1&#x6587;&#x4EF6;&#x5939;&#x7684;&#x5185;&#x5BB9;&#x662F;&#x4E00;&#x6837;&#x7684;
root@a87e160ee875:~/test1# ls
1.txt  2.txt  b.txt
//&#x5728;&#x5BB9;&#x5668;&#x79CD;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x6587;&#x4EF6;
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
//&#x9000;&#x51FA;&#x5BB9;&#x5668; &#x5728;&#x5BBF;&#x4E3B;&#x673A;&#x4E2D;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x4E00;&#x6837;&#x7684;&#x6587;&#x4EF6; &#x5185;&#x5BB9;&#x4E5F;&#x662F;&#x4E00;&#x6837;&#x7684;
[root@iZuf620p8rsr3faul3zsx6Z test1]# ls
1.txt  2.txt  a.txt  b.txt
[root@iZuf620p8rsr3faul3zsx6Z test1]# cat a.txt
aaaaawqewqewq

Original: https://www.cnblogs.com/Dewumu/p/16030921.html
Author: 德乌姆列特
Title: Docker基本命令

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/606542/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球