原创docker在Ubuntu下1小时快速学习

Posted Mr.智

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了原创docker在Ubuntu下1小时快速学习相关的知识,希望对你有一定的参考价值。

前言

由于工作原因,很多情况下需要快速学习新的知识,针对docker如果从头到尾看相关书籍学习会非常慢,所以整理了下docker的常用操作,只要跟着本文学习操作,一小时就能掌握docker大部分最常用操作方法,也可以当做工具手册随时查找学习,当然本文未涉及的部分,还是需要通过阅读书籍学习,这文章的目的是帮助需要快速上手应用的人。由于写该文章的时候还比较早,所以所用系统和docker版本比较早,但是基本上其他版本操作基本一致,就不在重新更换版本重新编写。

一、 Ubuntu 14.0.4系统安装docker

1.1 在线安装docker

以下操作步骤均在root用户下操作

序列
操作步骤
详细说明
1 检查内核是否符合要求 Docker 要求 Ubuntu 系统的内核版本高于 3.10 ,建议在 Ubuntu14.04 版本

root@duke:/var/cache/apt/archives# uname -r
3.13.0-135-generic

2 安装docker

root@duke:~# wget -qO- https://get.docker.com/ | sh

root@duke:~# curl -sSL 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.0-ce
API version: 1.34
Go version: go1.8.3
Git commit: 1caf76c
Built: Mon Nov 20 18:36:37 2017
OS/Arch: linux/amd64
Server:
Version: 17.11.0-ce
API version: 1.34 (minimum version 1.12)
Go version: go1.8.3
Git commit: 1caf76c
Built: Mon Nov 20 18:35:10 2017
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!
当要以非root用户可以直接运行docker时,需要执行 sudo usermod -aG docker runoob 命令,然后重新登陆,否则会有如下报错
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.

3 启动docker 后台服务

root@duke: service docker start
start: Job is already running: docker
root@duke:

4 测试运行hello-world

root@duke: docker run hello-world

【注意】:如果在sh -c apt-get install -y -qq --no-install-recommends docker-ce >/dev/null这步失败时,报以下错误:

E: 无法下载 https://download.docker.com/linux/ubuntu/dists/trusty/pool/edge/amd64/docker-ce_17.11.0~ce-0~ubuntu_amd64.deb Operation too slow. Less than 10 bytes/sec transferred the last 120 seconds

可以先利用工具下载docker-ce_17.11.0~ce-0~ubuntu_amd64.deb,下好后,将docker-ce_17.11.0~ce-0~ubuntu_amd64.deb放入/var/cache/apt/archives目录即可。

1.2 修改docker默认存储路径

