Docker基本操作

Docker基本操作

Docker是一個輕量級的基於進程的虛擬化解決方案。簡單來說,Docker是將應用程序和運行環境一起打包成一個容器,進而可以發佈到其他平臺上運行。

環境:Ubuntu 18.04.1 LTS

安裝Docker

root@jaking-virtual-machine:~# apt-get install -y docker-engineReading package lists... Done Building dependency tree Reading state information... Done ... root@jaking-virtual-machine:~# docker versionClient:Version: 18.06.1-ce API version: 1.38 Go version: go1.10.4 Git commit: e68fc7a Built: Fri Oct 1919:43:142018 OS/Arch:linux/amd64 Experimental: falseServer:Engine: Version: 18.06.1-ce API version: 1.38 (minimum version 1.12) Go version: go1.10.4 Git commit: e68fc7a Built: Thu Sep 2702:39:502018 OS/Arch: linux/amd64 Experimental: falseroot@jaking-virtual-machine:~# systemctl start dockerroot@jaking-virtual-machine:~# systemctl enable dockerSynchronizing state of docker.service with SysV service>

搜索ubuntu相關的容器

root@jaking-virtual-machine:~# docker search ubuntuNAMEDESCRIPTION STARS OFFICIAL AUTOMATED ubuntu Ubuntu is a Debian-based Linux operating sys… 8838 [OK] dorowu/ubuntu-desktop-lxde-vnc Ubuntu with openssh-server and NoVNC 247 [OK] rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 184 [OK] consol/ubuntu-xfce-vnc Ubuntu container with"headless" VNC session… 136 [OK] ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 95 [OK] ubuntu-upstart Upstart is an event-based replacement for th… 92 [OK]

下載容器

root@jaking-virtual-machine:~# docker pull ubuntu-upstart Using default tag: latest latest: Pulling from library/ubuntu-upstart8387d9ff0016: Pull complete3b52deaaf0ed: Pull complete4bd501fad6de: Pull complete a3ed95caeb02: Pull complete a6dc1658c730: Pull complete9ed623dca71b: Pull complete998ee72febf9: Pull complete437038dc2fba: Pull complete da0ee05a1a1d: Pull complete1e1c3e99deb1: Pull complete4fcc22d7b2a1: Pull complete6c7dda5571e4: Pull complete Digest:sha256:597dfb1868012dcd04a705572dbc1542cb7598bce0eaa1c2656eb3acfc8b51d2 Status: Downloaded newer image for ubuntu-upstart:latest

查看容器的鏡像

root@jaking-virtual-machine:~# docker images ubuntu-upstartREPOSITORY TAG IMAGE ID CREATED SIZE ubuntu-upstart latest b28219773b9b 2 years ago 253MB

從上面的結果可以看到,容器已經成功下載。利用下載的ubuntu-upstart容器,可以運行一個簡單的程序,此處以“Hello Docker”為例:

root@jaking-virtual-machine:~# docker run ubuntu-upstart /bin/echo Hello DockerHello Docker

還可以使用其他容器,如使用ubuntu作為容器,下載操作如下:

root@jaking-virtual-machine:~# docker pull ubuntu Using default tag: latest latest: Pulling from library/ubuntu 32802c0cfa4d: Pull complete da1315cffa03: Pull complete fa83472a3562: Pull complete f85999a86bef: Pull complete Digest: sha256:6d0e0c26489e33f5a6f0020edface2727db9489744ecc9b4f50c7fa671f23c49 Status: Downloaded newer image for ubuntu:latest root@jaking-virtual-machine:~# docker images ubuntu REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 93fd78260bd1 10 days ago 86.2MB

Docker的容器操作

當Docker中包含了容器,就如同虛擬機中安裝了操作系統一樣,可以運行、安裝軟件、做一些設置。現在就可以運行之前下載的ubuntu:

root@jaking-virtual-machine:~# docker run -i -t ubuntu /bin/bash

#運行一個名為ubuntu的容器#i選項表示捕獲標準輸入和輸出;t選項表示分配的終端和控制檯root@05559b460591:/# root@05559b460591:/# uname -aLinux 05559b460591 4.15.0-36-generic #39-Ubuntu SMP Mon Sep 24 16:19:09 UTC 2018 x86_64 x86_64 x86_64 GNU/Linuxroot@05559b460591:/# exit#退出容器exit

root@jaking-virtual-machine:~# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

從上面的命令可以看到,使用run命令運行了一個容器,但退出之後容器也關閉了,這不是想要的結果。這時可以使用選項d讓容器一直在後臺運行:

root@jaking-virtual-machine:~# docker run -d -i -t ubuntu /bin/bash b19cc95aef9cb6f402062915b527864cf045debc65dbabd23a495cea32a138dd root:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b19cc95aef9c ubuntu "/bin/bash" 15 seconds ago Up 14 seconds kind_johnson05559b460591"/bin/bash" 35 minutes ago Exited (0) 9 minutes ago xenodochial_hypatia5bc78fd29b2a ubuntu-upstart "/bin/echo Hello Doc…" 420) 42 minutes ago silly_jenningsc54bb6d664b7 ubuntu-upstart "/bin/echo Hello Doc…" 440) 44 minutes ago jolly_thompson

從上面的命令輸出可以看到一個ID為b19cc95aef9c的容器正在運行,這個ID號就是操作此容器的重要參數。

容器運行在後臺時,可以使用attach登錄正在運行的容器:

root@jaking-virtual-machine:~# docker attach b19cc95aef9croot@b19cc95aef9c:/# exit exit root:~#

容器的操作還有很多命令,常用的操作還有:

docker cp :將容器中的文件複製到主機上docker rm:刪除一個容器docker port:配置容器的端口轉發docker start:啟動一個容器docker stop:停止一個容器docker top:顯示容器中的進程docker ps:列出容器docker logs:獲取容器的日誌除了以上這些操作外,Docker還有許多操作,可自行閱讀相關文檔瞭解。

讓您學習到的每一節課都有所收穫


分享到:


相關文章: