docker基本操作常用命令

1、镜像是Docker运行容器的前提。
首先下载一个hello world镜像。下载前可以先搜索一下。

docker search hello-world 
docker pull hello-world

2、下载后可以使用如下命令查看:

[root@localhost ~]# docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
hello-world                  latest              fce289e99eb9        13 months ago       1.84kB

在该系统中存在一个镜像:

  • REPOSITRY:镜像的名称
  • TAG:镜像的版本
  • IMAGEID:镜像截断后的ID
  • CREATED:镜像什么时候创建的
  • SIZE:镜像所占的空间的大小

3、创建一个容器,运行上面的镜像:


4、查看docker基本信息

5、下载一个ubuntu的镜像

docker pull ubuntu

6、上面的hello-world运行完毕后就退出了。再创建一个含shell的容器。

[root@localhost ~]# docker run -it --name ubuntu_test ubuntu:latest /bin/bash
root@e18470e1bcce:/#

  -i:表示启动一个可交互的容器
  -t:表示使用打开这个容器
  --name:给该容器起一个别名
  ubuntu:latest:使用的镜像名,默认使用latest
  /bin/bash:表示启动容器时,要运行的命令
7、查看当前运行的容器

docker ps

8、运行一个长时间的容器。

# docker run -d ubuntu /bin/sh -c "while true;do echo Hello world;sleep1;done"

-d:表示让容器在后台运行,不关联到当前终端的shell.
使用docker logs Container_ID #容器ID可以通过docker ps 查看查看容器的日志。
9、操作容器,启动、停止、重启、强行停止和删除

docker start Container_ID
docker stop Container_ID
docker restart Container_ID
docker kill Container_ID
docker rm Container-ID
docker rm -f Container_ID

10、查看运行的所有容器

docker ps -a

11、删除所有的容器

docker rm -f `docerk ps -a -q`

12、持久化容器,容器在退出之后一般不会更改镜像,当希望保存容器中的数据,就需要容器的commit操作

[root@localhost ~]# docker run -it --name ubuntu_test ubuntu:latest /bin/bash
root@e18470e1bcce:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@e18470e1bcce:/# mkdir /data
root@e18470e1bcce:/# touch /data/file{1..9}.txt
root@e18470e1bcce:/# cd /data
root@e18470e1bcce:/data# ls
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt  file6.txt  file7.txt  file8.txt  file9.txt
root@e18470e1bcce:/data# echo "1111" > file1.txt
root@e18470e1bcce:/data# cat file1.txt
1111
root@e18470e1bcce:/data# exit
exit
[root@localhost ~]#

在上述容器创建了data目录以及9个文件。
容器退出后一般不会更改,所以导出该镜像:

[root@localhost ~]# docker commit e18470e1bcce  my_ubuntu
sha256:951280ad074338407728470eba07e55e7b5bfe25c9c1c6875e4760d8667b04af

docker images下面就可以看到my_ubuntu的镜像。
再次运行:

[root@localhost ~]# docker run -it --rm my_ubuntu /bin/bash
root@63b2b330e718:/# ls /data/    
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt  file6.txt  file7.txt  file8.txt  file9.txt

13、获取镜像的三种方式:
   1. 通过pull拉取
   2. 通过commit得到
   3. 通过Dockfile生成
14、容器的导入导出
导出:

docker export -o ubuntu1.tar e18470e1bcce
或
docker export e18470e1bcce > ubuntu1.tar

导入:

cat ubuntu1.tar | docker import  - test/ubuntu:v3.0

15、容器的标识:
   1、–name:
       长UUID、短UUID、Name
    2、image[:tag]
16、特权模式

默认情况下,容器运行在非特权模式下,这样容器内不能访问任何宿主机的设备,但是有时用户可能会需要访问某些设备,就必须使用–privileged 参数。如果你希望使用某个特定设备可以使用–device,比如你想使用 GPU 时。
17、删除镜像

docker rmi -f Image_
发布了36 篇原创文章 · 获赞 3 · 访问量 8015

猜你喜欢

转载自blog.csdn.net/qq_41547105/article/details/104390197