[example@centosali ~]$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
[example@centosali ~]$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
[example@centosali ~]$ sudo yum update
[example@centosali ~]$ sudo yum install docker-ce docker-ce-cli containerd.io
# 查看安装结果
[example@centosali ~]$ yum list docker-ce --showduplicates | sort -r
已加载插件:fastestmirror
已安装的软件包
可安装的软件包
Loading mirror speeds from cached hostfile
docker-ce.x86_64 3:18.09.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.3-3.el7 @docker-ce-stable
docker-ce.x86_64 3:18.09.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable
# 查看 docker 状态
[example@centosali ~]$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: https://docs.docker.com
# 设置开机启动
[example@centosali ~]$ sudo systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
# 启动docker
[example@centosali ~]$ sudo systemctl start docker
#docker 已启动成功
[example@centosali ~]$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since 六 2019-03-16 10:50:35 CST; 2min 17s ago
Docs: https://docs.docker.com
Main PID: 14096 (dockerd)
Tasks: 8
Memory: 31.9M
CGroup: /system.slice/docker.service
└─14096 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
# 执行下 docker ps 瞅瞅
[example@centosali ~]$ docker ps
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.39/containers/json: dial unix /var/run/docker.sock: connect: permission denied
# 呀, docker不是已经启动了
# 将example用户加入docker 组 再重新登录即可
[example@centosali ~]$ sudo usermod -a -G docker example
[example@centosali ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 在 /etc/docker/ 目录下新建文件 daemon.json
[example@centosali ~]$ sudo cat /etc/docker/daemon.json
{
"bip": "172.17.42.1/24",
"dns": [
"172.17.42.1",
"114.114.114.114"
],
"registry-mirrors": [
"https://reg-mirror.qiniu.com",
"https://hub-mirror.c.163.com",
"https://registry.aliyuncs.com"
],
"hosts" : ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2375"]
}
# 编辑/usr/lib/systemd/system/docker.service 文件
注释行: ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
新增行:ExecStart=/usr/bin/dockerd
# 重新加载配置
[example@centosali ~]$ sudo systemctl daemon-reload
# 启动docker
[example@centosali ~]$ sudo systemctl start docker
# 查看到docker 服务状态
[example@centosali ~]$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since 六 2019-03-16 11:18:53 CST; 6s ago
[example@centosali ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 检查 2375 端口状态
[example@centosali ~]$ sudo netstat -antp | grep 2375
tcp6 0 0 :::2375 :::* LISTEN 15291/dockerd
[example@centosali ~]$ docker network --help
Usage: docker network COMMAND
Manage networks
Commands:
connect Connect a container to a network
create Create a network
disconnect Disconnect a container from a network
inspect Display detailed information on one or more networks
ls List networks
prune Remove all unused networks
rm Remove one or more networks
创建自定义网桥 example ,定义网段为 192.168.17.0/24
[example@centosali ~]$ docker network ls
NETWORK ID NAME DRIVER SCOPE
d7a710c5798d bridge bridge local
a081a523bb18 host host local
1fbcd56021db none null local
[example@centosali ~]$ docker network create --subnet=192.168.17.0/24 example
a5ca2fc42ce130fe07ef6a781b3fc8454dec1fcc428d2577383ba8b7ea36a84e
[example@centosali ~]$ docker network ls
NETWORK ID NAME DRIVER SCOPE
d7a710c5798d bridge bridge local
a5ca2fc42ce1 example bridge local
a081a523bb18 host host local
1fbcd56021db none null local
6.2 为容器固定ip 地址
在使用默认网桥时,容器Ip 是自动分配的,每次重启过后IP地址都有可能发生变化:
[example@centosali ~]$ docker run -itd --name=nginx -p 80:80 nginx
ccef70a0bbf003d2ceb49aa2a0455543ad1e6cd6fa9f806f48c3f6be116f674b
[example@centosali ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ccef70a0bbf0 nginx "nginx -g 'daemon of…" 4 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp nginx
# exit
[example@centosali ~]$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' nginx
172.17.42.2
[example@centosali ~]$ docker run -itd --name=httpd httpd
af03b0b78e19ded54f357f5e6b8c5a16649fde729ee9cec85d51210dec7a4e1d
[example@centosali ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
af03b0b78e19 httpd "httpd-foreground" 3 seconds ago Up 2 seconds 80/tcp httpd
ccef70a0bbf0 nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp nginx
[example@centosali ~]$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' httpd
172.17.42.3
[example@centosali ~]$ docker stop nginx && docker restart httpd
[example@centosali ~]$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' httpd
172.17.42.2
[example@centosali ~]$ docker run -d --name=nginx --network example --ip 192.168.17.10 nginx
1e71d885df8a5b4f85ebf0ce618d29466365a750e5ced45122639dc2ca8e59b2
[example@centosali ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1e71d885df8a nginx "nginx -g 'daemon of…" 3 seconds ago Up 2 seconds 80/tcp nginx
af03b0b78e19 httpd "httpd-foreground" 9 minutes ago Up 9 minutes 80/tcp httpd
[example@centosali ~]$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' nginx
192.168.17.10
[example@centosali ~]$ docker network connect example httpd
# 这时容器httpd有两个Ip 地址
[example@centosali ~]$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' httpd
172.17.42.2192.168.17.2
[example@centosali ~]$ docker exec -it httpd bash
root@af03b0b78e19:/usr/local/apache2# ping nginx
PING nginx (192.168.17.10): 56 data bytes
64 bytes from 192.168.17.10: icmp_seq=0 ttl=64 time=0.111 ms
root@af03b0b78e19:/usr/local/apache2# exit
exit
[example@centosali ~]$ docker exec -it nginx bash
root@1e71d885df8a:/# ping httpd
PING httpd (192.168.17.2): 56 data bytes
64 bytes from 192.168.17.2: icmp_seq=0 ttl=64 time=0.065 ms
root@1e71d885df8a:/# exit
exit
#httpd 移出网络 example
[example@centosali ~]$ docker network disconnect example httpd
[example@centosali ~]$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' httpd
172.17.42.2
1.安装
[example@centosali ~]$ sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
[example@centosali ~]$ ls -al /usr/local/bin/docker-compose
-rw-r--r-- 1 root root 11748168 3月 17 09:41 /usr/local/bin/docker-compose
[example@centosali ~]$ sudo chmod +x /usr/local/bin/docker-compose
[example@centosali ~]$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/
[example@centosali ~]$ docker-compose -v
docker-compose version 1.23.2, build 1110ad01
[example@centosali ~]$ mkdir compose
[example@centosali ~/compose]$ cat docker-compose.yml
version: '3' #Version in docker-compose.yml should be a string.
services:
nginx:
container_name: nginx
image: nginx:latest
networks:
- example
restart: always
ports:
- "8080:80"
networks:
example:
driver: bridge
ipam:
config:
- subnet: 192.168.1.0/24
** 注: 在使用自定义网络时,需配置networks 项。docker-compose 会自动创建一个以“目录名_网络名”来命名的自定义网络.
#启动容器并使其后台运行
[example@centosali ~/compose]$ docker-compose up -d
Creating network "compose_example" with driver "bridge"
Creating nginx ... done
[example@centosali ~/compose]$
[example@centosali ~/compose]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a96c41d006e0 nginx:latest "nginx -g 'daemon of…" 6 seconds ago Up 5 seconds 0.0.0.0:8080->80/tcp nginx
# down 命令会删除 up时所创建的容器、网桥、镜像等
[example@centosali ~/compose]$ docker-compose down
Stopping nginx ... done
Removing nginx ... done
Removing network compose_example
[example@centosali ~/compose]$