这篇文章主要讲解了“Docker的基本操作方法有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Docker的基本操作方法有哪些”吧!
安装Docker
root@jaking-virtual-machine:~# apt-get install -y docker-engineReading package lists... DoneBuilding dependency tree Reading state information... Done...root@jaking-virtual-machine:~# docker versionClient:Version: 18.06.1-ceAPI version: 1.38Go version: go1.10.4Git commit: e68fc7aBuilt: Fri Oct 19 19:43:14 2018OS/Arch: linux/amd64Experimental: 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 AUTOMATEDubuntu 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: latestlatest: Pulling from library/ubuntu-upstart8387d9ff0016: Pull complete3b52deaaf0ed: Pull complete4bd501fad6de: Pull completea3ed95caeb02: Pull completea6dc1658c730: Pull complete9ed623dca71b: Pull complete998ee72febf9: Pull complete437038dc2fba: Pull completeda0ee05a1a1d: Pull complete1e1c3e99deb1: Pull complete4fcc22d7b2a1: Pull complete6c7dda5571e4: Pull completeDigest: sha256:597dfb1868012dcd04a705572dbc1542cb7598bce0eaa1c2656eb3acfc8b51d2Status: Downloaded newer image for ubuntu-upstart:latest
查看容器的镜像
root@jaking-virtual-machine:~# docker images ubuntu-upstartREPOSITORY TAG IMAGE ID CREATED SIZEubuntu-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: latestlatest: Pulling from library/ubuntu32802c0cfa4d: Pull completeda1315cffa03: Pull completefa83472a3562: Pull completef85999a86bef: Pull completeDigest: sha256:6d0e0c26489e33f5a6f0020edface2727db9489744ecc9b4f50c7fa671f23c49Status: Downloaded newer image for ubuntu:latestroot@jaking-virtual-machine:~# docker images ubuntuREPOSITORY TAG IMAGE ID CREATED SIZEubuntu 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#退出容器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/bashb19cc95aef9cb6f402062915b527864cf045debc65dbabd23a495cea32a138ddroot@jaking-virtual-machine:~# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESb19cc95aef9c ubuntu "/bin/bash" 15 seconds ago Up 14 seconds kind_johnson05559b460591 ubuntu "/bin/bash" 35 minutes ago Exited (0) 9 minutes ago xenodochial_hypatia5bc78fd29b2a ubuntu-upstart "/bin/echo Hello Doc…" 42 minutes ago Exited (0) 42 minutes ago silly_jenningsc54bb6d664b7 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的基本操作方法有哪些这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!