如何在Ubuntu 14.04上安装Docker并运行image

如何在Ubuntu 14.04上安装Docker并运行image

一. 环境确认和准备

1.  所要安装的正是Trusty 14.40 (LTS), 硬件是x86_64的。

目前仅支持以下3个64 bit的Ubuntu 版本,支持x86_64 和armhf (ARM)硬件架构。 

  • Yakkety 16.10
  • Xenial 16.04 (LTS)
  • Trusty 14.04 (LTS)
2. . 删掉先前安装过的旧版docker(新版的名字有变化)
  sudo apt-get remove docker docker-engine          /// 新的版本中,社区版的叫docker-ce, 企业版的叫docker-ee
 
二.  开始安装
1.  安装linux-image-extra-* 以便docker能使用aufs存储驱动
  sudo apt-get update
        sudo apt-get install linux-image-extra-(uname -r)  linux-image-extra-virtual 
2.  安装docker 
  有两种安装方式,一种是通过docker repository(docker 仓库在线安装),另一种是下载安装包离线安装。
  两种方式都试了,但在线方式一直卡住不动,只好用了离线方式。两种安装方式分别如下:
2.1 在线安装
a. 安装apt通过https访问所需要的包 

sudo apt-get install apt-transport-https  ca-certificates  curl software-properties-common

b. 安装docker的官方GPG key

curl -fsSL https://download.docker.com/linux/ubuntu/gpg  | duso apt-key add -

    并用下面命令确认新安装KEY的指纹是9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88

sudo apt-key fingerprint 0EBFCD88 

结果如下: 

pub   4096R/0EBFCD88 2017-02-22

      Key fingerprint = 9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88

uid                  Docker Release (CE deb) <[email protected]>

sub   4096R/F273FCD8 2017-02-22

c. 安装docker repository 到apt source 

       sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu  $(lsb_release -cs)   stable"    //  如果是ARM架构,arch=armhf,  $(lsb_release -cs) 主要是列出当前ubuntu disctibution名字

sudo apt-get update 

    备注: 如果sudo add-apt-repository 或update 没执行成功,手工把这行加到/etc/apt/source.list中也可  

deb [arch=amd64] https://download.docker.com/linux/ubuntu trusty stable

d. 开始安装

 sudo apt-get install docker-ce 

2.2 离线下载

到https://download.docker.com/linux/ubuntu/dists/trusty/pool/stable/amd64/ 下载下面的 .deb安装包,ubuntu 14.40对应的是trusty,不同版本要到对应目录中下,速度不快

sudo dpkg -i  path_to_package.deb     

三. 运行确认安装已OK

sudo 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/  

ps -ef | grep docker 也能看到已运行的dockerd和docker-containerd.

  +  2003  4225  0 15:16 pts/31   00:00:00 grep --color=auto docker

root     31976     1  0 14:17 ?        00:00:04 /usr/bin/dockerd --raw-logs

root     31989 31976  0 14:17 ?        00:00:02 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc

下来就可以pull 远程的image 跑真正的项目了   $ sudo docker docker_dev.sh ......

四.  常用操作命令

docker images   // list imges 

        docker ps -a       // list the containers running 

        docker pull         // download image from repository 

        docker commit      // create image

       docker rm               // delete container

       docker rmi                // delete images

       docker run                // run image/command in a new container

       docker logs xxx  (xxx 为docker images 列出的image id)  // view the logs of running image 

i.e.  running ubuntu:14.04 image

sudo docker run -i -t ubuntu /bin/bash or 

sudo docker pull ubuntu:14.04

sudo docker images

docker run -i -t ubuntu:14.04 // terminal 提示符将显示所进入docker的HOSTNAME

do any change in this image but exit without save, enter again, nothing changed.

docker commit xxx ubuntu:14.04 // xxx 为image id, ubuntu:14.4 为image name, 也可改为其它名字便于区分,rocker images时会显示新的名字  

五. 备注:

1. 不想每次运行时都带着sudo,那就创建个docker给并把当前用户加进去

 sudo groupadd docker  && sudo usermod -aG docker $USER

 退出shell 再登录就可以不用sudo了  docker run hello-world 

参考自:  https://docs.docker.com/engine/installation/linux/ubuntu/  

更多Docker相关教程见以下内容: 

Docker 的详细介绍:请点这里
Docker 的下载地址:请点这里

相关推荐