Docker使用会占用大量的磁盘空间。默认的存储路径是/var/lib/docker/,一般情况下/var/lib/路径是系统默认磁盘容量空间不会很大, 所以会造成docker占用磁盘满的问题产生。所以在安装好docker后,最好第一时间修改docker的默认存储路径。

  1. 查看docker当前配置

    docker info
    

    root@duke-211:/etc/systemd/system/docker.service.d# docker info
    Containers: 0
    Running: 0
    Paused: 0
    Stopped: 0
    Images: 0
    Server Version: 17.03.2-ce
    Storage Driver: aufs
    Root Dir: /var/lib/docker/aufs
    Backing Filesystem: extfs
    Dirs: 0
    Dirperm1 Supported: true
    Logging Driver: json-file
    Cgroup Driver: cgroupfs
    Plugins:
    Volume: local
    Network: bridge host macvlan null overlay
    Swarm: inactive
    Runtimes: runc
    Default Runtime: runc
    Init Binary: docker-init
    containerd version: 4ab9917febca54791c5f071a9d1f404867857fcc
    runc version: 54296cf40ad8143b62dbcaa1d90e520a2136ddfe
    init version: 949e6fa
    Security Options:
    apparmor
    seccomp
    Profile: default
    Kernel Version: 4.10.0-28-generic
    Operating System: Ubuntu 16.04.3 LTS
    OSType: linux
    Architecture: x86_64
    CPUs: 12
    Total Memory: 62.83 GiB
    Name: duke-211
    ID: IPDB:XJ53:E2RU:ML3E:BA4G:FQBL:GBSW:SBM5:M3PA:JI3G:A2TW:NPKO
    Docker Root Dir: /var/lib/docker 需要修改该路径
    Debug Mode (client): false
    Debug Mode (server): false
    Registry: https://index.docker.io/v1/
    Experimental: false
    Insecure Registries:
    127.0.0.0/8
    Live Restore Enabled: false

    WARNING: No swap limit support

  2. 创建docker配置目录(下面是ubuntu16.04的系统环境命令)

    mkdir -p /etc/systemd/system/docker.service.d
    
  3. 新增docker配置文件(下面是ubuntu16.04的系统环境命令)

    root@duke-211:/data1/docker# cd /etc/systemd/system/docker.service.d
    root@duke-211:/etc/systemd/system/docker.service.d# vi docker-overlay.conf
    [Service]
    ExecStart=
    ExecStart=/usr/bin/dockerd --graph="/home/docker" --storage-driver=overlay

  4. 停止docker

    service docker stop
    
  5. 重载docker配置

    systemctl daemon-reload
    
  6. 启动docker

    service docker start
    
  7. 查看docker当前配置

    root@duke-211:/etc/systemd/system/docker.service.d# docker info
    Containers: 0
    Running: 0
    Paused: 0
    Stopped: 0
    Images: 0
    Server Version: 17.03.2-ce
    Storage Driver: overlay
    Backing Filesystem: extfs
    Supports d_type: true
    Logging Driver: json-file
    Cgroup Driver: cgroupfs
    Plugins:
    Volume: local
    Network: bridge host macvlan null overlay
    Swarm: inactive
    Runtimes: runc
    Default Runtime: runc
    Init Binary: docker-init
    containerd version: 4ab9917febca54791c5f071a9d1f404867857fcc
    runc version: 54296cf40ad8143b62dbcaa1d90e520a2136ddfe
    init version: 949e6fa
    Security Options:
    apparmor
    seccomp
    Profile: default
    Kernel Version: 4.10.0-28-generic
    Operating System: Ubuntu 16.04.3 LTS
    OSType: linux
    Architecture: x86_64
    CPUs: 12
    Total Memory: 62.83 GiB
    Name: duke-212
    ID: OAIE:B5FM:CLEF:G4YS:DNDS:NSKV:JE26:JZYL:GOY3:DDLI:JGDV:OFHX
    Docker Root Dir: /home/docker 已经完成存储目录修改
    Debug Mode (client): false
    Debug Mode (server): false
    Registry: https://index.docker.io/v1/
    Experimental: false
    Insecure Registries:
    127.0.0.0/8
    Live Restore Enabled: false

    WARNING: No swap limit support

二、 Docker使用非root用户

通常我们使用Docker的时候都是使用的root,官方说法如下:

The docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can access it with sudo. For this reason, docker daemon always runs as the root user. 
To avoid having to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.

下面是使用非root用户操作的步骤:

  1. 创建docker组
    sudo groupadd docker
    
  2. 将当前用户加入docker组
    sudo gpasswd -a ${USER} docker
    
  3. 重新启动docker服务(下面是CentOS7的命令)
    sudo systemctl restart docker
    
  4. 当前用户退出系统重新登陆
  5. 运行docker命令
    docker ps
    

三、 镜像

以下操作步骤均在root用户下操作

3.1 镜像查找

  1. 镜像可以自己做,也可以从官方直接下载,在https://hub.docker.com就可以直接查找下载
  2. 也可以直接使用docker命令进行镜像查找如下表(例:不截断信息的搜索14.04.1的镜像),命令如下
    docker search --no-trunc=true 14.04.1
    
    执行过程如下:
    root@duke:/var/cache/apt/archives# docker search --no-trunc=true 14.04.1 
    NAME                                  DESCRIPTION                                                                                                             STARS                                                                  OFFICIAL            AUTOMATED
    linode/lamp                           LAMP on Ubuntu 14.04.1 LTS Container                                                                                    121                                                                                        
    araczkowski/oracle-apex-ords          Oracle Express Edition 11g Release 2 on Ubuntu 14.04.1 LTS with APEX 5 and ORDS                                         13                                                                                         [OK]
    b7alt/drupal                          Drupal >= 7.34 already installed, SQLite on nginx, APC, SSH, drush, Ubuntu 14.04.1 LTS                                  5                                                                                          [OK]
    densuke/trusty-jp                     [Obsoleted] Ubuntu Linux 14.04LTS(14.04.1)に日本語の風味を付けておきました                                                   3                                                                                          [OK]
    matriphe/ubuntunginxphp               Ubuntu 14.04.1 (phusion/baseimage-docker) with Nginx and PHP.                                                           1                                                                                          [OK]
    zsoltm/ubuntu-armhf                   Ubuntu 14.04.1 minimal install, latest updates for ARMv7 (armhf) 
    

