Docker 从入门到放弃(一)安装
前言
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。
Docker采用 C/S架构 Docker daemon 作为服务端接受来自客户的请求,并处理这些请求(创建、运行、分发容器)。 客户端和服务端既可以运行在一个机器上,也可通过 socket 或者RESTful API 来进行通信。
Docker daemon 一般在宿主主机后台运行,等待接收来自客户端的消息。 Docker 客户端则为用户提供一系列可执行命令,用户用这些命令实现跟 Docker daemon 交互。
安装
前提条件 Docker 要求 Ubuntu 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的 Ubuntu 版本是否支持 Docker。
通过 uname -r 命令查看你当前的内核版本
# uname -r
4.4.--generic
使用脚本安装 Docker
1、获取最新版本的 Docker 安装包
# wget -qO- https://get.docker.com/ | sh
# Executing docker install script, commit: 11aa13e
+ sh -c apt-get update -qq >/dev/null
+ sh -c apt-get install -y -qq apt-transport-https ca-certificates curl software-properties-common >/dev/null
+ sh -c curl -fsSL "https://download.docker.com/linux/ubuntu/gpg" | apt-key add -qq - >/dev/null
+ sh -c echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu trusty edge" > /etc/apt/sources.list.d/docker.list
+ [ ubuntu = debian ]
+ sh -c apt-get update -qq >/dev/null
+ sh -c apt-get install -y -qq --no-install-recommends docker-ce >/dev/null
+ sh -c docker version
Client:
Version: 17.11.-ce
API version: 1.34
Go version: go1.8.3
Git commit: 1caf76c
Built: Mon Nov ::
OS/Arch: linux/amd64 Server:
Version: 17.11.-ce
API version: 1.34 (minimum version 1.12)
Go version: go1.8.3
Git commit: 1caf76c
Built: Mon Nov ::
OS/Arch: linux/amd64
Experimental: false
If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like: sudo usermod -aG docker your-user Remember that you will have to log out and back in for this to take effect! WARNING: Adding a user to the "docker" group will grant the ability to run
containers which can be used to obtain root privileges on the
docker host.
Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
for more information.
安装完成后有个提示:

当要以非root用户可以直接运行docker时,需要执行 sudo usermod -aG docker runoob 命令,然后重新登陆,否则会报错
2、启动docker 后台服务
sudo service docker start
start: Job is already running: docker
3、用Hello World校验Docker的安装
用Docker运行Hello World镜像,命令如下:
# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
Status: Downloaded newer image for hello-world:latest Hello from Docker!
This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps:
. The Docker client contacted the Docker daemon.
. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
. 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://cloud.docker.com/ For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
可见,Docker注册服务器从Docker Hub获取到最新的Hello World镜像,下载到了本地。可以再次运行Hello World镜像。
# docker run hello-world Hello from Docker!
This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps:
. The Docker client contacted the Docker daemon.
. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
. 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://cloud.docker.com/ For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
二、使用
1、Docker Hello World
Docker 允许你在容器内运行应用程序, 使用 docker run 命令来在容器内运行一个应用程序
root@aliyunTinywan:~# docker run ubuntu:15.10 /bin/echo "Hello world"
Unable to find image 'ubuntu:15.10' locally
15.10: Pulling from library/ubuntu
7dcf5a444392: Pull complete
759aa75f3cee: Pull complete
3fa871dc8a2b: Pull complete
224c42ae46e7: Pull complete
Digest: sha256:02521a2d079595241c6793b2044f02eecf294034f31d6e235ac4b2b54ffc41f3
Status: Downloaded newer image for ubuntu:15.10
Hello world
Docker首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库 Docker Hub 下载公共镜像。这里发现镜像ubuntu:15.10 不存在,正在从镜像仓库下载
参数解析:
docker: Docker 的二进制执行文件。
run:与前面的 docker 组合来运行一个容器。
ubuntu:15.10:指定要运行的镜像,Docker首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库 Docker Hub 下载公共镜像。
/bin/echo "Hello world": 在启动的容器里执行的命令
root@aliyunTinywan:~# docker run ubuntu:15.10 /bin/echo "Hello world"
Hello world
以上命令完整的意思可以解释为:Docker 以 ubuntu15.10 镜像创建一个新容器,然后在容器里执行 bin/echo "Hello world",然后输出结果。
2、运行交互式的容器
通过docker的两个参数 -i -t,让docker运行的容器实现"对话"的能力
root@aliyunTinywan:~# docker run -i -t ubuntu:15.10 /bin/bash
root@aliyunTinywan:/#
参数解析:
-t:在新容器内指定一个伪终端或终端。
-i:允许你对容器内的标准输入 (STDIN) 进行交互。
此时我们已进入一个 ubuntu15.10系统的容器,尝试在容器中运行命令 cat /proc/version 和 ls 分别查看当前系统的版本信息和当前目录下的文件列表
root@aliyunTinywan:/# cat /proc/version
Linux version 4.4.--generic (buildd@lcy01-) (gcc version 4.8. (Ubuntu 4.8.-2ubuntu1~14.04.) ) #~14.04.-Ubuntu SMP Mon Aug :: UTC
root@aliyunTinywan:/# cat /etc/issue
Ubuntu 15.10 \n \l
root@aliyunTinywan:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
可以通过运行exit命令或者使用CTRL+D来退出容器,查看当前真实的服务器系统版本
root@18c5f2d5476b:/# exit
exit
root@iZ235mi4a64Z:~# cat /etc/issue
Ubuntu 14.04. LTS \n \l
3、启动容器(后台模式)
使用以下命令创建一个以进程方式运行的容器
root@iZ235mi4a64Z:~# docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
6ddd469230b5402251c4fd3214b63f6a6af7d9e2711f8944a9d74b36346bbd5a
在输出中,我们没有看到期望的"hello world",而是一串长字符 2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63 这个长字符串叫做容器ID,对每个容器来说都是唯一的,我们可以通过容器ID来查看对应的容器发生了什么。
首先,我们需要确认容器有在运行,可以通过 docker ps 来查看
root@iZ235mi4a64Z:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ddd469230b5 ubuntu:15.10 "/bin/sh -c 'while t…" About a minute ago Up About a minute romantic_visvesvaraya
CONTAINER ID:容器ID
NAMES:自动分配的容器名称
在容器内使用docker logs命令,查看容器内的标准输出
(1)使用ID
root@iZ235mi4a64Z:~# docker logs 6ddd469230b5
hello world
hello world
hello world
...
(2)使用NAMES
root@iZ235mi4a64Z:~# docker logs romantic_visvesvaraya
hello world
hello world
...
发现结果是一样的,输出了同样的结果
4、停止容器
使用 docker stop 命令来停止容器,让然啦,要指定停止的容器对象啦,不然会这样子
root@iZ235mi4a64Z:~# docker stop
"docker stop" requires at least argument.
See 'docker stop --help'. Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...] [flags] Stop one or more running containers
指定一个容器
root@iZ235mi4a64Z:~# docker stop 6ddd469230b5
6ddd469230b5
通过 docker ps 查看,容器已经停止工作
root@iZ235mi4a64Z:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
也可以用下面的NAMES命令来停止
docker stop romantic_visvesvaraya
Docker 从入门到放弃(一)安装的更多相关文章
- Docker从入门到放弃(1) Docker简介与安装
目录 一.Docker简介 1.Docker是什么: 2.为什么有docke的出现: 3.docker与传统容器的区别: 4.docker基本组成 5.docker工作原理: 二.Docker安装 ...
- DOCKER 从入门到放弃(一)
前言 关于docker的各种概念已有各位大神珠玉在前,请各位自行查看,本系列的目的是各种详细操作步骤 各种概念特别推荐CloudMan的3篇blog: http://www.cnblogs.com/C ...
- Docker 从入门到放弃(四)Docker+Jenkins_自动化持续集成
Windows 查看密码 $ cat /var/jenkins_home/secrets/initialAdminPassword 14e14c414f41481aa5955753d3f31f9f 自 ...
- Docker 从入门到放弃(三)镜像使用
当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载. 下面我们来学习: 1.管理和使用本地 Docker ...
- CentOS7 下Docker最新入门教程 超级详细 (安装以及简单的使用)
转载https://blog.csdn.net/wzsy_ll/article/details/82866627 1.为什么使用Docker(本人) 最近总是频繁的在新服务器发布项目, 每次发布都需要 ...
- DOCKER 从入门到放弃(二)
搜索镜像 从docker官方镜像仓库搜索镜像 docker search [OPTIONS] TERM OPTIONS: --automated :只显示自动创建的镜像,默认值为fasle --fil ...
- DOCKER 从入门到放弃(三)
使用docker create [image-name] 创建一个容器 创建一个nginx镜像的容器,由于没有指定各项参数,容器实用默认参数,创建后并不会启动,并将容器的ID输出到终端,如果本地没有镜 ...
- Docker 从入门到放弃(二)容器使用
Docker 容器使用 一.Docker 客户端 docker 客户端非常简单 ,我们可以直接输入 docker 命令来查看到 Docker 客户端的所有命令选项. root@iZ235mi4a64Z ...
- docker的入门到放弃--docker基本命令
docker的镜像中国:https://www.docker-cn.com/registry-mirror 1.搜索镜像 [root@localhost ~]# docker search tomca ...
随机推荐
- windows 设置ipsec防火墙
windows server 推荐使用ipsec修改防火墙设置,默认防火墙需要手动导入导出.wfw文件,需要手动添加单条规则,维护麻烦,推荐关闭,使用ipsec管理 以下是线上防火墙配置,可参照业务环 ...
- 做事从来不坚持的我又开始学习PyQt了。。。。。。
链接附上,不再更新:PyQt5图形界面编程 第一部分 第一个程序 # -*- coding: utf-8 -*- import sys from PyQt5.QtWidgets import QApp ...
- 「FJOI2016」神秘数 解题报告
「FJOI2016」神秘数 这题不sb,我挺sb的... 我连不带区间的都不会哇 考虑给你一个整数集,如何求这个神秘数 这有点像一个01背包,复杂度和值域有关.但是你发现01背包可以求出更多的东西,就 ...
- luogu4145 上帝造题的七分钟2 (线段树)
题意:给一个数列,维护两个操作,区间开根号.询问区间和 注意到1e12开根号六次后就变成1,而且根号1等于1 也就是说,就算我们用单点修改,只要跳过1,那么修改的次数最多也就是6n 那么维护一个区间最 ...
- #509. 「LibreOJ NOI Round #1」动态几何问题
下面给出部分分做法和满分做法 有一些奇妙的方法可以拿到同样多的分数,本蒟蒻只能介绍几种常见的做法 如果您想拿18分左右,需要了解:质因数分解 如果您想拿30分左右,需要了解:一种较快的筛法 如果您想拿 ...
- Ubuntu下redis数据库的安装和配置详细过程
Redis 安装 当前redis最新稳定版本是4.0.9 当前ubuntu虚拟机中已经安装好了redis,以下步骤可以跳过 最新稳定版本下载链接:http://download.redis.io/re ...
- Http协议常见状态码
206 - 断点下载时用到,客户端请求了一部分内容,服务器成功把这部分内容返回给它,这时候就是用这个状态. 301 - 永久跳转,原地址不存在了,url被指向到另一个地址.这个主要是搜索引擎相关,影响 ...
- Servlet -- 中文乱码解决
请求:对于get和post都有效果 request.setCharacterEncoding("UTF-8"); 相应: 设置服务器输出的编码为UTF-8 response.set ...
- BZOJ 1370: [Baltic2003]Gang团伙(luogu 1892)(种类并查集)
题面: bzoj题面有误,还是看luogu的吧 https://www.luogu.org/problemnew/show/P1892 题解: 种类并查集.. 因为有敌人的敌人是朋友这个条件,所以需要 ...
- uoj#80 二分图最大权匹配
题意:给定二分图,有边权,求最大边权匹配.边权非负. 解:KM算法求解最大权完备匹配. 完备匹配就是点数少的那一边每个点都有匹配. 为了让完备匹配与最大权匹配等价,我们添加若干条0边使之成为完全二分图 ...