Docker的安装和测试

时间:2021-01-31 00:36:06

1,Docker安装

Docker是啥,以及其与虚拟机的对比,就不介绍了,网上有很多资源可以学习和了解。 
本篇文章重点介绍Docker的安装和测试使用。

  • Docker的安装,分为离线安装和在线安装两种方式,方便不同的应用场景使用不同的安装方式。
  • 关于Docker的测试使用,本文会介绍如何运行Docker容器、如何修改Docker容器,以及如何完成Docker容器到Docker镜像的转化。

1.1,docker离线安装

很简单,从官网下载最新的docker离线安装包,并安装。

# wget https://apt.dockerproject.org/repo/pool/main/d/docker-engine/docker-engine_17.05.0~ce-0~ubuntu-trusty_amd64.deb
# wget http://archive.ubuntu.com/ubuntu/pool/main/libt/libtool/libltdl7_2.4.6-0.1_amd64.deb
# dpkg -i *.deb

1.2,docker在线安装

通过apt-get安装

# cat /etc/issue
Ubuntu 14.04.5 LTS \n \l
# uname -r
4.4.0-81-generic
# sudo apt-get update # 更新系统资源列表
# apt-get -y install docker.io # 安装docker
# docker -v # 查看docker版本信息
Docker version 1.6.2, build 7c8fca2

通过daocloud安装docker

# curl -sSL https://get.daocloud.io/docker | sh
# docker -v
Docker version 17.06.0-ce, build 02c1d87

通过阿里云的镜像仓库安装docker

# curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet | sh -
# docker -v
Docker version 17.05.0-ce, build 89658be

1.3,执行docker,验证docker是否安装成功

# docker
Usage: docker COMMAND A self-sufficient runtime for containers Options:
--config string Location of client config files (default "/root/.docker")
-D, --debug Enable debug mode
--help Print usage
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
  • 1

1.4,用户权限修改

安装成功之后,需要使用root用户才能执行docker命令。 
如果希望用非root用户执行docker命令,可以执行以下语句。 
注意:执行完成后,需要退出再登陆才能生效。

# sudo usermod -aG docker $your-user
  • 1

2,术语介绍

Docker有仓库Repositories、镜像Images和容器Container三大核心,三者可以互相转化。

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

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

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 <hannibal@a-team.com>")
-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上传到仓库,或者保存为本地离线文件

push image到阿里云的私有镜像仓库

# docker login --username=670590744@qq.com 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的安装和测试

保存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_init
  • 备注:导入IMAGE之后,可以通过docker tag命令对IMAGE打标签
kevin@        

4,参考页面

Docker的安装和测试的更多相关文章

  1. 【Docker】安装并测试安装成功

    1.环境描述 Centos 7 2.安装步骤 通过命令yum install docker安装 等待下载安装-,出现下图,按y继续 继续等待-出现下图按y继续 再继续等待- 知道出现上图表示安装完毕 ...

  2. docker安装与测试 及 安装docker compose

    Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口 ...

  3. 【docker 入门 - 01】- Docker 在 Centos7 上安装与测试

    一.学习文档 官网网站: https://www.docker.com 中文网站:http://www.docker-cn.com 官方安装文档:https://docs.docker.com/ins ...

  4. 『开发技术』Docker开发教程(一)安装与测试(Windows 家庭版)

    0.前言 针对其他系统和版本,Docker都很容易安装,可以参考官方教程:https://docs.docker.com/docker-hub/ 由于Windows10家庭版无法安装docker,因此 ...

  5. centos7安装docker并安装jdk和tomcat(常用命令)

    阿里专属的镜像加速 在宿主机器编辑文件:vi /etc/docker/daemon.json 阿里专属的镜像加速地址,类似于"https://91cntlkt.mirror.aliyuncs ...

  6. docker的安装和技巧

    工作了有一段时间,开发环境中需要docker环境,但是docker一直不算很熟,之前一直是利用yum安装,但是yum安装真的很费劲,所以总结了一些经验给大家: 1,利用yum直接安装 官网是直接给了y ...

  7. 在centos,docker中安装HeadlessChrome

    在centos6中安装chrome与chrome-driver,中间走了很多弯路,遇到很多坑,现将详细步骤总结如下.参考博客链接:https://blog.csdn.net/u013849486/ar ...

  8. 【swoole】如何在docker&plus;swoole环境下测试udp服务

    前面几篇文章讲了使用 docker+swoole 环境来测试tcp服务以及如何测试,但是当我开始学习udp服务那块的时候,发现使用原来的方式在 docker+swoole 环境下行不通啦,后来发现如果 ...

  9. Docker中安装WordPress

    前言 虚拟化技术风靡一时,还不层在生产环境中实践.只能是闭门造车,自己玩一玩了,接触了一下docker最简单的命令,这才自己搭建一个wordpress玩一玩. 安装wordpress之前先把本机的do ...

随机推荐

  1. spring源码:Aware接口(li)

    一.spring容器中的aware接口介绍 Spring中提供了各种Aware接口,比较常见的如BeanFactoryAware,BeanNameAware,ApplicationContextAwa ...

  2. Path Sum

    需如下树节点求和 5  /  \ 4     8  /     /  \ 11  13    4 / \     /  \  7    2      5   1 JavaScript实现 window ...

  3. 找回消失的ASUS显卡

    笔记本蓝屏了几次之后,发现独立显卡GT335M不见了,设备管理器,驱动精灵,都找不到,网上问这个问题的人还不少,得到的回答几乎都是说重装驱动.根本不行,这时候安装nVIDIA驱动,会提示说“未找到兼容 ...

  4. linux shell编程总结

    linux shell编程总结 本周学习了unix/linux shell编程,参考的是<LINUX与UNIX Shell 编程指南>,David Tansley著:徐焱,张春萌等译,由机 ...

  5. iOS 开发设计常用软件及工具整理

    1, xCode 2, AppCode 3, Skech 原型设计软件 4, Hype 动画设计工具 5, fontawsome 免费图表 6, Prepo icon, images.catlog 生 ...

  6. - Shell - sort处理大文件&lpar;页 1&rpar; - ChinaUnix&period;net

    - Shell - sort处理大文件(页 1) - ChinaUnix.net sort处理大文件

  7. virtuoso数据库的安装方法

    数据库virtuoso有两种安装配置方式 第一种就是直接在系统中默认安装,拷贝virtuoso的安装文件,直接默认安装. 第二种是配置安装方式 参考地址:http://vos.openlinksw.c ...

  8. 01&lowbar;Eclipse的使用方法

     1 选择工作站 Workspace:表示工作站 2 切换工作站 选择工作站的方式:File->SwitchWorkspace 这里选择其他的工作站 清除工作站的方式: 找到MyEclips ...

  9. VS2019 实用操作

    本文列出了在编写程序过程中的几个非常实用的操作方式,通过这些操作方式,可以在一定程度上减少重复操作.提高编码效率.改善编程体验. 列模式操作 列操作是一项很常用且实用的功能,可以一次性修改不同的行. ...

  10. supervisor 监控redis &amp&semi; mongodb

    安装 安装python brew install python 安装pipwget https://bootstrap.pypa.io/get-pip.pysudo python get-pip.py ...