3.2 本地镜像查看

命令如下

docker images

执行过程如下:

root@duke:~# docker   images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              f2a91732366c        6 days ago          1.85kB
ubuntu              14.04               d6ed29ffda6b        9 days ago          221MB

3.3 镜像添加标签

镜像可以添加多个标签,可以理解为镜像别名,添加标签的镜像,并没有新生成镜像,只是生成了一个新的连接,就像linux中的ln命令一样,具体操作命令如下:

root@duke:~# docker images 
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest f2a91732366c 7 days ago 1.85kB
ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
registry latest a07e3f32a779 3 weeks ago 33.3MB
root@duke:~# docker tag ubuntu:14.04 10.0.0.76:5000/test_registry
root@duke:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest f2a91732366c 7 days ago 1.85kB
ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
10.0.0.76:5000/test_registry latest d6ed29ffda6b 10 days ago 221MB
registry latest a07e3f32a779 3 weeks ago 33.3MB

四、 容器

4.1 创建容器

root@duke:~# docker run -it ubuntu:14.04 bash 该命令是启动+创建容器
root@ef664677b896:/# ping localhost 
PING localhost (127.0.0.1) 56(84) bytes of data. 
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.054 ms 
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.026 ms 
^C 
--- localhost ping statistics --- 
2 packets transmitted, 2 received, 0% packet loss, time 999ms 
rtt min/avg/max/mdev = 0.026/0.040/0.054/0.014 ms 
root@ef664677b896:/# 
root@ef664677b896:/# ssh 
bash: ssh: command not found 
root@ef664677b896:/# exit 退出容器伪终端,相应的容器也会被终止运行
exit
root@duke:~# 

4.2 查看容器

root@duke:~# docker ps -a 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
ef664677b896 ubuntu:14.04 "bash" 21 minutes ago Exited (127) 2 minutes ago elegant_newton 
d97b7dc8aadd hello-world "/hello" 2 hours ago Exited (0) 2 hours ago thirsty_jackson 

4.3 删除容器

root@duke:~# docker rm d97b7dc8aadd 
d97b7dc8aadd
root@duke:~# docker ps -a 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
ef664677b896 ubuntu:14.04 "bash" About an hour ago Exited (127) 27 minutes ago elegant_newton 

有时候会存在大量退出状态的容器,可以使用以下几个方法删除:

  • 方法一:查询所有的容器,过滤出Exited状态的容器,列出容器ID,删除这些容器

    sudo docker rm `docker ps -a|grep Exited|awk \'{print $1}\'`
    
  • 方法二:删除所有未运行的容器(已经运行的删除不了,未运行的就一起被删除了)

    sudo docker rm $(sudo docker ps -a -q)
    
  • 方法三:根据容器的状态,删除Exited状态的容器

    sudo docker rm $(sudo docker ps -qf status=exited)
    
  • 方法四(官方):Docker 1.13版本以后,可以使用 docker containers prune 命令,删除孤立的容器。

    sudo docker container prune
    

4.4 终止容器

root@duke:/var/run# docker ps -a 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
ef664677b896 ubuntu:14.04 "bash" 2 hours ago Up 4 seconds elegant_newton
root@duke:/var/run# docker stop ef664677b896 
ef664677b896
root@duke:/var/run# docker ps -a 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
ef664677b896 ubuntu:14.04 "bash" 2 hours ago Exited (0) 47 seconds ago elegant_newton 

4.5 启动容器

root@duke:/var/run# docker ps -a 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
ef664677b896 ubuntu:14.04 "bash" 2 hours ago Exited (127) 2 hours ago elegant_newton 
root@duke:/var/run# docker start ef664677b896 
ef664677b896
root@duke:/var/run# docker ps -a 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
ef664677b896 ubuntu:14.04 "bash" 2 hours ago Up 4 seconds elegant_newton 

4.6 进入容器

root@duke:/var/run# docker exec -it ef664677b896 /bin/bash 
root@ef664677b896:/# hostname
ef664677b896
root@ef664677b896:/# exit
exit
root@duke:/var/run# docker ps -a 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
ef664677b896 ubuntu:14.04 "bash" 2 hours ago Up 31 seconds 

