🌈初探Docker
初探 Docker.
名词引入
- 首先 - Docker是啥就不解释了.
- 初探尝试安装时出现下面好几个选择,于是搜了搜~ 
- 找到 docker 家族有几个特殊的工具 - docker-Dockerfile,- docker-compose,- docker-machine
docker-Dockerfile
Docker Dockerfile
Dockerfile 是一个用来构建镜像的文本文件,文本内容包含了一条条构建镜像所需的指令和说明。
docker-compose
docker 与 docker-compose 区别
docker 是一个容器,你如果要是安装服务也是要单独一个一个的安装,如果你使用了 docker-compose 的话 就可以使用一个 docker-compose.yml 脚本一键安装
说来其是管理同一个 docker 下多个服务的.
docker-machine
Docker 三剑客之 docker-machine
这个是用来管理多个 docker 的.
安装启动
Manjaro
Windows
- 在 - Windows上安装就比较复杂了,牵扯到很多东西.
- Docker 基于 Linux 系统实现,所以无法直接安装在 Windows 上,需要虚拟化支持. - 这就需要借助wsl或者Virtual box或者hyper-v了(建议用 wsl)
 - 这里提一下,如果想用 hyper-v 引擎,只需要勾选hyper-v功能就行,wsl 的那两个不用开(当然开了对 docker 也没影响)
 
- 这就需要借助
- 安装 docker 前,需要装好 wsl,下面文章到 - 最后安装系统之前,都需要过一遍才能装好 wsl2
- 装好 wsl 后去 docker 官网下载 - Docker desktop for window,安装.
- 装好 docker 之后 - 重启才能正常使用!
Ubuntu-Server
- Ubuntu-Server装系统时就带有- Docker安装选项,直接安装即可,但是会遇到下面权限问题及解决办法
- 安装后开机检测- weidows@ubuntu-server:~$ docker 
 Usage: docker [OPTIONS] COMMAND
 A self-sufficient runtime for containers
 Options:
 --config string Location of client config files (default "/home/weidows/snap/docker/471/.docker")
 -c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
 -D, --debug Enable debug mode
 -H, --host list Daemon socket(s) to connect to
 -l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
 --tls Use TLS; implied by --tlsverify
 --tlscacert string Trust certs signed only by this CA (default "/home/weidows/snap/docker/471/.docker/ca.pem")
 --tlscert string Path to TLS certificate file (default "/home/weidows/snap/docker/471/.docker/cert.pem")
 --tlskey string Path to TLS key file (default "/home/weidows/snap/docker/471/.docker/key.pem")
 --tlsverify Use TLS and verify the remote
 -v, --version Print version information and quit
 Management Commands:
 builder Manage builds
 config Manage Docker configs
 container Manage containers
 context Manage contexts
 engine Manage the docker engine
 image Manage images
 network Manage networks
 node Manage Swarm nodes
 plugin Manage plugins
 secret Manage Docker secrets
 service Manage services
 stack Manage Docker stacks
 swarm Manage Swarm
 system Manage Docker
 trust Manage trust on Docker images
 volume Manage volumes
 Commands:
 attach Attach local standard input, output, and error streams to a running container
 build Build an image from a Dockerfile
 commit Create a new image from a container's changes
 cp Copy files/folders between a container and the local filesystem
 create Create a new container
 deploy Deploy a new stack or update an existing stack
 diff Inspect changes to files or directories on a container's filesystem
 events Get real time events from the server
 exec Run a command in a running container
 export Export a container's filesystem as a tar archive
 history Show the history of an image
 images List images
 import Import the contents from a tarball to create a filesystem image
 info Display system-wide information
 inspect Return low-level information on Docker objects
 kill Kill one or more running containers
 load Load an image from a tar archive or STDIN
 login Log in to a Docker registry
 logout Log out from a Docker registry
 logs Fetch the logs of a container
 pause Pause all processes within one or more containers
 port List port mappings or a specific mapping for the container
 ps List containers
 pull Pull an image or a repository from a registry
 push Push an image or a repository to a registry
 rename Rename a container
 restart Restart one or more containers
 rm Remove one or more containers
 rmi Remove one or more images
 run Run a command in a new container
 save Save one or more images to a tar archive (streamed to STDOUT by default)
 search Search the Docker Hub for images
 start Start one or more stopped containers
 stats Display a live stream of container(s) resource usage statistics
 stop Stop one or more running containers
 tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
 top Display the running processes of a container
 unpause Unpause all processes within one or more containers
 update Update configuration of one or more containers
 version Show the Docker version information
 wait Block until one or more containers stop, then print their exit codes
 Run 'docker COMMAND --help' for more information on a command.
 weidows@ubuntu-server:~$
解决权限问题
- 我安装的 - Ubuntu-Server没有- root用户,会导致 docker 启动异常.
- 用 - docker version检查- 最后一行会报错这样
 - Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.26/images/json: dial unix /var/run/docker.sock: connect: permission denied 
