docker 学习记录

docker 学习记录

docker 入门

https://labs.play-with-docker.com 4小时免费

docker 详解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 进入容器
$ docker exec -it 容器id bash
# exit 退出
# 强制删除容器
$ docker rm -f 容器id
# 提交镜像
$ docker commit 容器id 镜像名称:版本号
$ docker commit -a "author" -m "my xxx" 9sfsuiy734jd mynginx:v1
$ docker push mynginx:v1
# 查看镜像
$ dockers images
$ docker tag user1/xxx:tag user2/xxx:tag
$ docker push user2/xxx:tag
# 构建镜像
$ nano Dockerfile
$ docker build -t 镜像名称:版本号 .
# 使用 URL github.com/creack/docker-firefox 的 Dockerfile 创建镜像
$ docker build github.com/creack/docker-firefox
# 镜像打包
$ docker save 镜像名称 >1.tar
# 删除镜像
$ docker rmi 镜像名称
# 加载镜像
$ docker load 1.tar

docker 安装

国外

docker 安装

https://docs.docker.com/install/linux/docker-ce/centos/

1
2
3
curl -ssL https://get.docker.com/ | sh
systemctl enable docker
systemctl start docker

docker-compose 安装

https://docs.docker.com/compose/install/#install-compose

1
2
sudo curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
1
2
3
4
docker-compose --version
docker-compose up -d
docker-compose stop
docker-compose down

国内

docker 安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 删除
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

# 安装依赖
sudo yum install -y yum-utils device-mapper-persistent-data lvm2

# 开启ce
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

# 使用阿里的加速
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 安装
sudo yum install -y docker-ce docker-ce-cli containerd.io

# 开启服务
sudo systemctl enable docker
sudo systemctl start docker

# 添加组
sudo groupadd docker
sudo usermod -aG docker ${USER}

# 重新登录
logout

# 安装完docker,最好改为国内镜像,http://hub-mirror.c.163.com是docker公司专门为国内用户准备的!
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors":[
"http://hub-mirror.c.163.com"
],
"dns":[
"114.114.114.114",
"8.8.8.8"
]
}
EOF


sudo pkill -SIGHUP dockerd
# 或者
sudo systemctl daemon-reload
sudo systemctl restart docker

# 还有很多国内加速的服务器可以用,比如
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://3laho3y3.mirror.aliyuncs.com",
"https://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn",
"http://f1361db2.m.daocloud.io",
"https://registry.docker-cn.com",
"https://dockerhub.azk8s.cn"
]
}
EOF

# 一些常用的加速资源
docker run -it --rm=true --name=test alpine:3.10 ash

sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/alpine/g' /etc/apk/repositories

npm config set registry https://registry.npm.taobao.org/
composer config -g repo.packagist composer https://packagist.phpcomposer.com
pip install -i https://mirrors.aliyun.com/pypi/simple --upgrade pip docker-compose
pip install -i https://pypi.doubanio.com/simple/ --upgrade pip docker-compose
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

# k8s
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

yum install -y kubelet kubeadm kubectl
systemctl enable kubelet && systemctl start kubelet

docker-compose 安装

1
2
3
sudo yum install -y epel-release
sudo yum install -y python-pip
pip install -i https://pypi.doubanio.com/simple/ --upgrade pip docker-compose

常规命令语法

删除 exited 容器

1
docker rm $(docker ps -a | grep "Exited" | awk '{print $1 }')

删除 none 镜像

1
docker rmi $(docker images | grep "none" | awk '{print $3}')

进入容器

1
2
docker exec -it "容器name" bash
docker exec -it "容器id" sh

其它命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 构建建启动nignx容器
$ docker-compose up -d nginx
# 登录到nginx容器中
$ docker-compose exec nginx bash
# 删除所有nginx容器,镜像
$ docker-compose down
# 显示所有容器
$ docker-compose ps
# 重新启动nginx容器
$ docker-compose restart nginx
# 在 php-fpm 中不启动关联容器,并容器执行 php -v 执行完成后删除容器
$ docker-compose run --no-deps --rm php-fpm php -v
# 构建镜像
$ docker-compose build nginx
# 不带缓存的构建
$ docker-compose build --no-cache nginx
# 查看nginx的日志
$ docker-compose logs nginx
# 查看nginx的实时日志
$ docker-compose logs -f nginx
# 验证(docker-compose.yml)文件配置,当配置正确时,不输出任何内容,当文件配置错误,输出错误信息
$ docker-compose config -q
# 以 json 的形式输出nginx的docker日志
$ docker-compose events --json nginx
# 暂停nignx容器
$ docker-compose pause nginx
# 恢复ningx容器
$ docker-compose unpause nginx
# 删除容器(删除前必须关闭容器)
$ docker-compose rm nginx
# 停止nignx容器
$ docker-compose stop nginx
# 启动nignx容器
$ docker-compose start nginx