安装docker

Debian/Ubuntu 用户

开启 Enable Docker repository

如果已安装Docker,先卸载它

sudo apt-get remove docker docker-engine docker.io

这条命令没验证过,后面有专门的卸载章节

1.首先更新包列表并安装必要的依赖库,以此实现通过HTTPS来添加新的repository

sudo apt update
sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

2.通过curl命令来导入GPG密钥 - ubuntu version:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  • debian version:
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -

3.将Docker APT repository添加到你的系统

对于 amd64 架构的计算机,添加软件仓库:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

可以使用清华镜像

sudo add-apt-repository \
   "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

如果你是树莓派或其它ARM架构计算机,请运行:

echo "deb [arch=armhf] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian \
     $(lsb_release -cs) stable" | \
    sudo tee /etc/apt/sources.list.d/docker.list
安装Docker CE

现在Docker repository已开启,你可以开始安装你想要的Docker版本了

1.使用下面命令安装最新的docker版本,如果你想安装指定版本,跳过这一步到下一步

sudo apt update
sudo apt install docker-ce

2.如果要安装指定版本,首先列出Docker repository里那些可用的版本

apt list -a docker-ce

输出如下

iot@flyway:~$ sudo apt list -a docker-ce
Listing... Done
docker-ce/bionic 5:19.03.1~3-0~ubuntu-bionic amd64
docker-ce/bionic 5:19.03.0~3-0~ubuntu-bionic amd64
docker-ce/bionic 5:18.09.8~3-0~ubuntu-bionic amd64
docker-ce/bionic 5:18.09.7~3-0~ubuntu-bionic amd64
docker-ce/bionic 5:18.09.6~3-0~ubuntu-bionic amd64
docker-ce/bionic 5:18.09.5~3-0~ubuntu-bionic amd64
docker-ce/bionic 5:18.09.4~3-0~ubuntu-bionic amd64
docker-ce/bionic 5:18.09.3~3-0~ubuntu-bionic amd64
docker-ce/bionic 5:18.09.2~3-0~ubuntu-bionic amd64
docker-ce/bionic 5:18.09.1~3-0~ubuntu-bionic amd64
docker-ce/bionic 5:18.09.0~3-0~ubuntu-bionic amd64
docker-ce/bionic 18.06.3~ce~3-0~ubuntu amd64
docker-ce/bionic 18.06.2~ce~3-0~ubuntu amd64
docker-ce/bionic 18.06.1~ce~3-0~ubuntu amd64
docker-ce/bionic 18.06.0~ce~3-0~ubuntu amd64
docker-ce/bionic 18.03.1~ce~3-0~ubuntu amd64

Docker版本显示在第二列,如果你要安装版本18.09.0,可以使用下面命令:

sudo apt install docker-ce=5:18.09.0~3-0~ubuntu-bionic

Docker服务安装完成后会自动启动,通过下面命令验证

sudo systemctl status docker

输出大概如下

● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2019-08-06 00:02:51 MDT; 7s ago
     Docs: https://docs.docker.com
 Main PID: 17333 (dockerd)
    Tasks: 19
   CGroup: /system.slice/docker.service
           ├─17333 /usr/bin/dockerd -H unix://
           └─17517 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8080 -container-ip 1

輸入

docker --version

查看是否安裝成功

iot@flyway:~$ docker --version
Docker version 19.03.1, build 74b1e89

卸载docker:

在删除Docker之前,请先删除所有的containers, images, volumes, and networks

sudo apt purge docker-ce
sudo apt autoremove

安装docker-compose

1.使用curl命令将Docker compose二进制下载到/usr/local/bin:

sudo curl -L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

2.下载完成之后,给compose binary添加执行权限

sudo chmod +x /usr/local/bin/docker-compose

3.通过下面命令确认docker-compose安装成功

iot@flyway:~$ docker-compose version
docker-compose version 1.24.1, build 4667896
docker-py version: 3.7.3
CPython version: 2.7.15+
OpenSSL version: OpenSSL 1.1.1 11 Sep 2018

简易安装

安装环境: Ubuntu 18.04.1

直接使用命令

apt install docker docker-compose

使用docker-compose创建WordPress应用

本节,我们将在Ubuntu 18.04上创建一个多容器的WordPress应用

首先给这个项目创建目录my_app,并进入该目录

mkdir my_app
cd my_app

在该项目目录下创建文件docker-compose.yml

nano docker-compose.yml

添加以下内容

version: '3.3'
services:
  db:
    image: mysql:5.7
    restart: always
    volumes:
      - db_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: wordpress
  wordpress:
    image: wordpress
    restart: always
    volumes:
      - ./wp_data:/var/www/html
    ports:
      - "8080:80"
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: password
    depends_on:
       - db
