youngjuning / issues

一寸欢喜 - 怕什么真理无穷,进一寸有一寸的欢喜
https://youngjuning.js.org
44 stars 4 forks source link

Docker #192

Closed youngjuning closed 5 years ago

youngjuning commented 5 years ago
youngjuning commented 5 years ago
youngjuning commented 5 years ago

Portainer Setup on Windows 10

$ docker volume create portainer_data
$ docker run -d -p 9000:9000 --name portainer --restart always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer

如果不行参考:http://t.cn/ELGQkdAhttp://t.cn/ELGQFk6

youngjuning commented 5 years ago

Docker 容器与宿主机共享时区配置

操作系统的默认时间为 UTC 时间,而在中国区贩卖的云服务器往往不使用 UTC 标准时间,时区通常被默认设置为 Asia/Shanghai,这就造成了 Docker 容器时区跟宿主机不一致的情况。 动手依次修改每个容器的时区显然不是最佳实践,Docker 容器往往只需要将其启动,而不需要继续再对其内部做任何变更。

实际上你只需要为每个容器添加两个卷,将宿主机的时区设置映射到 Docker 容器中即可。

如果你正在使用命令行,可以使用以下命令:

$ docker run -v /etc/localtime:/etc/localtime:ro -v /etc/timezone:/etc/timezone:ro ...

如果你使用 Docker-compose 进行容器构建,可以在docker-compose.yml文件相应应用下添加卷:

volumes:
+ - /etc/localtime:/etc/localtime:ro
+ - /etc/timezone:/etc/timezone:ro

“ro”的意思是只读(read-only)模式,可以保证其挂载卷不被 Docker 容器内部文件系统配置所覆盖。

转载自 https://muguang.me/it/2658.html

youngjuning commented 5 years ago

Docker Ubuntu 初始化

适用于 raspbian、ubuntu mate、ubuntu等

1、安装

最快捷

$ curl -sSL https://get.docker.com | sh

国内镜像

$ curl -fsSL get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh --mirror Aliyun

2、安装后日志分析

// docker客户端
Client:
 Version:           18.09.0
 API version:       1.39
 Go version:        go1.10.4
 Git commit:        4d60db4
 Built:             Wed Nov  7 00:57:21 2018
 OS/Arch:           linux/arm
 Experimental:      false
// docker服务端 - 社区版
Server: Docker Engine - Community
 Engine:
  Version:          18.09.0
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.4
  Git commit:       4d60db4
  Built:            Wed Nov  7 00:17:57 2018
  OS/Arch:          linux/arm
  Experimental:     false

// 如果你不想用root账号使用 Docker,你应该把你的用户加入到 “docker” 分组中(安装过程中已经新建了docker分组)
If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:

  sudo usermod -aG docker your-user

// 记住你必须退出并重新登录才能生效
Remember that you will have to log out and back in for this to take effect!

// 警告:将用户添加到“docker”组将授予运行容器的能力,这些容器可用于获取docker主机上的root权限。
WARNING: 
Adding a user to the "docker" group will grant the ability to run containers which can be used to obtain root privileges on the docker host.
// 参考资源
Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface for more information.

总结:

  1. 一些版本信息
  2. 不想使用root账号操作docker,就在 "docker" 分组下添加user(重启后生效)

3、安装docker-compose

使用Docker的准时轻量级开发环境

$ apt-get install docker-compose

4、添加用户到docker分组【可选】

默认情况下,docker 命令会使用 Unix socket 与 Docker 引擎通讯。而只有 root 用户和 docker 组的用户才可以访问 Docker 引擎的 Unix socket。出于安全考虑,一般 Linux 系统上不会直接使用 root 用户。因此,更好地做法是将需要使用 docker 的用户加入 docker 用户组。

$ sudo usermod -aG docker $USER // 一般是 pi
# 如果没有该分组就新建 sudo groupadd docker

退出当前终端并重新登录

5、启动 Docker CE

$ sudo systemctl enable docker
$ sudo systemctl start docker

6、测试 Docker 是否安装正确

$ docker run hello-world

执行以上命令,若能正常输出以下信息,则说明安装成功。

Unable to find image 'arm32v7/hello-world:latest' locally
latest: Pulling from arm32v7/hello-world
ad0f38092cf2: Pull complete 
Digest: sha256:f1ee3cbf1dbeab65f6b542360aca910ce1f6ba0ff31c803fdf08a13f5f846249
Status: Downloaded newer image for arm32v7/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.
    (arm32v7)
 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://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

7、国内镜像

请在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件)

{
  "registry-mirrors": [
    "https://registry.docker-cn.com"
  ]
}

注意,一定要保证该文件符合 json 规范,否则 Docker 将不能启动。

之后重新启动服务。

$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

配置加速器之后,如果拉取镜像仍然十分缓慢,请手动检查加速器配置是否生效,在命令行执行 docker info,如果从结果中看到了如下内容,说明配置成功。

Registry Mirrors:
 https://registry.docker-cn.com/
youngjuning commented 5 years ago

Portainer

PortainerDocker引擎或Swarm集群上作为轻量级Docker容器(Docker映像权重小于4MB)运行。因此,在使用Docker的任何计算机上运行容器都是一个命令。

部署Portainer

使用以下Docker命令部署Portainer:

$ docker volume create portainer_data
$ docker run -d -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data --name portainer portainer/portainer

您只需要使用浏览器访问运行portainerDocker引擎的端口9000

注意:该-v /var/run/docker.sock:/var/run/docker.sock选项只能在Linux环境中使用。所有 windows 下安装需要 Switch to Linux Containers

youngjuning commented 5 years ago

单独支持树莓派的docker镜像

youngjuning commented 5 years ago

docker容器中查看容器linux版本

有时候需要登陆容器搞点事情,这时候需要看容器系统的版本,那么一条命令就能完成。 正确的姿势:

$ cat /etc/issue

错误的姿势:

cat /proc/versionuname -a ,这样查到的是宿主机的系统。

youngjuning commented 5 years ago

Alpine Linux

Alpine Linux是一个基于安全的轻量级Linux发行版,基于musl libc和busybox。

常用命令

$ apk add --update nano
youngjuning commented 5 years ago

命令索引

youngjuning commented 5 years ago

docker volume

volume 方式是 docker 中数据持久化的最佳方式。

docker 默认在主机上会有一个特定的区域(/var/lib/docker/volumes/),该区域用来存放 volume

非 docker 进程不应该去修改该区域。

volume 可以通过 docker volume 进行管理,如创建(create)、删除(rm)等操作。 volume 在生成的时候如果不指定名称,便会随机生成。

$ ls /var/lib/docker/volumes
ff664768bfe64e1a8cae4369dd4a2e1929362e29580735480290684e38c8f140
ffa4846b581c1a50a01e7a12a6342ad2aaa442701a35ae56ef2f0e5d7888b22c

volume 在容器停止或删除的时候会继续存在,如需删除需要显示声明。

$ docker rm -v <container_id>
$ docker volume rm <volume_name>