欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

Linux中Docker安装后如何使用Docker容器

程序员文章站 2023-11-01 09:08:46
Docker的安装在之前的文章中有介绍过,不少朋友在安装后不知要如何使用Docker容器,下面以图文的形式为大家介绍下如何使用... 15-01-19...

  在之前的文章中我们有介绍过docker的安装,不少朋友在安装后不知要如何使用docker容器,下面小编就给大家介绍下linux如何使用docker容器。

Linux中Docker安装后如何使用Docker容器

  1.启动一个docker容器

  [root@localhost ~]# docker run -i -t ubuntu /bin/bash

  unable to find image ‘ubuntu’ locally

  pulling repository ubuntu

  04c5d3b7b065: download complete

  511136ea3c5a: download complete

  c7b7c6419568: download complete

  70c8faa62a44: download complete

  d735006ad9c1: download complete

  status: downloaded newer image for ubuntu:latest

  注:

  -i:打开容器中的stdin

  -t:为容器分配一个伪tty终端

  从上面我们可以看出,首先docker会检查本地是否存在ubuntu镜像,如果在本地没有找到该镜像的话,那么docker就会去官方的docker hub registry查看docker hub中是否有该镜像。docker一旦找到该镜像,就会下载该镜像并将其保存到本地的宿主机中。

  然后,docker在文件系统内部用这个镜像创建了一个新的容器。该容器拥有自己的网络、ip地址,以及一个用来可以和宿主机进行通信的桥接网络接口。最后,我们告诉docker在新容器中要运行什么命令。

  当容器创建完毕之后,docker就会执行容器中的/bin/bash命令。这时间我们就可以看到容器内的shell

  root@8c342c0c275c:/#

  注:8c342c0c275c代表容器的id

  2.使用容器

  查看该容器的主机名

  root@8c342c0c275c:/# hostname

  8c342c0c275c

  可以看到,容器的主机名就是该容器的id

  查看该主机的hosts文件

  root@8c342c0c275c:/# cat /etc/hosts

  172.17.0.2 8c342c0c275c

  ff00::0 ip6-mcastprefix

  ff02::1 ip6-allnodes

  ff02::2 ip6-allrouters

  127.0.0.1 localhost

  ::1 localhost ip6-localhost ip6-loopback

  fe00::0 ip6-localnet

  可以看到docker为该容器的ip地址添加了一条主机配置项。

  查看该容器的ip地址

  root@8c342c0c275c:/# ip a

  1: lo: 《loopback,up,lower_up》 mtu 65536 qdisc noqueue state unknown group default

  link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

  inet 127.0.0.1/8 scope host lo

  valid_lft forever preferred_lft forever

  inet6 ::1/128 scope host

  valid_lft forever preferred_lft forever

  4: eth0: 《broadcast,up,lower_up》 mtu 1500 qdisc pfifo_fast state up group default qlen 1000

  link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff

  inet 172.17.0.2/16 scope global eth0

  valid_lft forever preferred_lft forever

  inet6 fe80::42:acff:fe11:2/64 scope link

  valid_lft forever preferred_lft forever

  查看容器中运行的进程

  root@8c342c0c275c:/# ps -aux

  user pid %cpu %mem vsz rss tty stat start time command

  root 1 0.0 0.1 18168 1976 ? ss 09:38 0:00 /bin/bash

  root 19 0.0 0.0 15568 1144 ? r+ 09:55 0:00 ps -aux

  在容器中安装一个软件包

  root@8c342c0c275c:/# apt-get update && apt-get install vim

  ign http://archive.ubuntu.com trusty inrelease

  ign http://archive.ubuntu.com trusty-updates inrelease

  ign http://archive.ubuntu.com trusty-security inrelease

  hit http://archive.ubuntu.com trusty release.gpg

  get:1 http://archive.ubuntu.com trusty-updates release.gpg [933 b]

  get:2 http://archive.ubuntu.com trusty-security release.gpg [933 b]

  hit http://archive.ubuntu.com trusty release

  get:3 http://archive.ubuntu.com trusty-updates release [62.0 kb]

  get:4 http://archive.ubuntu.com trusty-security release [62.0 kb]

  get:5 http://archive.ubuntu.com trusty/main sources [1335 kb]

  get:6 http://archive.ubuntu.com trusty/restricted sources [5335 b]

  get:7 http://archive.ubuntu.com trusty/universe sources [7926 kb]

  get:8 http://archive.ubuntu.com trusty/main amd64 packages [1743 kb]

  get:9 http://archive.ubuntu.com trusty/restricted amd64 packages [16.0 kb]

  get:10 http://archive.ubuntu.com trusty/universe amd64 packages [7589 kb]

  get:11 http://archive.ubuntu.com trusty-updates/main sources [193 kb]

  get:12 http://archive.ubuntu.com trusty-updates/restricted sources [1874 b]

  get:13 http://archive.ubuntu.com trusty-updates/universe sources [119 kb]

  get:14 http://archive.ubuntu.com trusty-updates/main amd64 packages [493 kb]

  get:15 http://archive.ubuntu.com trusty-updates/restricted amd64 packages [14.8 kb]

  get:16 http://archive.ubuntu.com trusty-updates/universe amd64 packages [298 kb]

  get:17 http://archive.ubuntu.com trusty-security/main sources [70.1 kb]

  get:18 http://archive.ubuntu.com trusty-security/restricted sources [1874 b]

  get:19 http://archive.ubuntu.com trusty-security/universe sources [19.1 kb]

  get:20 http://archive.ubuntu.com trusty-security/main amd64 packages [229 kb]

  get:21 http://archive.ubuntu.com trusty-security/restricted amd64 packages [14.8 kb]

  get:22 http://archive.ubuntu.com trusty-security/universe amd64 packages [98.1 kb]

  fetched 20.3 mb in 41s (490 kb/s)

  reading package lists.。。 done

  reading package lists.。。 done

  building dependency tree

  reading state information.。。 done

  the following extra packages will be installed:

  libgpm2 libpython2.7 libpython2.7-minimal libpython2.7-stdlib vim-runtime

  suggested packages:

  gpm ctags vim-doc vim-scripts

  the following new packages will be installed:

  libgpm2 libpython2.7 libpython2.7-minimal libpython2.7-stdlib vim

  vim-runtime

  0 upgraded, 6 newly installed, 0 to remove and 2 not upgraded.

  need to get 9083 kb of archives.

  after this operation, 42.9 mb of additional disk space will be used.

  do you want to continue? [y/n] y

  get:1 http://archive.ubuntu.com/ubuntu/ trusty/main libgpm2 amd64 1.20.4-6.1 [16.5 kb]

  get:2 http://archive.ubuntu.com/ubuntu/ trusty/main libpython2.7-minimal amd64 2.7.6-8 [307 kb]

  get:3 http://archive.ubuntu.com/ubuntu/ trusty/main libpython2.7-stdlib amd64 2.7.6-8 [1872 kb]

  get:4 http://archive.ubuntu.com/ubuntu/ trusty/main libpython2.7 amd64 2.7.6-8 [1044 kb]

  get:5 http://archive.ubuntu.com/ubuntu/ trusty/main vim-runtime all 2:7.4.052-1ubuntu3 [4888 kb]

  get:6 http://archive.ubuntu.com/ubuntu/ trusty/main vim amd64 2:7.4.052-1ubuntu3 [956 kb]

  fetched 9083 kb in 51s (175 kb/s)

  selecting previously unselected package libgpm2:amd64.

  (reading database 。。。 11527 files and directories currently installed.)

  preparing to unpack 。。。/libgpm2_1.20.4-6.1_amd64.deb 。。。

  unpacking libgpm2:amd64 (1.20.4-6.1) 。。。

  selecting previously unselected package libpython2.7-minimal:amd64.

  preparing to unpack 。。。/libpython2.7-minimal_2.7.6-8_amd64.deb 。。。

  unpacking libpython2.7-minimal:amd64 (2.7.6-8) 。。。

  selecting previously unselected package libpython2.7-stdlib:amd64.

  preparing to unpack 。。。/libpython2.7-stdlib_2.7.6-8_amd64.deb 。。。

  unpacking libpython2.7-stdlib:amd64 (2.7.6-8) 。。。

  selecting previously unselected package libpython2.7:amd64.

  preparing to unpack 。。。/libpython2.7_2.7.6-8_amd64.deb 。。。

  unpacking libpython2.7:amd64 (2.7.6-8) 。。。

  selecting previously unselected package vim-runtime.

  preparing to unpack 。。。/vim-runtime_2%3a7.4.052-1ubuntu3_all.deb 。。。

  adding ‘diversion of /usr/share/vim/vim74/doc/help.txt to /usr/share/vim/vim74/doc/help.txt.vim-tiny by vim-runtime’

  adding ‘diversion of /usr/share/vim/vim74/doc/tags to /usr/share/vim/vim74/doc/tags.vim-tiny by vim-runtime’

  unpacking vim-runtime (2:7.4.052-1ubuntu3) 。。。

  selecting previously unselected package vim.

  preparing to unpack 。。。/vim_2%3a7.4.052-1ubuntu3_amd64.deb 。。。

  unpacking vim (2:7.4.052-1ubuntu3) 。。。

  setting up libgpm2:amd64 (1.20.4-6.1) 。。。

  setting up libpython2.7-minimal:amd64 (2.7.6-8) 。。。

  setting up libpython2.7-stdlib:amd64 (2.7.6-8) 。。。

  setting up libpython2.7:amd64 (2.7.6-8) 。。。

  setting up vim-runtime (2:7.4.052-1ubuntu3) 。。。

  processing /usr/share/vim/addons/doc

  setting up vim (2:7.4.052-1ubuntu3) 。。。

  update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in auto mode

  update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vimdiff (vimdiff) in auto mode

  update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rvim (rvim) in auto mode

  update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rview (rview) in auto mode

  update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vi (vi) in auto mode

  update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/view (view) in auto mode

  update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/ex (ex) in auto mode

  update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in auto mode

  processing triggers for libc-bin (2.19-0ubuntu6.4) 。。。

  退出容器

  root@8c342c0c275c:/# exit

  exit

  当我们输入exit后,容器就停止工作了。只有在指定的/bin/bash命令处于运行状态的时间,容器才会相应地处于运行状态。一旦退出容器,/bin/bash命令也就结束了,这时容器也就停止了。

  查看系统中容器的列表

  [root@localhost ~]# docker ps -a

  container id image command created status ports names

  8c342c0c275c ubuntu:latest “/bin/bash” 26 minutes ago exited (0) 5 minutes ago sharp_bohr

  8c342c0c275c:代表容器的id,

  ubuntu:latest :创建容器的镜像

  “/bin/bash”  :容器最后执行的命令

  26 minutes ago :创建时间

  exited (0) 5 minutes :容器退出的状态

  sharp_bohr     :容器的名称

  docker中容器的命名

  docker在创建容器时会自动为容器生成一个随机的名称。那么如果我们想在创建一个容器时指定该容器的名称可以使用如下命令:

  [root@localhost ~]# docker run --name ovcer_the_container -i -t ubuntu /bin/bash

  root@1ce9f640478d:/#

  上面的命令将会创建一个名为ovcer_the_container的容器。对于一个合法的容器的名称来说只可以包括以下字符:小写字母a~z、大写字母a-z、数字0~9、下划线、圆点、横线。

  上面就是linux使用docker容器的方法介绍了,在使用命令创建docker容器后,就可以使用命令对docker容器进行命名了。