Docker介绍(安装和测试使用)

参考链接1
参考链接2

1 docker安装

#获取最新版本的docker
wget -qO- https://get.docker.com/ | sh
#或者
apt-get install docker.io
#检查是否安装成功以及版本
docker
docker -V

2 术语介绍

docker三大核心:Repositories(仓库)、Images(镜像)和Container(容器);三者可以互相转换。

  • Docker, 平台工具、守护进程
  • Image, 镜像,包括应用及系统的数据包
  • Container, 容器,虚拟机的运行机制
  • Registry, 仓库登记,管理镜像文件
  • Docker hub, Docker官方公有仓库

三大核心的关系,如下图所示:

这里写图片描述

3创建镜像

Docker镜像的创建方式有两种,分别是:

  • 基于现有的镜像修改
  • 直接创建新镜像
    本文介绍基于已有镜像修改的方式,一共分为五步。

3.1 第一步,下载官网的基础镜像

从官网上pull镜像hello-world,并运行

root@apple:/home/kevin/docker# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b04784fba78d: Pull complete 
Digest: sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f
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:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 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://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

从官网上pull镜像ubuntu,并以交互的方式运行

root@apple:/home/kevin/docker# docker run -it ubuntu bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
e0a742c2abfd: Pull complete 
486cb8339a27: Pull complete 
dc6f0d824617: Pull complete 
4f7a5649a30e: Pull complete 
672363445ad2: Pull complete 
Digest: sha256:84c334414e2bfdcae99509a6add166bbb4fa4041dc3fa6af08046a66fed3005f
Status: Downloaded newer image for ubuntu:latest
root@93a1b9d39683:/#
root@93a1b9d39683:/#
root@93a1b9d39683:/#
root@93a1b9d39683:/#
root@93a1b9d39683:/#

3.2 第二步,在container中执行软件的安装

安装linux基础软件
在第一步的基础上,在容器里面,用root用户执行linux基础软件的安装

root@93a1b9d39683:/home# apt-get update      
Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]                                         
Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Get:3 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
Get:4 http://security.ubuntu.com/ubuntu xenial-security/universe Sources [42.0 kB]
Get:5 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [384 kB]
Get:6 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
Get:7 http://archive.ubuntu.com/ubuntu xenial/universe Sources [9802 kB]
Get:8 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [12.8 kB]                                                                                
Get:9 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [178 kB]                                                                                   
Get:10 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [2931 B]                                                                                
Get:11 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB]                                                                                               
Get:12 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages [14.1 kB]                                                                                         
Get:13 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [9827 kB]                                                                                           
Get:14 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [176 kB]                                                                                          
Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/universe Sources [207 kB]                                                                                           
Get:16 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [742 kB]                                                                                        
Get:17 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [13.3 kB]                                                                                 
Get:18 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [640 kB]                                                                                    
Get:19 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [16.8 kB]                                                                                 
Get:20 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [4930 B]                                                                                      
Get:21 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [6237 B]                                                                                  
Fetched 24.2 MB in 12min 34s (32.1 kB/s)                                                                                                                                   
Reading package lists... Done

root@93a1b9d39683:/home# apt-get install vim 
root@93a1b9d39683:/home# apt-get install curl
root@93a1b9d39683:/home# apt-get install openssh-server 
root@93a1b9d39683:/home# apt-get install python 

# 安装ifconfig、netstat
root@93a1b9d39683:/home# apt-get install net-tools

# 安装ping
root@93a1b9d39683:/home# apt-get install iputils-ping

退出container
ctrl+d, 退出容器且关闭,docker ps 查看容器已退出运行(Exited)
ctrl+p, ctrl+q, 退出容器但不关闭,docker ps 查看容器依旧处于up状态

3.3 将container保存成image

查看容器列表

root@apple:/home/kevin/docker# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
966bd52b72da        ubuntu              "/bin/sh -c 'while..."   3 hours ago         Exited (137) 2 hours ago                       stupefied_knuth
568e5204fff3        ubuntu              "/bin/sh -c 'while..."   4 hours ago         Exited (137) 2 hours ago                       kind_khorana
00f561d97811        ubuntu              "/bin/echo hello w..."   4 hours ago         Exited (0) 4 hours ago                         nifty_mcnulty
93a1b9d39683        ubuntu              "bash"                   4 hours ago         Up 3 hours                                     zealous_noether
abdc084f9821        hello-world         "/hello"                 4 hours ago         Exited (0) 3 hours ago                         sleepy_clarke
51097978fdc6        hello-world         "/hello"                 4 hours ago         Exited (0) 3 hours ago                         vibrant_panini

查看镜像列表

root@apple:/home/kevin/docker# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              14f60031763d        3 days ago          120MB
hello-world         latest              1815c82652c0        5 weeks ago         1.84kB

保存容器到镜像

