主頁 > 知識庫 > Linux中Docker安裝后如何使用Docker容器

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

熱門標(biāo)簽:河南智能電話機器人公司 怎樣給景區(qū)加百度地圖標(biāo)注 電話機器人哪里有賣的 地球地圖標(biāo)注方法 商戶地圖標(biāo)注 壽光百度地圖標(biāo)注中心網(wǎng)站 河北語音電銷機器人 樺甸電銷機器人 新鄭電銷外呼系統(tǒng)線路

  在之前的文章中我們有介紹過Docker的安裝,不少朋友在安裝后不知要如何使用Docker容器,下面小編就給大家介紹下Linux如何使用Docker容器。

  1.啟動一個Docker容器

 ?。踨oot@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在文件系統(tǒng)內(nèi)部用這個鏡像創(chuàng)建了一個新的容器。該容器擁有自己的網(wǎng)絡(luò)、IP地址,以及一個用來可以和宿主機進行通信的橋接網(wǎng)絡(luò)接口。最后,我們告訴Docker在新容器中要運行什么命令。

  當(dāng)容器創(chuàng)建完畢之后,Docker就會執(zhí)行容器中的/bin/bash命令。這時間我們就可以看到容器內(nèi)的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

 ?。海? 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.

 ?。≧eading 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

  當(dāng)我們輸入exit后,容器就停止工作了。只有在指定的/bin/bash命令處于運行狀態(tài)的時間,容器才會相應(yīng)地處于運行狀態(tài)。一旦退出容器,/bin/bash命令也就結(jié)束了,這時容器也就停止了。

  查看系統(tǒng)中容器的列表

 ?。踨oot@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 :創(chuàng)建容器的鏡像

  “/bin/bash” ?。喝萜髯詈髨?zhí)行的命令

  26 minutes ago :創(chuàng)建時間

  Exited (0) 5 minutes?。喝萜魍顺龅臓顟B(tài)

  sharp_bohr    ?。喝萜鞯拿Q

  Docker中容器的命名

  Docker在創(chuàng)建容器時會自動為容器生成一個隨機的名稱。那么如果我們想在創(chuàng)建一個容器時指定該容器的名稱可以使用如下命令:

 ?。踨oot@localhost ~]# docker run --name ovcer_the_container -i -t ubuntu /bin/bash

  root@1ce9f640478d:/#

  上面的命令將會創(chuàng)建一個名為ovcer_the_container的容器。對于一個合法的容器的名稱來說只可以包括以下字符:小寫字母a~z、大寫字母A-Z、數(shù)字0~9、下劃線、圓點、橫線。

  上面就是Linux使用Docker容器的方法介紹了,在使用命令創(chuàng)建Docker容器后,就可以使用命令對Docker容器進行命名了。

標(biāo)簽:咸陽 廣州 六安 懷化 阿壩 遵義 喀什 湖北

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Linux中Docker安裝后如何使用Docker容器》,本文關(guān)鍵詞  Linux,中,Docker,安裝,后,如何,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Linux中Docker安裝后如何使用Docker容器》相關(guān)的同類信息!
  • 本頁收集關(guān)于Linux中Docker安裝后如何使用Docker容器的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章