4.7 导出容器

root@duke2:/data1/duke/docker/container# docker ps -a 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
f9e7b88c5b99 ubuntu14.04-cn-ssh-vim:0.1 "/run.sh" 44 hours ago Up 44 hours 0.0.0.0:30001->22/tcp ubuntu14.04-cn-ssh-vim 
68b9abed5bf8 flyceek/centos7-ssh "/usr/sbin/sshd -D" 2 days ago Up 2 hours 0.0.0.0:30002->22/tcp centos7-ssh 
b7f4b7a70d99 ubuntu:14.04 "bash" 3 days ago Up 47 hours festive_brown 
root@duke2:/data1/duke/docker/container# docker export 68b9abed5bf8 > centos7-ssh.tar 
root@duke2:/data1/duke/docker/container# ls 
centos7-ssh.tar 

4.8 导入容器

root@duke172:/data/docker/container# docker import centos7-ssh.tar centos7-ssh:v0.1 
sha256:df1bf065ea466b18c32ac4c5492497d4622f8e477cffa216e65078986f4f56d3 
root@duke172:/data/docker/container# docker images 
REPOSITORY TAG IMAGE ID CREATED SIZE 
centos7-ssh v0.1 df1bf065ea46 6 seconds ago 3.37GB 

五、仓库

前面章节下载的镜像都来自于hup.docker.com的公共镜像仓库,docker实际上还提供了本地镜像仓库,用于存放自建的镜像或者是从公共仓库中下载的镜像。

5.1 创建本地仓库

从公共仓库下载registry镜像,将会自动搭建本地私有仓库。
默认创建仓库命令如下,此时将会将仓库创建在容器的/tmp/registry下:

root@duke:~# docker run -d -p 5000:5000 registry

如果要指定仓库的存放目录,就可以使用-v参数来进行路径指定,命令如下:

root@duke:~# docker run -d -p 5000:5000 -v /home/docker/registry:/tmp/registry registry 
Unable to find image \'registry:latest\' locally 
latest: Pulling from library/registry 
49388a8c9c86: Pull complete 
e4d43608dd22: Pull complete 
3a41740f900c: Pull complete 
e16ef4b76684: Pull complete 
65f212f7c778: Pull complete 
Digest: sha256:d837de65fd9bdb81d74055f1dc9cc9154ad5d8d5328f42f57f273000c402c76d 
Status: Downloaded newer image for registry:latest 
8893ffeb80dd5746453cfa6ed43f29d0bb640de607d94fef0f31ea080ad15fb7 

本地仓库创建完成后,会自动启动5000监听端口

5.2 验证本地仓库

在能够连接本地镜像服务器的浏览器中输入http://10.0.0.76:5000/v2/进行登录查看,返回json数据就表示安装成功

【注意】:
本地仓库是存在版本的
当前下载的仓库镜像版本为V2
因此网上使用http://10.0.0.76:5000/v1的查看方式是无法生效的,只会返回404 page not found错误

5.3 上传镜像到本地仓库

将自己别名的镜像上传到本地仓库(必须使用tag别名私有仓库地址前缀,否则无法上传到私有镜像库),操作如下:

root@duke:~# docker push 10.0.0.76:5000/test_registry 
The push refers to repository [10.0.0.76:5000/test_registry] 
Get https://10.0.0.76:5000/v2/: http: server gave HTTP response to HTTPS client 
root@duke:~# 

但是在上述命令中报错,信息为:

Get https://10.0.0.76:5000/v2/: http: server gave HTTP response to HTTPS client

这个问题可能是由于客户端采用https,docker registry未采用https服务所致。
处理方式是把客户对地址“10.0.0.76:5000”请求改为http。
【修改方法】:
1、在docker1.12.3以前的版本,修改docker的配置文件/etc/systemconfig/docker,重启docker来解决这个问题。
2、在docker1.12.3以后的版本,在/etc/docker/目录下,创建daemon.json文件。在文件中写入{ "insecure-registries":["10.0.0.76:5000"] },保存退出后,重启docker。
操作步骤如下:

序列操作步骤
详细说明
1 新增配置

root@duke:~# vim /etc/docker/daemon.json
{ "insecure-registries":["10.0.0.76:5000"] }
~
"/etc/docker/daemon.json" [] 1L, 45C 已写入

