Docker学习笔记(二)——Hello world
注释:本篇文章为容器的一个普通运用例子。
1.从Docker镜像仓库下载镜像
查询centos镜像,命令:docker search centos
[root@izuf61gqesu0zgmx2ctlo6z /]# docker search centos NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 6258 [OK] ansible/centos7-ansible Ansible on Centos7 132 [OK] consol/centos-xfce-vnc Centos container with "headless" VNC session… 123 [OK] jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 115 [OK] centos/systemd systemd enabled base container. 86 [OK] centos/mysql-57-centos7 MySQL 5.7 SQL database server 84 imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 58 [OK] tutum/centos Simple CentOS docker image with SSH access 46 centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 46 kinogmt/centos-ssh CentOS with SSH 29 [OK] pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag names… 13 guyton/centos6 From official centos6 container with full up… 10 [OK] drecom/centos-ruby centos ruby 6 [OK] pivotaldata/centos Base centos, freshened up a little with a Do… 5 mamohr/centos-java Oracle Java 8 Docker image based on Centos 7 3 [OK] pivotaldata/centos-mingw Using the mingw toolchain to cross-compile t… 3 pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated wi… 3 darksheer/centos Base Centos Image -- Updated hourly 3 [OK] miko2u/centos6 CentOS6 日本??境 2 [OK] indigo/centos-maven Vanilla CentOS 7 with Oracle Java Developmen… 1 [OK] mcnaughton/centos-base centos base image 1 [OK] blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK] pivotaldata/centos7-dev CentosOS 7 image for GPDB development 0 pivotaldata/centos6.8-dev CentosOS 6.8 image for GPDB development 0 smartentry/centos centos with smartentry 0 [OK]
从镜像仓库拉取指定镜像,命令:docker pull centos
[root@izuf61gqesu0zgmx2ctlo6z /]# docker pull centos Using default tag: latest latest: Pulling from library/centos 3c72a8ed6814: Pull complete Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd Status: Downloaded newer image for centos:latest docker.io/library/centos:latest
查看本地镜像,命令:docker images
[root@izuf61gqesu0zgmx2ctlo6z /]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 0d120b6ccaa8 2 months ago 215MB
刚才拉取的镜像centos已经存在本地镜像里了。
2.镜像运行,容器创建
创建一个新的容器并运行一个命令,命令:docker run centos /bin/echo "Hello world"
[root@izuf61gqesu0zgmx2ctlo6z /]# docker run centos /bin/echo "Hello world" Hello world
各参数解析:
docker: Docker 的二进制执行文件。
run: 与前面的 docker 组合来运行一个容器。
centos 指定要运行的镜像,Docker 首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库 Docker Hub 下载公共镜像。
/bin/echo "Hello world": 在启动的容器里执行的命令 。
运行交互式的容器,通过 -i -t,让docker运行的容器实现“对话”功能:
[root@izuf61gqesu0zgmx2ctlo6z /]# docker run -i -t centos /bin/bash [root@f8bde570a6e0 /]#
各参数解析:
-t: 在新容器内指定一个伪终端或终端。
-i: 允许你对容器内的标准输入 (STDIN) 进行交互。
我们可以尝试在容器中运行命令,如查看当前操作系统版本信息:
[root@f8bde570a6e0 /]# cat /proc/version Linux version 3.10.0-514.26.2.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) ) #1 SMP Tue Jul 4 15:04:05 UTC 2017
可以通过exit命令或者CTRL+D快捷键来退出容器:
[root@f8bde570a6e0 /]# exit exit [root@izuf61gqesu0zgmx2ctlo6z /]#
3.容器启动(后台启动)
刚才运行镜像后,会生成一个容器,容器查看命令:docker ps -a
[root@izuf61gqesu0zgmx2ctlo6z /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f8bde570a6e0 centos "/bin/bash" 15 minutes ago Exited (0) 9 minutes ago infallible_carson c9a026afa9d7 centos "/bin/echo 'Hello wo…" 29 minutes ago Exited (0) 29 minutes ago naughty_vaughan
以上容器列表,两个容器都是我们刚才运行镜像产生的,输出详情:
CONTAINER ID:容器id
IMAGE:使用的镜像
COMMAND:启动容器时运行的命令
CREATED:容器的创建时间
STATUS:容器状态
状态有7种["created(已创建)", "restarting(重启中)", "running/Up(运行中)", "removing(迁移中)", "paused(暂停)", "exited(停止)", "dead(死亡)"]
PROTS:容器的端口信息和使用的连接类型(TCP/UDP)
NAMES:自动分配的容器名称
容器启动,命令:docker start f8bde570a6e0
[root@izuf61gqesu0zgmx2ctlo6z /]# docker start f8bde570a6e0 f8bde570a6e0 [root@izuf61gqesu0zgmx2ctlo6z /]#
接下我们查看容器列表:
[root@izuf61gqesu0zgmx2ctlo6z /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f8bde570a6e0 centos "/bin/bash" 36 minutes ago Up About a minute infallible_carson c9a026afa9d7 centos "/bin/echo 'Hello wo…" 51 minutes ago Exited (0) 51 minutes ago naughty_vaughan
我们看到容器id为"f8bde570a6e0"的容器,状态(STATUS) 为启动状态了。
4.容器停止
我们停止刚才启动的容器,命令:docker stop f8bde570a6e0
[root@izuf61gqesu0zgmx2ctlo6z /]# docker stop f8bde570a6e0 f8bde570a6e0 [root@izuf61gqesu0zgmx2ctlo6z /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f8bde570a6e0 centos "/bin/bash" 42 minutes ago Exited (0) 8 seconds ago infallible_carson c9a026afa9d7 centos "/bin/echo 'Hello wo…" 57 minutes ago Exited (0) 57 minutes ago naughty_vaughan
容器id为"f8bde570a6e0"的容器,状态已经为Exited(停止)。
5.进入&退出容器
刚才我们已经启动了容器,但是它只是在后台运行,没办法和它进行交互式访问,接下我们再启动容器,通过命令进入容器
[root@izuf61gqesu0zgmx2ctlo6z /]# docker start f8bde570a6e0 f8bde570a6e0 [root@izuf61gqesu0zgmx2ctlo6z /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f8bde570a6e0 centos "/bin/bash" About an hour ago Up 4 seconds infallible_carson c9a026afa9d7 centos "/bin/echo 'Hello wo…" About an hour ago Exited (0) About an hour ago naughty_vaughan
进入容器有两种方式:attach、exec;推荐使用exec命令,因为此退出容器终端,不会到导致容器的停止。
下面演示docker attach命令:
[root@izuf61gqesu0zgmx2ctlo6z /]# docker attach f8bde570a6e0 [root@f8bde570a6e0 /]# exit exit [root@izuf61gqesu0zgmx2ctlo6z /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f8bde570a6e0 centos "/bin/bash" About an hour ago Exited (0) 7 seconds ago infallible_carson c9a026afa9d7 centos "/bin/echo 'Hello wo…" About an hour ago Exited (0) About an hour ago naughty_vaughan
进入容器后,当使用exit退出容器时,容器此时的状态为停止。
下面演示docker exec命令:
[root@izuf61gqesu0zgmx2ctlo6z ~]# docker start f8bde570a6e0 f8bde570a6e0 [root@izuf61gqesu0zgmx2ctlo6z ~]# docker exec -it f8bde570a6e0 /bin/bash [root@f8bde570a6e0 /]# exit exit [root@izuf61gqesu0zgmx2ctlo6z ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f8bde570a6e0 centos "/bin/bash" About an hour ago Up 15 seconds infallible_carson c9a026afa9d7 centos "/bin/echo 'Hello wo…" 2 hours ago Exited (0) 2 hours ago naughty_vaughan
我现在已经从容器里退出了,但是容器状态显示为Up运行中,说明exec方式进入容器后退出时,不会使容器停止。