-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
root
committed
Dec 31, 2019
1 parent
9fb9922
commit 77e595b
Showing
4 changed files
with
341 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
### 创建镜像 | ||
|
||
创建docker镜像有两种方式: | ||
|
||
1. 从已经创建的容器镜像中更新镜像,并提交这个镜像 | ||
2. 使用Dockerfile构建镜像 | ||
|
||
### 使用已存在的镜像进行构建 | ||
|
||
首先对已存在的镜像进行更新: | ||
|
||
```shell | ||
docker run --name gitimg -it centos /bin/bash | ||
``` | ||
|
||
在容器的可交互shell中进行修改操作: | ||
|
||
```shell | ||
yum install -y git | ||
``` | ||
|
||
使用`exit`退出交互界面,同时对修改后的容器进行提交操作: | ||
|
||
```shell | ||
docker commit -a "feb13th" -m "mygitimage" gitimg feb13th/git:latest | ||
``` | ||
|
||
使用`docker images`可以看到`feb13th/git`这个镜像。 | ||
|
||
### 使用Dockerfile构建 | ||
|
||
首先创建一个文件夹,并创建一个`Dockerfile`文件 | ||
|
||
```shell | ||
mkdir git | ||
cd git | ||
touch Dockerfile | ||
``` | ||
|
||
修改`Dockerfile`文件 | ||
|
||
```dockerfile | ||
# 创建一个简单的git镜像 | ||
FROM centoslatest | ||
# 维护者的信息 | ||
MAINTAINER feb13th [email protected] | ||
# 执行指令 | ||
RUN yum update -y | ||
RUN yum install -y git | ||
``` | ||
|
||
在当前目录中执行(注意最后的点): | ||
|
||
```shell | ||
docker build -t="feb13th/git:latest" . | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
## Docker 基础命令 | ||
|
||
### docker run | ||
|
||
运行一个镜像, 如果本地镜像库中不存在则从远程仓库下载 | ||
|
||
```shell | ||
docker run -i -t --name NAME centos:latest /bin/bash | ||
``` | ||
|
||
* `-i` : 指定可以和容器进行交互 | ||
* `-t` : 打开一个shell窗口 | ||
* `-P` : 自动将容器内需要的端口映射到宿主机动态端口,自动映射的端口范围为49000-49900 | ||
* `-p` : `-p 5000`将容器的5000端口映射到宿主机动态端口,`-p 6000:5000`将容器5000端口映射到宿主机6000端口上 | ||
* `--name` : 指定当前容器的名字 | ||
|
||
### docker ps | ||
|
||
列出容器信息 | ||
|
||
```shell | ||
docker ps -al NAME | ||
``` | ||
|
||
* `-a` : 显示所有的容器信息, 不适用该参数仅显示存活的容器信息 | ||
* `-l` : 显示最后创建的容器 | ||
|
||
### docker logs | ||
|
||
显示容器的标准输出 | ||
|
||
```shell | ||
docker logs -f NAME | ||
``` | ||
|
||
* `-f` : 类似于`tail -f`,追踪文件最新变化 | ||
|
||
### docker create | ||
|
||
创建一个容器 | ||
|
||
```shell | ||
docker create --name NAME centos:latest /bin/bash | ||
``` | ||
|
||
### docker start | ||
|
||
启动创建好的容器 | ||
|
||
```she | ||
docker start NAME | ||
``` | ||
|
||
### docker stop | ||
|
||
停止正在运行的容器 | ||
|
||
```sh | ||
docker stop NAME | ||
``` | ||
|
||
### docker rm | ||
|
||
移除已停止运行的容器 | ||
|
||
```shell | ||
docker rm -vf NAME | ||
``` | ||
|
||
* `-v` : 移除容器的同时移除容器关联的卷信息 | ||
* `-f` : 强制删除一个正在运行的容器(可能会丢失信息) | ||
|
||
### docker images | ||
|
||
显示本地仓库中已存在的镜像 | ||
|
||
```shell | ||
docker images -aq | ||
``` | ||
|
||
* `-a` : 显示所有镜像 | ||
* `-q` : 仅显示镜像id | ||
|
||
### docker rmi | ||
|
||
删除本地仓库镜像 | ||
|
||
```shell | ||
docker rmi -f IMAGE | ||
``` | ||
|
||
* `-f` : 强制删除镜像 | ||
|
||
### docker port | ||
|
||
查看端口的映射信息 | ||
|
||
```shell | ||
docker port NAME [port] | ||
``` | ||
|
||
**port**为容器内的端口,不存在时显示所有的容器到主机端口的映射,否则仅显示指定的端口到主机的映射。 | ||
|
||
### docker top | ||
|
||
查看容器内所有的进程 | ||
|
||
```shell | ||
docker top NAME | ||
``` | ||
|
||
### docker inspect | ||
|
||
```shell | ||
docker inspect -f {{.Config.Hostname}} NAME | ||
``` | ||
|
||
仅使用`docker inspect NAME`会显示容器的源数据信息 | ||
|
||
* `-f` : 使用go模板匹配查找项 | ||
|
||
### docker pull | ||
|
||
从远程仓库下载镜像到本地仓库 | ||
|
||
```shell | ||
docker pull IMAGE | ||
``` | ||
|
||
### docker search | ||
|
||
从远程仓库中搜索镜像 | ||
|
||
```shell | ||
docker search IMAGES | ||
``` | ||
|
||
### docker commit | ||
|
||
将修改后的镜像提交为一个新的镜像 | ||
|
||
```shell | ||
docker commit -a "" -m "" ID OURUSER/OURIMAGE:TAG | ||
``` | ||
|
||
ID: 被修改的镜像id, OURUSER:当前用户名称,OURIMAGE:镜像名, TAG:标签 | ||
|
||
* -a` : 指定作者信息 | ||
* `-m` : 指定提交的信息 | ||
|
||
### docker build | ||
|
||
构建镜像 | ||
|
||
```shell | ||
docker build -t="OURUSER/OURIMAGE:TAG" | ||
``` | ||
|
||
* `-t` : 指定名称和一个可选的版本号标签, 格式必须为 name:tag | ||
|
||
### docker tag | ||
|
||
给已存在的镜像添加标签。 | ||
|
||
```shell | ||
docker tag ID OURUSER/OURIMAGE:TAG | ||
``` | ||
|
||
给id为`ID`的镜像添加一个`OURUSER/OURIMAGE:TAG`的标签 | ||
|
||
### docker push | ||
|
||
推送镜像到docker hub | ||
|
||
```shell | ||
docker push OURUSER/OURIMAGE | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
## docker 镜像下载加速 | ||
|
||
由于docker hub服务器部署在国外,通过国内直接访问可能会出现无法访问或者访问过慢的问题,我们可以通过以下两种方式加速下载。 | ||
|
||
|
||
|
||
### 使用第三方加速 | ||
|
||
我们可以通过使用阿里云镜像加速服务器来加快我们的下载速度 | ||
|
||
一、首先,访问[阿里镜像加速服务器](https://cr.console.aliyun.com/undefined/instances/mirrors),使用账户密码登陆后会获取到加速地址,如:`https://3cassqq.mirror.aliyuncs.com` | ||
|
||
二、在docker的宿主机上修改`/etc/docker/daemin.json`文件 | ||
|
||
```json | ||
{ | ||
"registry-mirrors": ["https://3cassqq.mirror.aliyuncs.com"] | ||
} | ||
``` | ||
|
||
三、重启docker | ||
|
||
```shell | ||
systemctl daemon-reload | ||
systemctl restart docker | ||
``` | ||
|
||
|
||
|
||
### 配置http代理 | ||
|
||
docker守护进程在启动的使用使用`HTTP_PROXY`,`HTTPS_PROXY`和`NO_PROXY`变量来配置http或https的代理行为。我们不能在`daemon.json`文件中配置这些变量。取而代之的是,我们可以配置docker systemd服务文件。 | ||
|
||
1.为docker服务创建一个目录 | ||
|
||
```shell | ||
$ mkdir -p /etc/systemd/system/docker.service.d | ||
``` | ||
|
||
2.创建一个`/etc/systemd/system/docker.service.d/http-proxy.conf`文件,并在该文件中添加以下内容 | ||
|
||
```shell | ||
[Service] | ||
Environment="HTTP_PROXY=http://proxy.example.com:80/" | ||
``` | ||
|
||
同样的如果你需要https的代理,那么需要创建`/etc/systemd/system/docker.service.d/https-proxy.conf`,并在该文件中添加以下内容 | ||
|
||
```shell | ||
[Service] | ||
Environment="HTTPS_PROXY=https://proxy.example.com:443/" | ||
``` | ||
|
||
3.如果有些内容不需要通过连接,那么我们需要添加`NO_PROXY`变量来使这些请求不经过代理 | ||
|
||
`NO_PROXY`变量使用一个包含了 **,** (逗号)分割的特殊字符串来从代理中排除指定的请求,可用于排除的规则如下: | ||
|
||
* ip地址前缀 (1.2.3.4) | ||
* 域名或特殊的DNS标签(*) | ||
* 根据域名或所有的子域名进行匹配。一个以 **.** 开头匹配模式可用于匹配所有的子域名。例如,给定了`foo.example.com`和`example.com`这两个域名: | ||
* `example.com`匹配`example.com`和`foo.example.com` | ||
* 而`.example.com`仅能匹配`foo.example.com` | ||
* 单个 **\*** 可用于所有的代理都不会生效 | ||
* 根据特殊的端口数字进行排除,如ip地址前缀(1.2.3.4:80)和域名(foo.example.com:80) | ||
|
||
配置的例子: | ||
|
||
HTTP | ||
|
||
```shell | ||
[Service] | ||
Environment="HTTP_PROXY=http://proxy.example.com:80/" "NO_PROXY=localhost,127.0.0.1,docker-registory.example.com,.corp" | ||
``` | ||
|
||
HTTPS | ||
|
||
```shell | ||
[Service] | ||
Environment="HTTPS_PROXY=https://proxy.example.com:443/" "NO_PROXY=localhost,127.0.0.1,docker-registory.example.com,.corp" | ||
``` | ||
|
||
4.刷新配置 | ||
|
||
```shell | ||
$ systemctl daemon-reload | ||
``` | ||
|
||
5.重启docker | ||
|
||
```shell | ||
$ systemctl restart docker | ||
``` | ||
|
||
6.验证修改是否生效 | ||
|
||
``` | ||
$ systemctl show --property=Environment docker | ||
``` | ||
|
||
出现上面配置的信息,说明配置成功。 |