Docker安装

当ubuntu版本在14.0.1以上则可以直接安装docker,因为ubuntu系统中自带docker安装包,但是由于有其他软件占据了“docker”这个名称,所以ubuntu的软件仓库中docker叫做“docker.io”。

1.系统自带方式安装

1、用系统自带的软件包安装docker:

  $ sudo apt-get update
  $ sudo apt-get install docker.io
Docker安装_第1张图片

2、如果不是root用户则需要使用sudo,否则会出现以下错误:

Docker安装_第2张图片

想要某个非root用户免sudo使用docker,则将该用户加入docker用户组中:

$ sudo gpasswd –a [用户名] docker
$ sudo service docker restart
$ newgrp – docker

最后一步非常有必要,因为group命令是获取缓存的组信息,刚生成的组信息不会即时生效,所以需要切换会话到新的group。

Docker安装_第3张图片

2.使用Docker源安装

一般ubuntu系统所带的docker版本相对比较低,想要安装新版的docker需要使用docker源安装。

1、使用docker源进行安装需要apt-transport-https的支持,所以首先安装apt-transport-https

$ sudo apt-get install apt-transport-https

2、将docker官网资料库的访问key添加到本地系统

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9

3、将最新的docker源添加到本地docker.list中

$  sudo bash -c "echo deb https://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"

4、安装docker

$ sudo apt-get update
$ sudo apt-get install lxc-docker
Docker安装_第4张图片

3.docker-enter安装

1、安装nsenter

apt-get install build-essential
wget  https://www.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.tar.gz
tar zxvf util-linux-2.24.tar.gz
cd util-linux-2.24 
./configure --without-ncurses 
make nsenter 
cp nsenter /usr/local/bin

2、在/usr/local/bin下创建shell脚本 docker-enter,将如下代码保存为docker-enter

#!/bin/sh

if [ -e $(dirname "$0")/nsenter ]; then
# with boot2docker, nsenter is not in the PATH but it is in the same folder
NSENTER=$(dirname "$0")/nsenter

else
NSENTER=nsenter

fi

if [ -z "$1" ]; then
echo "Usage: `basename "$0"` CONTAINER [COMMAND [ARG]...]"
echo ""
echo "Enters the Docker CONTAINER and executes the specified COMMAND."
echo "If COMMAND is not specified, runs an interactive shell in CONTAINER."
else
PID=$(docker inspect --format "{{.State.Pid}}" "$1")

if [ -z "$PID" ]; then
exit 1

fi
shift

OPTS="--target $PID --mount --uts --ipc --net --pid --"

if [ -z "$1" ]; then
# No command given.
# Use su to clear all host environment variables except for TERM,
# initialize the environment variables HOME, SHELL, USER, LOGNAME, PATH,
# and start a login shell.
"$NSENTER" $OPTS su - root

else
# Use env to clear all host environment variables.
"$NSENTER" $OPTS env --ignore-environment -- "$@"
fi
fi

3、修改docker-enter脚本的权限

chomod +x docker-enter

4、运行 docker-enter ,这样就进入到指定的容器中

你可能感兴趣的:(Docker安装)