一 安装docker
1.0 安装软件包
yum install -y yum-utils device-mapper-persistent-data lvm2
1.1 仓库选择
yum-config-manager --add-repo http://download.docker.com/linux/centos/docker-ce.repo
(中央仓库)
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
(阿里仓库)
1.3 安装以及启动
yum list docker-ce --showduplicates | sort -r
yum install docker-ce-18.03.1.ce
systemctl start docker
systemctl enable docker
docker version
二 安装容器
自定义网桥:
docker network create --subnet=172.18.0.0/24 mynet
2.1mysql:新建mysql 容器(不推荐使用)
docker pull mysql:5.7
docker run --restart=always -p 3306:3306 --name mysql -v /etc/localtime:/etc/localtime -v /usr/data/mysql/conf:/etc/mysql -v /usr/data/mysql/logs:/var/log/mysql -v /usr/data/mysql/data:/var/lib/mysql -v /usr/data/mysql/my.cnf:/etc/mysql/my.cnf -e MYSQL_ROOT_PASSWORD=123456 --network mynet --ip 172.18.0.2 -d mysql:5.7
2.2centos:新建centos容器
docker pull centos
docker run -it --restart=always --privileged -p 56990:22 -p 56991:8080 -p 56992:8081 -p 56993:8082 -p 56994:8083 --name=blog -v /etc/localtime:/etc/localtime -v /data/docker/blog:/home/blog -v /usr/java:/usr/java --network mynet --ip 172.18.0.5 -d centos /usr/sbin/init
配置ssh连接
yum install -y openssh-server
vi /etc/ssh/sshd_config
将UsePAM改为no
systemctl restart sshd
设置密码
passwd root
2.3redis:新建redis容器(不推荐)
docker pull redis
docker run --restart=always -it --name=redis -p 6279:6379 -v /etc/localtime:/etc/localtime -v /usr/data/redis/redis.conf:/etc/redis/redis.conf -v /usr/data/redis/data:/data --network mynet --ip 172.18.0.3 -d redis redis-server --appendonly yes --requirepass "123456"
2.4jenkins:新建jenkins容器
docker pull jenkins/jenkins
docker run --restart=always --name jenkins -p 9050:8080 -v /home/jenkins:/home/jenkins -d jenkins
2.4新建mongo容器(不推荐)
docker pull mongo
docker run -d --restart=always -p 37017:27017 --name mongo -v /etc/localtime:/etc/localtime -v /usr/data/mongo/db:/data/db --network mynet --ip 172.18.0.4 -d mongo --auth
docker exec -it mongo /bin/bash
mongo
use admin
db.createUser({ user: 'root', pwd: '123456', roles: [ { role: "root", db: "admin" } ] });
db.auth('root','123456')
use db
db.createUser({ user: 'root', pwd: '123456', roles: [ { role: "readWrite", db: "库名" } ] });
exit
三 删除
3.1删除容器
关闭容器
docker rm 容器名/ID
3.2删除镜像
docker rmi 镜像ID
其他
1.查看ip
docker inspect 容器名/ID
2.进入容器
docker exec -it -u 0 container /bin/bash
3.修改容器内存占用大小
docker update --memory 2048m --memory-swap -1 container
4.容器和虚拟机时间一致
-v /etc/localtime:/etc/localtime
或者修改:
cd /etc/
mv localtime localtime_bak
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
5.将宿主机 docker.sock 文件挂载到容器
实现容器内 docker 操作宿主机 docker 的目的
docker run -it -v /var/run/docker.sock:/var/run/docker.sock ...
容器创建成功后,可以通过修改配置文件来添加目录挂载,步骤如下:
a.关闭docker
b.cd /var/lib/docker/containers/容器id/
c.vi hostconfig.json,目录绑定:
"Binds": ["/data:/dir"]
d.vi config.v2.json,添加MountPoints:
"MountPoints": {
"/dir": {
"Source": "/data",
"Destination": "/dir",
"RW": true,
"Name": "",
"Driver": "",
"Type": "bind",
"Propagation": "rprivate",
"Spec": {
"Type": "bind",
"Source": "/data",
"Target": "/dir"
},
"SkipMountpointCreation": false
}
}
6.docker 容器添加端口映射
docker创建容器时,通过-p命令添加端口映射,容器创建成功后,可以通过修改配置文件来添加端口映射
步骤:
a.停掉docker服务
systemctl stop docker
b.进入要修改的容器目录
cd /var/lib/docker/containers/{container_id}
c.修改hostconfig.json
修改PortBindings 值。在这个json集合里边追加新端口:
"8888/tcp": [ //容器端口
{
"HostIp": "",
"HostPort": "8888" //宿主机端口
}
],
d.修改config.v2.json
修改ExposedPorts,添加容器新增映射的端口json:
"9050/tcp":{}
e.启动docker服务,启动容器
7.docker使用network
a、创建网络 test-network
docker network create test-network
b、将php容器或其他容器加入到test-network网络
docker network connect test-network php
c、 查看网络
docker network ls
d、 断开和移除网络
移除网络之前需要断开所有连接到网络的容器:docker network disconnet test-network php
移除网络:docker network rm test-network
e、 查看网络内部信息
docker network inspect test-network
8.systemctl报错
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
原因:
docker的bug
解决方法:
docker run ... /usr/sbin/init
注意:本文归作者所有,未经作者允许,不得转载