2 重启docker

root@duke:~# service docker restart
docker stop/waiting
docker start/running, process 20676

3 查看容器状态

root@duke:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8893ffeb80dd registry "/entrypoint.sh /etc" About an hour ago Exited (2) 52 seconds ago infallible_swirles
ef664677b896 ubuntu:14.04 "bash" 18 hours ago Exited (0) 52 seconds ago elegant_newton

4 启动本地仓库容器

root@duke:~# docker start 8893ffeb80dd
8893ffeb80dd

5 上传镜像到本地仓库

root@duke:~# docker push 10.0.0.76:5000/test_registry
The push refers to repository [10.0.0.76:5000/test_registry]
59482791e4b2: Pushed
cd514e6bdf2f: Pushed
02323b2bcb37: Pushed
c088f4b849d4: Pushed
c08b59ef4a3d: Pushed
latest: digest: sha256:f558f2d306f8cb0390426da1e18e9489a870c4e66876f03bed29dec4c6aa62c2 size: 1359

5.4 查看本地仓库镜像

【查看方式一】:

root@duke:~# curl http://10.0.0.76:5000/v2/_catalog 
{"repositories":["test_registry"]} 
root@duke:~# curl http://10.0.0.76:5000/v2/test_registry/tags/list 
{"name":"test_registry","tags":["latest"]} 

【查看方式二】:
在能够连接本地镜像服务器的浏览器中输入http://10.0.0.76:5000/v2/_catalog进行登录查看,返回json数据就表示安装成功,如下图:

 

详细 curl 操作docker仓库,见官方文档

5.5 下载本地仓库镜像

序列操作步骤
详细说明
1 查看本地镜像

root@duke:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest f2a91732366c 7 days ago 1.85kB
10.0.0.76:5000/test_registry latest d6ed29ffda6b 10 days ago 221MB
ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
registry latest a07e3f32a779 3 weeks ago 33.3MB

2 删除已经存在的镜像

root@duke:~# docker rmi 10.0.0.76:5000/test_registry
Untagged: 10.0.0.76:5000/test_registry:latest
Untagged: 10.0.0.76:5000/test_registry@sha256:f558f2d306f8cb0390426da1e18e9489a870c4e66876f03bed29dec4c6aa62c2

3 查看本地镜像

root@duke:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest f2a91732366c 7 days ago 1.85kB
ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
registry latest a07e3f32a779 3 weeks ago 33.3MB

4 查看容器状态

root@duke:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8893ffeb80dd registry "/entrypoint.sh /etc …" 4 hours ago Exited (2) 3 minutes ago infallible_swirles
ef664677b896 ubuntu:14.04 "bash" 21 hours ago Exited (0) 3 hours ago elegant_newton

5 启动本地仓库容器

root@duke:~# docker start 8893ffeb80dd
8893ffeb80dd

6 从本地仓库下载镜像

root@duke:~# docker pull 10.0.0.76:5000/test_registry
Using default tag: latest
latest: Pulling from test_registry
Digest: sha256:f558f2d306f8cb0390426da1e18e9489a870c4e66876f03bed29dec4c6aa62c2
Status: Downloaded newer image for 10.0.0.76:5000/test_registry:latest

7 查看镜像是否下载

root@duke:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest f2a91732366c 7 days ago 1.85kB
10.0.0.76:5000/test_registry latest d6ed29ffda6b 10 days ago 221MB
ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
registry latest a07e3f32a779 3 weeks ago 33.3MB

5.6 设置本地仓库安全校验

由于有的新版本的docker对安全性要求较高,所以需要仓库支持SSL/TLS的证书,对于内部使用的私有仓库,可以自行配置证书,或者关闭仓库的安全检验。
仓库关闭证书校验方法如下:

序列操作步骤
详细说明
1 修改配置

root@duke:~# vi /etc/docker/daemon.json
{ "insecure-registries":["10.0.0.76:5000"] }
DOCKER_OPTS="--insecure-registry 10.0.0.76:5000"
~
"/etc/docker/daemon.json" 2L, 94C 已写入

2 重启docker

root@duke:~# service docker restart

六、创建镜像--开机启动ssh

以下操作步骤均在root用户下操作,执行以下命令:

序列操作步骤
详细说明
1 查看容器状态

