您好,登錄后才能下訂單哦!
這篇文章主要講解了“Docker的基本操作方法有哪些”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“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 19 19:43:14 2018 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 27 02:39:50 2018 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 script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable docker
搜索ubuntu相關的容器
root@jaking-virtual-machine:~# docker search ubuntuNAME DESCRIPTION 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-upstartUsing default tag: latest latest: Pulling from library/ubuntu-upstart 8387d9ff0016: Pull complete 3b52deaaf0ed: Pull complete 4bd501fad6de: Pull complete a3ed95caeb02: Pull complete a6dc1658c730: Pull complete 9ed623dca71b: Pull complete 998ee72febf9: Pull complete 437038dc2fba: Pull complete da0ee05a1a1d: Pull complete 1e1c3e99deb1: Pull complete 4fcc22d7b2a1: Pull complete 6c7dda5571e4: 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 ubuntuUsing 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 ubuntuREPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 93fd78260bd1 10 days ago 86.2MB
當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#退出容器exitroot@jaking-virtual-machine:~# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
命令可以看到使用run命令運行了一個容器但退出之后容器也關閉了這不是想要的結果-這時可以使用選項d讓容器一直在后臺運行” data-source-line=”114″>從上面的命令可以看到,使用run命令運行了一個容器,但退出之后容器也關閉了,這不是想要的結果。這時可以使用選項d讓容器一直在后臺運行:
root@jaking-virtual-machine:~# docker run -d -i -t ubuntu /bin/bashb19cc95aef9cb6f402062915b527864cf045debc65dbabd23a495cea32a138dd root@jaking-virtual-machine:~# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b19cc95aef9c ubuntu "/bin/bash" 15 seconds ago Up 14 seconds kind_johnson 05559b460591 ubuntu "/bin/bash" 35 minutes ago Exited (0) 9 minutes ago xenodochial_hypatia 5bc78fd29b2a ubuntu-upstart "/bin/echo Hello Doc…" 42 minutes ago Exited (0) 42 minutes ago silly_jennings c54bb6d664b7 ubuntu-upstart "/bin/echo Hello Doc…" 44 minutes ago Exited (0) 44 minutes ago jolly_thompson
從上面的命令輸出可以看到一個ID為b19cc95aef9c的容器正在運行,這個ID號就是操作此容器的重要參數。 容器運行在后臺時,可以使用attach登錄正在運行的容器:
root@jaking-virtual-machine:~# docker attach b19cc95aef9croot@b19cc95aef9c:/# exitexitroot@jaking-virtual-machine:~#
容器的操作還有很多命令,常用的操作還有:
docker cp :將容器中的文件復制到主機上 docker rm:刪除一個容器 docker port:配置容器的端口轉發 docker start:啟動一個容器 docker stop:停止一個容器 docker top:顯示容器中的進程 docker ps:列出容器 docker logs:獲取容器的日志 除了以上這些操作外,Docker還有許多操作,可自行閱讀相關文檔了解。
感謝各位的閱讀,以上就是“Docker的基本操作方法有哪些”的內容了,經過本文的學習后,相信大家對Docker的基本操作方法有哪些這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。