Docker

install

wget -qO- https://get.docker.com | sh

config

https://www.jianshu.com/p/95e397570896
``` sh
如果还没有 docker group 就添加一个:
sudo groupadd docker

将用户加入该 group 内。然后退出并重新登录就生效啦。
sudo gpasswd -a ${USER} docker

重启 docker 服务
sudo service docker restart

切换当前会话到新 group 或者重启 X 会话
newgrp - docker

注意:最后一步是必须的,否则因为 groups 命令获取到的是缓存的组信息,刚添加的组信息未能生效,所以 docker images 执行时同样有错。
```

加速器

https://www.daocloud.io
config docker preference(registry-mirrors) :
for ubuntu

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://6cde3c02.m.daocloud.io
sudo systemctl restart docker.service

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://d1d9aef0.m.daocloud.io

示例

1
2
3
4
5
6
7
8
9
10
11
12
docker run hello-world

docker run -it alpine sh

docker images

docker run -d -p 80:80 nginx
docker ps
docker kill __id__

docker run -d -p 9200:9200 elasticsearch
docker logs __id__

Docker 中文教程_Docker 开发中文手册[PDF]下载-极客学院 Wiki

-d: 标示是让 docker 容器在后台运行。
-P: 标示 Docker 所需的端口映射从主机映射到我们的容器内。
-t: 表示在新容器内指定一个伪终端或终端,
-i: 表示允许我们对容器内的 STDIN 进行交互。
-p: 标识来指定容器端口绑定到主机端口 > sudo docker run -d -p 5000:5000 training/webapp python app.py > sudo ocker port nostalgic_morse 5000 > https://wiki.jikexueyuan.com/project/docker/userguide/dockerlinks.html

1
2
3
4
sudo docker run -t -i training/sinatra /bin/bash
sudo docker commit -m="Added json gem" -a="Lou Li" \
9e4dcef3e152 lyloou/sinatra:v2
4f177bd27a9ff0f6dc2a830403925b5360bfe0b93d476f7fc3231110e7f71b1c
1
2
3
4
sudo docker build -t="lyloou/sinatra:v2" .
sudo docker tag 5db5f8471261 lyloou/sinatra:devel
sudo docker push lyloou/sinatra
sudo docker rmi lyloou/sinatra
1
2
3
4
5
6
# http://wiki.jikexueyuan.com/project/docker/examples/nodejs_web_app.html
# run in background
sudo docker run -p 49161:8080 -d lyloou/centos-node-hello:latest

# https://askubuntu.com/questions/505506/how-to-get-bash-or-ssh-into-a-running-container-in-background-mode
sudo docker exec -it f3b /bin/bash

数据持久化

如果需要数据持久化,可以使用数据卷机制。

1
docker run -p 8080:8080 -p 50000:50000 -v /your/home:/var/jenkins_home jenkins

or

1
2
docker volume create --name jenkins_home
docker run -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts

docker-compose

install and run

1
2
3
4
5
pip install docker-compose

# create file docker-compose.yml
# run
docker-compose up

uninstall and remove

1
2
# Stop and remove containers, networks, images, and volumes
docker-compose down
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 获取docker服务地址
[root@iZj6c9zr2mst7l8t6ardbkZ ~]# cat show_ip.sh
#/bin/bash
docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

[root@iZj6c9zr2mst7l8t6ardbkZ ~]# ./show_ip.sh
/nginx - 172.18.0.8
/reward - 172.18.0.4
/web - 172.18.0.14
/app - 172.18.0.7
/check - 172.18.0.13
/exapi - 172.18.0.3
/coin - 172.18.0.12
/api - 172.18.0.5
/trade - 172.18.0.9
/admin - 172.18.0.11
/service - 172.18.0.6
/rabbitmq - 172.18.0.17
/redis2 - 172.18.0.2
/zookeeper - 172.18.0.15
/redis1 - 172.18.0.10
/mysql - 172.18.0.16
/nexus3 - 172.17.0.2

网络管理

Docker 错误集合_挂件-CSDN 博客_error: pool overlaps with other one on this addres
查看 docker 网卡

1
docker network ls

查看 docker 网卡的相关详细信息 确认是自己创建的 ip 段 然后在删除相应的网卡

1
docker network inspect ${containerid}

删除 docker 网卡

1
docker network rm ${containerid}

清除日志

https://stackoverflow.com/a/43570083

1
truncate -s 0 /var/lib/docker/containers/*/*-json.log

查看日志

https://blog.csdn.net/jiangyu1013/article/details/96147534

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
docker logs -f -t --since="2017-05-31" --tail=10 edu_web_1
docker logs --tail 1 26b12d17fefc
docker logs -t 26b12d17fefc

docker logs [OPTIONS] CONTAINER

Options:

--details 显示更多的信息

-f, --follow 实时输出日志,最后一行为当前时间戳的日志

--since string 输出日志开始日期,即只输出指定日期之后的日志。

--tail string 显示最后多少行日志, 默认是all
(如: -tail=10 : 查看最后的 10 行日志。)

-t, --timestamps 显示时间戳

搭建私有仓库

https://yeasy.gitbook.io/docker_practice/repository/registry

停止

docker stop $(docker ps -aq)

docker-compose

1
2
3
4
5
6
docker-compose ps
docker-compose up -d
docker-compose down
docker-compose down -v
docker-compose start ce-kafka
docker-compose exec ce-kafka bash

docker-2021-09-17-10-38-19
https://docs.docker.com/compose/reference/down/

1
2
3
4
# rebuild demo-admin
docker-compose stop demo-admin && docker-compose rm -f demo-admin
docker-compose build demo-admin && docker-compose up -d demo-admin
echo "rebuild demo-admin success."

宿主机与容器之前的复制

容器到宿主机:docker cp container_name:/opt/abc.txt /tmp/
宿主机到容器:docker cp /tmp/abc.txt container_name:/opt/
不论容器是否启动,都可以执行上面的命令

查看容器信息

1
docker inspect aafbdd