root@duke:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8893ffeb80dd registry "/entrypoint.sh /etc" 5 hours ago Up 44 minutes 0.0.0.0:5000->5000/tcp infallible_swirles
ef664677b896 ubuntu:14.04 "bash" 22 hours ago Exited (0) 4 hours ago elegant_newton

2 启动容器

root@duke:~# docker start ef664677b896
ef664677b896

3 进入容器

root@duke:~# docker exec -it ef664677b896 /bin/bash

4 安装ssh

root@ef664677b896:/# ssh
bash: ssh: command not found
root@ef664677b896:/# apt-get install ssh
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package ssh
root@ef664677b896:/# apt-get update
Get:1 http://security.ubuntu.com trusty-security InRelease [65.9 kB]
Ign http://archive.ubuntu.com trusty InRelease
Get:2 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]
。。。。。。
Fetched 21.1 MB in 48s (440 kB/s)
Reading package lists... Done
root@ef664677b896:/# apt-get install ssh
Reading package lists... Done
Building dependency tree
。。。。。。
Processing triggers for ca-certificates (20170717~14.04.1) ...
Updating certificates in /etc/ssl/certs... 148 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d....done.

5 使用ssh远程登陆

root@ef664677b896:/# ssh root@10.0.0.11
The authenticity of host \'10.0.0.11 (10.0.0.11)\' can\'t be established.
ECDSA key fingerprint is 52:0d:55:c6:21:0f:9c:50:b6:37:05:5a:90:13:75:06.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added \'10.0.0.11\' (ECDSA) to the list of known hosts.
root@10.0.0.11\'s password:
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-32-generic x86_64)
* Documentation: https://help.ubuntu.com/
619 packages can be updated.
352 updates are security updates.
Last login: Tue Nov 28 14:07:59 2017 from 10.0.0.76
root@duke2:~# exit
登出
Connection to 10.0.0.11 closed.

6 创建ssh目录

创建/var/run/sshd目录,该目录必须存在
root@ef664677b896:/#mkdir -p /var/run/sshd

7 启动ssh

root@ef664677b896:/#/usr/sbin/sshd -D &

8 修改root密码

root@ef664677b896:/#passwd root
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

9 修改pam限制

root@ef664677b896:/#sed -ri \'s/session required pam_loginuid.so/#session required pam_loginuid.so/g\' /etc/pam.d/sshd

10 修改ssh配置文件

注释PermitRootLogin without-password
添加PermitRootLogin yes

root@ef664677b896:/# vi /etc/ssh/sshd_config
# Package generated configuration file
。。。。。。
LoginGraceTime 120
#PermitRootLogin without-password
PermitRootLogin yes

StrictModes yes

11 新建容器启动执行脚本

root@ef664677b896:/#cd / 必须在 / 目录下创建执行脚本
root@ef664677b896:/#vi run.sh
#!/bin/bash
/usr/sbin/sshd -D

root@ef664677b896:/#chmod +x run.sh

12 退出容器

root@ef664677b896:/# exit
exit

13 生成包含ssh功能的镜像

root@duke:~# docker commit -m "ubuntu 14.04 add ssh service" -a "hzw-duke" ef664677b896 ubuntu14.04-ssh:0.1
sha256:8443c965ca81a638444b6bd0b0f938fbebaa49aed4368084d3f298d3ebacdd2c

14 查看镜像库

root@duke:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu14.04-ssh 0.1 8443c965ca81 14 seconds ago 284MB
hello-world latest f2a91732366c 7 days ago 1.85kB
ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
10.0.0.76:5000/test_registry latest d6ed29ffda6b 10 days ago 221MB
registry latest a07e3f32a779 3 weeks ago 33.3MB

七、 存出镜像

以下操作步骤均在root用户下操作,执行以下命令:
方法1:

序列操作步骤
详细说明
1 查看镜像

root@duke:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu14.04-ssh 0.1 8443c965ca81 About an hour ago 284MB
hello-world latest f2a91732366c 7 days ago 1.85kB
10.0.0.76:5000/test_registry latest d6ed29ffda6b 10 days ago 221MB
ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
registry latest a07e3f32a779 3 weeks ago 33.3MB

2 存出镜像

root@duke:~# docker save ubuntu14.04-ssh > ubuntu14.04-ssh.tar

 
3 查看镜像包

root@duke:~# ls -alh ubuntu14.04-ssh.tar
-rw-r--r-- 1 root root 284M 11 月 28 16:37 ubuntu14.04-ssh.tar

