docker安装:
第一种方式:
操作系统默认的apt源有docker包,我们可以直接使用下面的apt-get命令安装docker:
$ sudo apt-get install -y docker.io
这种方法安装的docker在1.14.0ubuntu上,版本比较低,
第二种方式:官网
$ sudo apt-get update
$ sudo apt-get install apt-transport-https ca-certificates
$ sudo apt-key adv \
--keyserver hkp://ha.pool.sks-keyservers.net:80 \
--recv-keys 58118E89F3A912897C070ADBF76221572C52609D
从官网上找到你Ubuntu对应版本的Docker的Repository
然后:
$ echo "<REPO>" | sudo tee /etc/apt/sources.list.d/docker.list
把上面的 REPO 替换成你系统版本对应的信息。
但是ubutun仓库自带的往往不是最新的;为了获取最新的docker使用curl
再然后:
$ sudo apt-get update
#列出可用版本
$ apt-cache policy docker-engine
docker-engine:
Installed: 1.12.2-0~trusty
Candidate: 1.12.2-0~trusty
Version table:
*** 1.12.2-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
100 /var/lib/dpkg/status
1.12.1-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
1.12.0-0~trusty 0
500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
#最后执行更新:
$ sudo apt-get install docker-engine
$ docker version #查看 docker版本
这种方法有可能会出现安装失败,会报:
Err https://apt.dockerproject.org/repo/ ubuntu-trusty/main docker-engine amd64 1.12.6-0~ubuntu-trusty
Resolving timed out after 3513 milliseconds
E: Failed to fetch
https://apt.dockerproject.org/repo/pool/main/d/docker-engine/docker-engine_1.12.6-0~ubuntu-trusty_amd64.deb
Resolving timed out after 3513 milliseconds
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
貌似是说因为网速不行,然后给停掉了,这样的话可以找一个网速可以的机器把deb包下下来。(从上面报的错中可以找到)
https://apt.dockerproject.org/repo/pool/main/d/docker-engine/docker-engine_1.12.6-0~ubuntu-trusty_amd64.deb
$ curl -O https://apt.dockerproject.org/repo/pool/main/d/docker-engine/docker-engine_1.12.6-0~ubuntu-trusty_amd64.deb
#然后通过 scp传到服务器上
$ scp docker-engine_1.12.6-0~ubuntu-trusty_amd64.deb root@123.XXX.XXX.XXX:~
# 然后通过 dpkg安装
$ dpkg -i docker-engine_1.12.6-0~ubuntu-trusty_amd64.deb
#可能会报:
The following packages have unmet dependencies:
docker-engine : Depends: libsystemd-journal0 (>= 201) but it is not going to be installed
Recommends: aufs-tools but it is not going to be installed
Recommends: cgroupfs-mount but it is not installable or
cgroup-lite but it is not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
#可以执行
$ apt-get -f install
#然后在安装
$ dpkg -i docker-engine_1.12.6-0~ubuntu-trusty_amd64.deb
第三种方式:
网站 https://get.docker.com 提供了curl-able的安装脚本install.sh,我们可以通过curl的方式进行安装docker。
我们先安装curl:
$ sudo apt-get update
$ sudo apt-get install curl
然后进行docker的安装:
$ curl -k -sSl https://get.docker.com | sudo sh
查看docker版本:
$ docker version
Client:
Version: 1.9.1
API version: 1.21
Go version: go1.4.2
Git commit: a34a1d5
Built: Fri Nov 20 13:08:59 UTC 2015
OS/Arch: linux/amd64
建议使用第二种方式;
可以通过 https://get.docker.com 查看脚本命令。可以知道卸载方式是
sudo apt-get remove docker-engine
docker验证
若上述命令执行成功后,就要验证一下docker是否安装成功了:
执行下面命令
sudo 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:
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](http://lib.csdn.net/base/docker) 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 bashShare images, automate workflows, and more with a free Docker Hub account: https://hub.docker.comFor more examples and ideas,
visit: https://docs.docker.com/userguide/
证明docker正常工作!下面我们就可以开始docker之旅啦~
补充命令:可以使用命令查看容器状态
1、查看正在运行的容器
sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2、查看所有的容器
sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES5ddce0ca3e4f hello-world "/hello" 2 minutes ago Exited (0) 2 minutes ago serene_jennings551e257cbc08 hello-world "/hello" 2 minutes ago Exited (0) 2 minutes ago drunk_sammet584c3a343739 hello-world "/hello" About an hour ago Exited (0) About an hour ago trusting_hypatia005c31a4cd49 hello-world "/hello" About an hour ago Exited (0) About an hour ago goofy_easleyb865dac40a66 hello-world "/hello" About an hour ago Exited (0) About an hour ago furious_borg
3、查看本地镜像
sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEhello-world latest 0a6ba66e537a 8 weeks ago 960 B
4、从镜像中运行/停止一个新实例
sudo docker run/stop --help
通过help查询一些参数,根据自己的id启动/停止实例
注意:
每次 使用docker命令都需要使用sudo
这里把当前用户加入到docker组就可以直接使用命令,而不用每次都加sudo
$ sudo groupadd docker
#改完后需要重新登陆用户
$ sudo gpasswd -a ${USER} docker