volumes:
    db_data:
    wp_data:

现在,我们逐行来分析这些代码

第一行,我们指定了compose文件版本。

接下来,我们定义了两个服务,dbwordpress, 每个服务运行一个image,并在docker-compose运行时创建一个独立的容器

db服务:

  • 使用mysql:5.7, 如果系统上没有这个image,它会从Docker Hub public repository里去拉取
  • 使用always重启策略 policy
  • 创建 volume db_data 来实现持久数据库 Creates a named volume db_data to make the database persistent.
  • mysql:5.7定义环境变量

wordpress服务:

  • 使用wordpress image, 如果系统上没有这个image,它会从Docker Hub public repository里去拉下来
  • Mounts the wp_data directory on the host to /var/lib/mysql inside the container.
  • 将容器上的exposed端口80转发到host主机8080端口
  • 为image wordpress定义环境变量
  • depends_on说明书定义了两个服务之间的依赖,就这个例子来说,db会在wordpress之前启动.

在项目目录下,执行下面命令启动WordPress应用

docker-compose up

输出如下

iot@flyway:~/my_app$ sudo docker-compose up
Creating network "myapp_default" with the default driver
Creating volume "myapp_db_data" with default driver
Creating volume "myapp_wp_data" with default driver
Pulling db (mysql:5.7)...
5.7: Pulling from library/mysql
0a4690c5d889: Pull complete
......
2406ac6e266f: Pull complete
Digest: sha256:540488d8f0e04c1077d17934d1c1511fe417e2221dff508ce4621f5efe6131db
Status: Downloaded newer image for mysql:5.7
Pulling wordpress (wordpress:latest)...
latest: Pulling from library/wordpress
f5d23c7fed46: Pull complete
.............
0d7f34f2c87d: Pull complete
Digest: sha256:acf0e97b2cb3f780fb7944036e1c57bca4da3c2b9b608b818f6bb6c5409be459
Status: Downloaded newer image for wordpress:latest
Creating myapp_db_1 ...
Creating myapp_db_1 ... done
Creating myapp_wordpress_1 ...
Creating myapp_wordpress_1 ... done
Attaching to myapp_db_1, myapp_wordpress_1
....
wordpress_1 | [Tue Aug 06 03:30:47.087261 2019] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.3.8 configured -- resuming normal operations
wordpress_1 | [Tue Aug 06 03:30:47.087722 2019] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'

Compose 会把两个image都拉到本地, 启动两个容器并在项目目录下创建wp_data目录.

在你的浏览器访问http://127.0.0.1:8080/,就可以看到wordpress的安装界面.

至此,wordpress应用已启动并运行起来了,你可以继续去完成主题和插件等

CTRL+C命令可以停止compose

你可以通过添加flag -d, 使用detached mode来启动compose

docker-compose up -d

iot@flyway:~/my_app$ sudo docker-compose up -d
Starting myapp_db_1 ...
Starting myapp_db_1 ... done
Starting myapp_wordpress_1 ...
Starting myapp_wordpress_1 ... done

如果要查看运行服务,使用-p选项

docker-compose ps

iot@flyway:~/my_app$ sudo docker-compose ps
      Name Command State Ports
---------------------------------------------------------------------------------
myapp_db_1 docker-entrypoint.sh mysqld Up 3306/tcp, 33060/tcp
myapp_wordpress_1 docker-entrypoint.sh apach ... Up 0.0.0.0:8080->80/tcp

当compose运行在detached mode时,使用下面命令终止服务

docker-compose stop

如果你想完全移除容器,使用down选项

docker-compose down

添加--volumes可以同时移除data volumes

卸载Docker Compose

sudo rm /usr/local/bin/docker-compose (手册)

sudo rm /usr/bin/docker-compose

常见问题

ERROR: yaml.scanner.ScannerError: mapping values are not allowed here in "./docker-compose.yml", line 611, column 26
no acceptable C compiler found in $PATH when installing python

https://stackoverflow.com/questions/19816275/no-acceptable-c-compiler-found-in-path-when-installing-python

Debian base:

apt-get install build-essential

如何判断版本

iot@flyway:/$ cat /proc/version

Linux version 4.15.0-29-generic (buildd@lgw01-amd64-057) (gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3)) #31-Ubuntu SMP Tue Jul 17 15:39:52 UTC 2018

参考文档

  • https://linuxize.com/post/how-to-install-and-use-docker-on-ubuntu-18-04/
  • https://linuxize.com/post/how-to-install-and-use-docker-compose-on-ubuntu-18-04/
  • https://mirrors.tuna.tsinghua.edu.cn/help/docker-ce/