OS:CentOS Linux release 7.6.1810 (Core)php
Docker版本:Docker version 1.13.1, build 07f3374/1.13.1mysql
docs.docker-cn.com/get-started…linux
先提三个问,docker是什么?docker能帮咱们解决什么问题?为何要使用docker?本文将快速带领你走进容器的世界。程序员
docker是什么? 官方解释:Docker 是一个开源的应用容器引擎,让开发者能够打包他们的应用以及依赖包到一个可移植的容器中,而后发布到任何流行的 Linux 机器上,也能够实现虚拟化。容器是彻底使用沙箱机制,相互之间不会有任何接口sql
我的大白话:或许你如今尚未用过docker,可是你可能用过虚拟机吧?docker和虚拟机的差异在哪儿呢?首先第一个,虚拟机给人的第一映像就是大和重,想要部署一个环境到虚拟机上面,就必须安装一个操做系统才能作相关的操做,其次就是你无论安装什么你都得安装一个操做系统才能运行。可是如今docker的出现就解决了这个问题,为何docker就解决了这个问题呢?其实docker就是将这个软件所须要的操做系统或者相关环境给精简了,去除了全部你用不到的东西,只留下软件最基本的依赖,这些依赖会一层依赖一层,这就会让一个docker的镜像很是小,比起整个操做系统轻便了很是多,并且最主要的是能够将你的环境打包成一个镜像发布到dockerhub等地方,而后你就在各个流行的linux上面直接将镜像下载下来就能够直接使用了,能够说就是一次部署,处处使用。docker
docker能帮咱们解决什么问题? 众所周知,每次咱们的项目代码编写完成事后,会将代码发布到服务器上,可是服务器必需要安装一系列咱们须要的工具或者依赖包,亦或者是一些依赖的软件,这个时候发布项目的时间成本就过高了,若是碰到不熟悉的工做人员,这个时间可能会拉的更长,可是有了docker就不同了,咱们只须要简单的会docker,会一些基础的image和container相关的命令,就能完成这项工做,并且在不少太服务器的时候,也只须要一个docker就能帮咱们完成部署。ubuntu
为何要使用docker? 可能你在想我都有一个Linux系统了还要docker来干吗?可是你有没有想过当你系统的环境须要迁移的时候,好比说大家须要集群,或者须要搭建测试服务器这些等等等等,你就须要花大量的时间来搭建环境,而后下载一些相应的包,可是这中间颇有可能出现各类各样的坑,这些都是没法避免的,可是docker就是一个容器,它无论在那个环境上面运行,它都依赖于本身的容器,而不会去依赖装载容器的系统,这也就避免了由于系统的缘由致使安装的步骤都不同。centos
仍是以程序员的灵魂hello world来做为例子,而后快速进入docker的世界,可使用docker run 镜像名
来启动一个镜像,当这个镜像在本地不存在时,那么docker会到docker仓库去获取这个镜像。 执行命令bash
docker run hello-world
复制代码
响应结果服务器
[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
Trying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
1b930d010525: Pull complete
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Status: Downloaded newer image for docker.io/hello-world:latest
WARNING: IPv4 forwarding is disabled. Networking will not work.
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
[root@localhost ~]#
复制代码
上面咱们已经运行了一个hello-word,那么咱们来看一下执行了hello-world咱们会生成那些东西?首先第一个是镜像,也是最重要的一个。执行如下命令能够查看容器中的全部镜像 执行命令
docker images
复制代码
响应结果
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hello-world latest fce289e99eb9 2 months ago 1.84 kB
[root@localhost ~]#
复制代码
从上面的响应信息能够看到咱们已经有了一个镜像 docker.io/hello-world
,分析一下这个镜像的字段吧
docker run hello-world:latest
docker run fce289e99eb9
来启动。这里咱们来看下由hello-world生成的容器是什么样的 执行命令
docker ps -a
复制代码
响应结果
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e91d9159c248 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago adoring_mestorf
[root@localhost ~]#
复制代码
分析字段
-p 宿主机端口:容器端口
来设置--name 容器名
来设置咱们能够经过docker start 容器id
来启动一个容器 执行命令:
docker start 你的容器id
复制代码
可是能够看到咱们的容器时什么都没有输出,这是为何呢?刚开始不是输出来hello world吗?由于在docker里面容器启动的时候只会输出启动的id,想要看详细信息只有进入容器查看,可使用docker ps
命令来查看运行的容器, 可是咱们执行docker ps为何也没有任何信息?这是由于咱们没有后台运行,必需要在运行(执行run的时候)容器的时候加上-d
参数。
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]#
复制代码
因为咱们的hello-world的例子并不支持后台,咱们能够从新获取一个镜像来执行,咱们能够先执行docker search 镜像名
来查找你想要的镜像, 咱们这里来下载一个Centos的镜像 执行命令
docker search centos
复制代码
响应结果
[root@localhost ~]# docker search centos
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/centos The official build of CentOS. 5261 [OK]
docker.io docker.io/ansible/centos7-ansible Ansible on Centos7 121 [OK]
docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.10 x86_64 / CentOS-7 7.5.1804 x... 109 [OK]
docker.io docker.io/consol/centos-xfce-vnc Centos container with "headless" VNC sessi... 83 [OK]
docker.io docker.io/imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 52 [OK]
docker.io docker.io/centos/mysql-57-centos7 MySQL 5.7 SQL database server 49
docker.io docker.io/tutum/centos Simple CentOS docker image with SSH access 44
docker.io docker.io/gluster/gluster-centos Official GlusterFS Image [ CentOS-7 + Glu... 40 [OK]
docker.io docker.io/openshift/base-centos7 A Centos7 derived base image for Source-To... 39
docker.io docker.io/centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relationa... 37
docker.io docker.io/kinogmt/centos-ssh CentOS with SSH 26 [OK]
docker.io docker.io/centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or b... 22
docker.io docker.io/centos/php-56-centos7 Platform for building and running PHP 5.6 ... 20
docker.io docker.io/openshift/jenkins-2-centos7 A Centos7 based Jenkins v2.x image for use... 20
docker.io docker.io/pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag nam... 10
docker.io docker.io/openshift/wildfly-101-centos7 A Centos7 based WildFly v10.1 image for us... 6
docker.io docker.io/openshift/jenkins-1-centos7 DEPRECATED: A Centos7 based Jenkins v1.x i... 4
docker.io docker.io/darksheer/centos Base Centos Image -- Updated hourly 3 [OK]
docker.io docker.io/pivotaldata/centos Base centos, freshened up a little with a ... 2
docker.io docker.io/pivotaldata/centos-mingw Using the mingw toolchain to cross-compile... 2
docker.io docker.io/blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
docker.io docker.io/openshift/wildfly-81-centos7 A Centos7 based WildFly v8.1 image for use... 1
docker.io docker.io/pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated ... 1
docker.io docker.io/jameseckersall/sonarr-centos Sonarr on CentOS 7 0 [OK]
docker.io docker.io/smartentry/centos centos with smartentry 0 [OK]
[root@localhost ~]#
复制代码
上面咱们已经使用docker search centos
来搜索到了不少的惊醒,如今咱们来使用docker pull 镜像名
来获取镜像吧 执行命令
docker pull docker.io/centos
复制代码
响应结果
[root@localhost ~]# docker pull docker.io/centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
8ba884070f61: Pull complete
Digest: sha256:8d487d68857f5bc9595793279b33d082b03713341ddec91054382641d14db861
Status: Downloaded newer image for docker.io/centos:latest
[root@localhost ~]#
复制代码
能够从响应结果来看到咱们的惊喜那个已经成功的获取下来了,执行docker images
来查看咱们的镜像 执行命令
docker images
复制代码
响应结果
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 9f38484d220f 6 days ago 202 MB
docker.io/hello-world latest fce289e99eb9 2 months ago 1.84 kB
[root@localhost ~]#
复制代码
从上面的响应结果咱们能够看到咱们多了一个centos的镜像,如今来启动这个镜像吧,使用docker run 镜像名
来启动镜像 执行命令
docker run -d -ti --name docker.io/centos /bin/bash
复制代码
参数描述
带上参数的缘由是Docker中系统镜像的缺省命令是 bash,若是不加 -ti bash 命令执行了自动会退出。这是由于若是没有衔接输入流,自己就会立刻结束。加-ti 后docker命令会为容器分配一个伪终端,并接管其stdin/stdout支持交互操做,这时候bash命令不会自动退出。
响应结果
[root@localhost ~]# docker run -d -ti docker.io/centos /bin/bash
9687356aaf8e8b7ce24be22af28c6d6b1d9bc130c34e0211f6da05fc7a244d2b
复制代码
如今容器已经启动起来了,咱们能够上面学习的docker ps -a
命令来查看全部的容器 执行命令
docker ps -a
复制代码
响应结果
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9687356aaf8e docker.io/centos "/bin/bash" 2 minutes ago Up 2 minutes youthful_mccarthy
e91d9159c248 hello-world "/hello" 2 hours ago Exited (0) 36 minutes ago adoring_mestorf
[root@localhost ~]#
复制代码
从上面能够看到已经有两个运行中的容器了,因为咱们刚才启动容器的时候使用来一些额外的参数,如今咱们在来看是否有一个运行中的容器。 执行命令
docker ps
复制代码
响应结果
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9687356aaf8e docker.io/centos "/bin/bash" 5 minutes ago Up 5 minutes youthful_mccarthy
[root@localhost ~]#
复制代码
能够从响应结果里面看到咱们的容器已经在运行了
容器是启动了,可是咱们无法操做呀,这里介绍一下怎么进入容器,可使用该命令进入容器docker exec -ti 容器id bash
执行命令
docker exec -ti 9687356aaf8e bash
复制代码
响应结果
[root@localhost ~]# docker exec -ti 9687356aaf8e bash
[root@9687356aaf8e /]#
复制代码
能够从响应结果来看咱们登录的服务器id变成来容器id,也就是所咱们进入了容器了,如今你就能够在容器里面作你想要作的事了
咱们如今已经在容器里面了,可是想要退出出来怎么办呢?能够执行exit
命令来退出,还能够按键盘上的ctrl + p + q
来退出,这两种退出方式都不会让容器中止
咱们如今已经成功的启动了一个容器了,如今介绍一下如何中止一个容器,可使用命令docker stop 容器id
来中止容器 执行命令
docker stop 9687356aaf8e
复制代码
响应结果
[root@localhost ~]# docker stop 9687356aaf8e
9687356aaf8e
[root@localhost ~]#
复制代码
能够看到中止容器时会返回一个容器的id
当容器过多或者容器无用的时候咱们就须要将容器删除,使用如下命令能够删除一个容器docker rm 你的容器id
执行命令
docker rm 9687356aaf8e
复制代码
响应结果
[root@localhost ~]# docker rm 9687356aaf8e
9687356aaf8e
[root@localhost ~]#
复制代码
能够看到仍是返回了一个被删除的容器id,咱们执行如下docker ps -a 来看下容器到底有没有被删除掉(想要删除容器必须让容器先中止) 执行命令
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e91d9159c248 hello-world "/hello" 3 hours ago Exited (0) 52 minutes ago adoring_mestorf
[root@localhost ~]#
复制代码
当咱们生成来无用镜像的时候就须要将这个镜像删除掉,可是须要注意的是,删除镜像的时候这个镜像不能有容器,也就是说必需要先将容器删除掉才行,能够执行docker rmi 镜像id
来删除一个镜像 执行命令
docker rmi 9f38484d220f
复制代码
响应结果
[root@localhost ~]# docker rmi 9f38484d220f
Untagged: docker.io/centos:latest
Untagged: docker.io/centos@sha256:8d487d68857f5bc9595793279b33d082b03713341ddec91054382641d14db861
Deleted: sha256:9f38484d220fa527b1fb19747638497179500a1bed8bf0498eb788229229e6e1
Deleted: sha256:d69483a6face4499acb974449d1303591fcbb5cdce5420f36f8a6607bda11854
[root@localhost ~]#
复制代码
能够看到这里是返回了多个信息,由于开头咱们就介绍了镜像是一层一层的相互继承的,想要删除这个镜像必须一层一层的删除,因此这里会有多个删除记录。