Ubuntu安装配置Docker和Docker-compose

Docker 知识库

1. Docker

1.1 安装Docker-Engine

1.先查看Ubuntu内核版本 Docker要求Linux的内核版本必须 >= 3.10 如果不满足可能需要升级内核

$ uname -r [unmae -a]

> 4.4.0-45-generic [Linux tang 4.4.0-45-generic #66-Ubuntu SMP Wed Oct 19 14:12:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux]

2.更新apt源

$ sudo apt-get update
$ sudo apt-get install apt-transport-https ca-certificates

3.添加docker官方apt源的公钥 有可能会添加不成功多尝试几次就好

$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

4.查看自己的系统版本和官方提供的仓库地址进行匹配

$ cat /etc/issue
> Ubuntu 16.04.1 LTS \n \l

$ sudo lsb_release -a
> No LSB modules are available.
  Distributor ID:   Ubuntu
  Description:  Ubuntu 16.04.1 LTS
  Release:  16.04
  Codename: xenial

以下是官方给出的不同系统版本对应的仓库地址

Ubuntu version Repository
Precise 12.04 (LTS) deb https://apt.dockerproject.org/repo ubuntu-precise main
Trusty 14.04 (LTS) deb https://apt.dockerproject.org/repo ubuntu-trusty main
Xenial 16.04 (LTS) deb https://apt.dockerproject.org/repo ubuntu-xenial main

5.生成和配置docker.list

$ echo "" | sudo tee /etc/apt/sources.list.d/docker.list

 就是对应的仓库地址
例如:
$ echo "deb https://apt.dockerproject.org/repo ubuntu-xenial main" | sudo tee /etc/apt/sources.list.d/docker.list

6.再次更新apt源

$ sudo apt-get update

7.安装内核补丁(这是docker官方建议的)

Ubuntu Xenial 16.04 (LTS)
Ubuntu Trusty 14.04 (LTS)

$ sudo apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtual

Ubuntu Precise 12.04 (LTS)

$ sudo apt-get install linux-image-generic-lts-trusty

重启系统:

$ sudo reboot

8.安装docker-engine

安装docker
$ sudo apt-get install docker-engine

检查是否安装成功
$ docker -v

启动docker
$ sudo service docker start

hello-world
$ sudo docker run hello-world

9.配置当前用户对docker的操作权限

添加docker用户组(docker在安装的时候已经为我们创建了docker这个用户组,如果没有创建可以使用以下命令创建)
$ sudo groupadd docker

将当前用户/指定用户添加进docker用户组(这样做的目的是为了我们在操作docker的时候不需要使用root权限即每次都添加sudo)
$ sudo usermod -aG docker $USER
例如:
$ sudo usermod -aG docker tang

10.设置docker服务开机启动

针对Ubuntu 16.04版本
sudo systemctl start docker

11.配置Docker 镜像加速器
这里使用的是阿里云Docker 镜像加速器所以直接参看博客Docker 镜像加速器

12.所有的配置完成以后重启系统

$ sudo reboot

参考资料

Installation on Ubuntu
Ubuntu 16.04下Docker的安装与代理配置
Docker 镜像加速器


2. docker-compose

2.1 安装docker-compose

1.首先需要安装pip

$ sudo apt-get -y install python-pip

2.安装Docker-Compose组件

$ sudo pip install docker-compose

2.2 docker-compose一些基本命令

通过进入docker-compose.yml文件所在的目录直接运行 docker-compose 会看到一下输出:

tang@tang:~/DockerProjects/mysql$ docker-compose  
Define and run multi-container applications with Docker.  

Usage:  
  docker-compose [-f ...] [options] [COMMAND] [ARGS...]  
  docker-compose -h|--help  

Options:  
  -f, --file FILE             Specify an alternate compose file (default: docker-compose.yml)  
  -p, --project-name NAME     Specify an alternate project name (default: directory name)  
  --verbose                   Show more output  
  -v, --version               Print version and exit  
  -H, --host HOST             Daemon socket to connect to  

  --tls                       Use TLS; implied by --tlsverify  
  --tlscacert CA_PATH         Trust certs signed only by this CA  
  --tlscert CLIENT_CERT_PATH  Path to TLS certificate file  
  --tlskey TLS_KEY_PATH       Path to TLS key file  
  --tlsverify                 Use TLS and verify the remote  
  --skip-hostname-check       Don't check the daemon's hostname against the name specified  
                              in the client certificate (for example if your docker host  
                              is an IP address)  

Commands:  
  build              Build or rebuild services  
  bundle             Generate a Docker bundle from the Compose file  
  config             Validate and view the compose file  
  create             Create services  
  down               Stop and remove containers, networks, images, and volumes  
  events             Receive real time events from containers  
  exec               Execute a command in a running container  
  help               Get help on a command  
  kill               Kill containers  
  logs               View output from containers  
  pause              Pause services  
  port               Print the public port for a port binding  
  ps                 List containers  
  pull               Pulls service images  
  push               Push service images  
  restart            Restart services  
  rm                 Remove stopped containers  
  run                Run a one-off command  
  scale              Set number of containers for a service  
  start              Start services  
  stop               Stop services  
  unpause            Unpause services  
  up                 Create and start containers  
  version            Show the Docker-Compose version information  

  以上命令根据后面的英文就基本知道其作用了。

常用的一些命令:

$ docker-compose up
$ docker-compose up -d
$ docker-compose ps
$ docker-compose start
$ docker-compose stop

编写Hello-World

1.创建docker-compose.yml 文件

$ nano docker-compose.yml

2.添加docker-compose.yml的内容

hello-world:
  image:hello-world

参考资料

Install Docker Compose
How To Install and Use Docker Compose on Ubuntu 14.04

你可能感兴趣的:(Docker)