- 解决方案 - sudo groupadd docker #添加docker用户组 
 sudo gpasswd -a $USER docker #将当前用户添加至docker用户组
 newgrp docker #更新docker用户组
加速
镜像加速
阿里的镜像服务虽然是能访问, 但是已经停止更新了…
- 登录阿里云并找到 - 容器镜像服务
- 阿里会给一个加速地址,把地址复制到 Docker 设置里就可. 
Windows

Manjaro
- 新建或者修改 - /etc/docker/daemon.json- { 
 "registry-mirrors": ["https://docker.hub.weidows.tech"]
 }
- 然后重启 docker 服务 - sudo systemctl daemon-reload 
 sudo systemctl restart docker
设置代理
docker-本身代理
国内网络环境拉取镜像非常慢,挂代理可以快很多. (不过一般配置了上面的镜像加速就用不着配置代理了)
docker desktop 设置里操作, localhost:7890
docker-容器使用宿主机代理
比如在 docker 里跑了虚拟机 (docker-osx, 默认是桥接网络实际用起来是没有科学的
可以选择在创建容器时 --net=host 使用主机网络, 但不如:
host.docker.internal 指向宿主机内网 IP, proxy 可以直接写为 host.docker.internal:7890
docker 管理
VScode-docker
- 在 Manjaro 虚拟机中启动 docker 服务,然后用 vscode-ssh 连接 manjaro 
- 安装 docker 插件,就可以享受比较方便的可视化了(个人非常喜欢这种方式) 
- 另外再安装 - Resource Monitor这个插件可以监控远程机资源消耗(如上图最下方)
Portainer
- docker 的可视化工具,可连接本地和远程,支持集群,有很多部署方式,这里选择直接在 docker 部署. 
- 首先它需要一个 - volume(数据卷),新建- docker volume create portainer_data 
- 然后拉取并运行 - 需要保证 9000 端口没被占用,不然无法运行
 - docker run -d --name portainer --restart always -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer 
- 浏览器打开 - localhost:9000
- 官网上的版本要比 github 的 tag 慢一些,左下角提示更新可以无视~ 
常用命令
命令行执行
有些文档的命令是这样, 行末的 '' 是 shell 的换行符, windows 不支持, 删掉就好
docker run -d \
  -p 8088:8088 \
  --name next-terminal \
  --restart always ghcr.io/dushixiang/next-terminal:latest
docker run -d -p 8088:8088 --name next-terminal --restart always ghcr.io/dushixiang/next-terminal:latest
docker 自启
- 启动服务 - sudo systemctl start docker.service 
- 开机自动启动 docker 服务 - sudo systemctl enable docker.service 
- 重新载入配置 - sudo systemctl daemon-reload 
- 重启 docker - sudo systemctl restart docker 
容器自启
| # 开启 | 
使用案例
多数据库管理
- 开发时需要好几个不同的数据库 - 在 win 主机上安装配置的话,必然各种 bug,大部分时间还空浪费性能 
- 于是考虑在虚拟机上装,但是鉴于各种系统环境还不一样,每个数据库挨个配置和解决 bug 都废手 
- 于是让数据库进入 docker 
 
- 通过 - docker-compose.yml一条龙管理 (免去了每个数据库安装,开通远程连接,配置账户这些麻烦)- version: "3.1" 
 services:
 # redis 无密码
 redis:
 image: redis:latest
 restart: always
 container_name: "redis-app"
 command: redis-server /usr/local/etc/redis/redis.conf
 ports:
 - 6379:6379
 volumes:
 - ./redis.conf:/usr/local/etc/redis/redis.conf
 - /data/redis:/data
 # mongo root-2333
 mongo:
 image: mongo:latest
 restart: always
 container_name: "mongo-app"
 environment:
 MONGO_INITDB_ROOT_USERNAME: root
 MONGO_INITDB_ROOT_PASSWORD: 2333
 ports:
 - 27017:27017
 volumes:
 - /data/mongo:/etc/mongo
 # mariadb root-2333
 mariadb:
 image: mariadb:latest
 restart: always
 container_name: "mariadb-app"
 ports:
 # 端口映射
 - 3306:3306
 volumes:
 # 容器与宿主机时间同步
 - /etc/localtime:/etc/localtime
 # 数据库目录映射
 - ./config/mariadb:/var/lib/mysql
 # (推荐)如果要使用自定义的MySQL配置,则可以在主机上的目录中创建备用配置文件,然后将该目录位置/etc/mysql/conf.d安装在mariadb容器内。自己所需的配置文件可以放在自己服务器./config/data/mariadb-config里面
 - ./config/mariadb-config:/etc/mysql/conf.d
 environment:
 TIME_ZONE: Asia/Shanghai
 MYSQL_ROOT_PASSWORD: 2333- 进此配置目录执行 - docker-compose up, ->最新文件地址<-
docker-osx
记一下我做的配置更改, 备忘
- 设置-网络-代理- host.docker.internal:7890
 
- 修改 /etc/hosts- github520
 
- 安装 homebrew