root@apple:/home/kevin/docker# docker commit 93a1b9d39683 learn/visual_init:v1
sha256:56a4eab7dc5b9c1b0dca010e36f4e792963a213ad4b362ab4662602eb66f0216

==============
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]Create a new image from a container's changes

  -a, --author=""     Author (e.g., "John Hannibal Smith <[email protected]>")
  -m, --message=""    Commit message
  -p, --pause=true    Pause container during commit
==============

查看镜像列表

root@apple:/home/kevin/docker# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
learn/visual_init   v1                  56a4eab7dc5b        39 seconds ago      321MB
ubuntu              latest              14f60031763d        3 days ago          120MB
hello-world         latest              1815c82652c0        5 weeks ago         1.84kB

附, 容器的常用操作

root@apple:/home/kevin/docker# docker attach $CONTAINER_ID #连接一个已存在的docker容器
root@apple:/home/kevin/docker# docker stop $CONTAINER_ID #停止docker容器
root@apple:/home/kevin/docker# docker start $CONTAINER_ID #启动docker容器
root@apple:/home/kevin/docker# docker restart $CONTAINER_ID #重启docker容器
root@apple:/home/kevin/docker# docker kill $CONTAINER_ID #强制关闭docker容器
root@apple:/home/kevin/docker# docker logs $CONTAINER_ID #查看docker容器运行日志,确保正常运行
root@apple:/home/kevin/docker# docker inspect $CONTAINER_ID #查看container的属性,比如ip等等
root@apple:/home/kevin/docker# docker rm $CONTAINER_ID      # 删除容器

3.4 第四步,将image上传到repository,或者保存为本地离线文件push image到阿里云的私有仓库

# docker login [email protected] registry.cn-beijing.aliyuncs.com
# docker tag 56a4eab7dc5b registry.cn-beijing.aliyuncs.com/zhangsp/aiwen:visual_init
# docker push registry.cn-beijing.aliyuncs.com/zhangsp/aiwen:visual_init

上传成功,如下图所示
这里写图片描述

保存docker image为本地文件,然后,可以在其他机器上还原使用

root@apple:/home/kevin# docker images
REPOSITORY                                       TAG                 IMAGE ID            CREATED             SIZE
learn/visual_init                                v1                  56a4eab7dc5b        3 minutes ago       321MB
registry.cn-beijing.aliyuncs.com/zhangsp/aiwen   visual_init         56a4eab7dc5b        3 minutes ago       321MB
ubuntu                                           latest              14f60031763d        3 days ago          120MB
hello-world                                      latest              1815c82652c0        5 weeks ago         1.84kB

# 将docker image保存为离线的本地文件,执行docker save image_name > ./save.tar 或者 docker save -o filepath image_name
root@apple:/home/kevin/docker# docker save -o visual_init.tar 56a4eab7dc5b
root@apple:/home/kevin/docker# ll
total 344956
drwxrwxr-x  2 kevin kevin      4096 Jul 24 08:14 ./
drwxr-xr-x 14 kevin kevin      4096 Jul 24 03:07 ../
-rw-rw-r--  1 kevin kevin  19182004 May  5 13:03 docker-engine_17.05.0~ce-0~ubuntu-trusty_amd64.deb
-rw-rw-r--  1 kevin kevin     38292 Feb  7  2016 libltdl7_2.4.6-0.1_amd64.deb
-rw-------  1 root           root           333993472 Jul 24 08:13 visual_init.tar

3.5 第五步,在其他机器上还原image测试使用

  • 加载一个docker镜像文件,执行docker load -i filepath 或者 docker load < ./save.tar
  • 执行docker pull 或者 docker run,运行仓库的image镜像
aiwen@703:~$ docker pull registry.cn-beijing.aliyuncs.com/zhangsp/aiwen:visual_init
visual_init: Pulling from zhangsp/aiwen
e0a742c2abfd: Pull complete 
486cb8339a27: Pull complete 
dc6f0d824617: Pull complete 
4f7a5649a30e: Pull complete 
672363445ad2: Pull complete 
6f2c8900b1ea: Pull complete 
Digest: sha256:faf4284d0980958df3c10c49efcc09a041dc894edd54e29304fb7fbe97c674dc
Status: Downloaded newer image for registry.cn-beijing.aliyuncs.com/zhangsp/aiwen:visual_in
  • 备注:导入image之后,可以通过docker tag命令对image打标签
kevin@orange:~/docker$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
<none>              <none>              293e064b491b        24 hours ago        321.2 MB
kevin@orange:~/docker$ docker tag 293e064b491b aiwen/learn:v1
kevin@orange:~/docker$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
aiwen/learn         v1                  293e064b491b        24 hours ago        321.2 MB

猜你喜欢

转载自blog.csdn.net/minione_2016/article/details/80709520
今日推荐