方法2:

序列操作步骤
详细说明
1 查看镜像

root@duke:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu14.04-ssh 0.1 8443c965ca81 About an hour ago 284MB
hello-world latest f2a91732366c 7 days ago 1.85kB
10.0.0.76:5000/test_registry latest d6ed29ffda6b 10 days ago 221MB
ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
registry latest a07e3f32a779 3 weeks ago 33.3MB

2 存出镜像

root@duke:~# docker save --output ubuntu14.04-ssh.tar ubuntu14.04-ssh

3 查看镜像包

root@duke:~# ls -alh ubuntu14.04-ssh.tar
-rw-r--r-- 1 root root 284M 11 月 28 16:37 ubuntu14.04-ssh.tar

方法3:

序列操作步骤
详细说明
1 查看镜像

root@duke:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu14.04-ssh 0.1 8443c965ca81 About an hour ago 284MB
hello-world latest f2a91732366c 7 days ago 1.85kB
10.0.0.76:5000/test_registry latest d6ed29ffda6b 10 days ago 221MB
ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
registry latest a07e3f32a779 3 weeks ago 33.3MB

2 存出镜像

root@duke:~# docker save -o ubuntu14.04-ssh.tar ubuntu14.04-ssh

3 查看镜像包

root@duke:~# ls -alh ubuntu14.04-ssh.tar
-rw-r--r-- 1 root root 284M 11 月 28 16:37 ubuntu14.04-ssh.tar

方法4:

序列操作步骤
详细说明
1 查看镜像

root@duke:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu14.04-ssh 0.1 8443c965ca81 About an hour ago 284MB
hello-world latest f2a91732366c 7 days ago 1.85kB
10.0.0.76:5000/test_registry latest d6ed29ffda6b 10 days ago 221MB
ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
registry latest a07e3f32a779 3 weeks ago 33.3MB

2 存出镜像

root@duke:~# docker save -o ubuntu14.04-ssh.tar ubuntu14.04-ssh:0.1

3 查看镜像包

root@duke:~# ls -alh ubuntu14.04-ssh.tar
-rw-r--r-- 1 root root 284M 11 月 28 16:37 ubuntu14.04-ssh.tar

八、 载入镜像

以下操作步骤均在root用户下操作,执行以下命令:
方法1:

序列操作步骤
详细说明
1 查看镜像包

root@duke:~# ls -alh ubuntu14.04-ssh.tar
-rw-r--r-- 1 root root 284M 11 月 28 16:37 ubuntu14.04-ssh.tar

2 载入镜像

root@duke:~# docker load --input ubuntu14.04-ssh.tar

3 查看镜像

root@duke:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu14.04-ssh 0.1 8443c965ca81 About an hour ago 284MB
hello-world latest f2a91732366c 7 days ago 1.85kB
10.0.0.76:5000/test_registry latest d6ed29ffda6b 10 days ago 221MB
ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
registry latest a07e3f32a779 3 weeks ago 33.3MB

方法2:

序列操作步骤
详细说明
1 查看镜像包

root@duke:~# ls -alh ubuntu14.04-ssh.tar
-rw-r--r-- 1 root root 284M 11 月 28 16:37 ubuntu14.04-ssh.tar

2 载入镜像

root@duke:~# docker load < ubuntu14.04-ssh.tar

3 查看镜像

root@duke:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu14.04-ssh 0.1 8443c965ca81 About an hour ago 284MB
hello-world latest f2a91732366c 7 days ago 1.85kB
10.0.0.76:5000/test_registry latest d6ed29ffda6b 10 days ago 221MB
ubuntu 14.04 d6ed29ffda6b 10 days ago 221MB
registry latest a07e3f32a779 3 weeks ago 33.3MB

九、 docker拥有root权限

root@duke:~# docker run -it --privileged=true ubuntu:14.04

privileged 参数功能,设置为true的时

以上是关于原创docker在Ubuntu下1小时快速学习的主要内容,如果未能解决你的问题,请参考以下文章

ubuntu 中安装jenkins,基于docker运行jenkins

Ubuntu20.04下使用docker安装FATE联邦学习框架

Ubuntu20.04下使用docker安装FATE联邦学习框架

Ubuntu20.04下使用docker安装FATE联邦学习框架

Ubuntu20.04下使用docker安装FATE联邦学习框架

技术分享|在Ubuntu下编译安装GreatSQL