1、查看服务器是否安装了Docker命令
若提示找不到命令则说明没安装Docker
2、服务器安装Docker容器docker info
3、获取某容器服务的信息,这里以nginx为例yum -y install docker
4、在容器中拉去镜像到本地,例如nginxdocker search nginx
执行后如下:docker pull nginx
5、查看刚拉取的nginx镜像[root@blog-tag-gg ~]# docker pull nginx
Using default tag: latest
Trying to pull repository docker.io/library/nginx ...
latest: Pulling from docker.io/library/nginx
Digest: sha256:2834dc507516af02784808c5f48b7cbe38b8ed5d0f4837f16e78d00deb7e7767
Status: Image is up to date for docker.io/nginx:latest
执行后如下:docker images nginx
6、安装nginx容器服务[root@blog-tag-gg ~]# docker images nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest c316d5a335a5 2 weeks ago 142 MB
docker run -d --name nginx -p 80:80 c316
- -d 指定容器以守护进程方式在后台运行
- --name 指定容器名称,此处我指定的是mynginx
- -p 指定主机与容器内部的端口号映射关系,格式 -p
- [宿主机端口号]:[容器内部端口],此处我使用了主机80端口,映射容器80端口
- c316 是nginx的镜像IMAGE ID前4位
docker ps -a
docker ps
8、进入容器方法很多。[root@blog-tag-gg ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8b92e4b387ed nginx "/docker-entrypoin..." 3 hours ago Up 3 hours 0.0.0.0:80->80/tcp nginxdss
docker attach 8b92e4b387ed
docker exec -it 8b92e4b387ed /bin/bash
[root@blog-tag-gg ~]# docker exec -it 8b92e4b387ed /bin/bash
root@8b92e4b387ed:/#
- exec 命令代表附着到运行着的容器内部
- -it 是 -i 与 -t两个参数合并写法,-i -t 标志着为我们指定的容器创建了TTY并捕捉了STDIN
- 8b92e4b387ed 是我们要进入的容器Id
- /bin/bash 指定了执行命令的shell
10、查看容器的ip地址mac等信息
docker network inspect bridge
11、设置开机自启Docker服务[root@blog-tag-gg ~]# docker network inspect bridge
[
{
"Name": "bridge",
"Id": "5dd7497db7a33ef7037c230d07b9f0f6211d16f2770ff74e252d78d96f1375ce",
"Created": "2022-02-12T10:57:27.736352902+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "192.168.1.5/24",
"Gateway": "192.168.1.5"
}
]
},
"Internal": false,
"Attachable": false,
"Containers": {
"8b92e4b387edc30366ffdbcc5297563190905217f795664657022e53bdf59425": {
"Name": "nginxdss",
"EndpointID": "741657b490193d927c3a9b97f074639bfe0a33d3141a3a382a19cb55d8f7ee44",
"MacAddress": "02:42:c0:a8:01:01",
"IPv4Address": "192.168.1.1/24",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": "1500"
},
"Labels": {}
}
]
12、删除docker服务systemctl enable docker
13、本地端口 81 映射到容器 80 端口yum remove docker
14、 查看容器日志docker run -p 81:80 --name testtttt nginx
docker logs -f -t --tail 显示行数 容器ID
docker logs 8b92e4b387ed
15、实时查看容器日志root@z2:~# docker logs 8b92e4b387ed
172.17.0.1 - - [13/May/2020:14:25:40 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.58.0" "-"
16、查看某容器的端口映射、docker logs -f 容器ID/name
docker port 8b92e4b387ed
17、后台启动容器[root@blog-tag-gg ~]# docker port 8b92e4b387ed
80/tcp -> 0.0.0.0:80
docker run -d -P --name nginx-test1 docker.io/nginx
18、创建容器后直接进入[root@blog-tag-gg ~]# docker run -d -P --name nginx-test1 docker.io/nginx
c1808423f71d791567ae23e93953f74675343d58e4e3e672e4b52c85d5c03a44
docker run -t -i --name test-centos2 docker.io/centos /bin/bash
19、容器的启动和关闭[root@blog-tag-gg ~]# docker run -t -i --name test-centos2 docker.io/centos /bin/bash
Unable to find image 'docker.io/centos:latest' locally
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for docker.io/centos:latest
20、获取容器的ip地址:docker stop 017fcfa5fdf1
docker start 017fcfa5fdf1
docker inspect -f "{{.NetworkSettings.IPAddress}}" 8b92e4b387ed
21、设置容器的dns地址。[root@blog-tag-gg ~]# docker inspect -f "{{.NetworkSettings.IPAddress}}" 8b92e4b387ed
192.168.1.1
22、设置容器的固定ipdocker run -it --rm --dns 223.5.5.5 centos bash
/etc/docker/daemon.json
23、 将容器的8000端口映射到docker主机的8001端口{
"ip": "0.0.0.0"
}
24、强制停止容器ptables -t nat -A DOCKER -p tcp --dport 8001 -j DNAT --to-destination 172.17.0.19:8000
25、删除已停止的容器docker kill 容器ID或者容器名
26、一次性删除多个容器docker rm 容器ID
docker rm -f $(docker ps -a -q)
27、查看容器内的进程docker ps -a -q | xargs docker rm
28、查看容器内的细节docker top 容器 ID
29、从容器中拷贝文件到主机docker inspect 容器 ID
将容器 8e5db45ddfd5中 /www/wwwroot/test.txt文件拷贝到 宿主服务器的 /root/目录docker cp [OPTIONS] CONTAINER:容器文件路径 宿主服务器路径
30、从宿主机拷贝文件到容器中docker cp 8e5db45ddfd5:/www/wwwroot/test.txt /root/
将宿主/root下的test.txt文件拷贝到容器8e5db45ddfd5的/www/wwwroot/目录docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH
31、查看可安装的docker版本docker cp /root/test.txt 8e5db45ddfd5:/www/wwwroot/
32、解释为:Docker 以 ubuntu15.10 镜像创建一个新容器,然后在容器里执行 bin/echo "Hello world",然后输出结果。yum list docker-ce --showduplicates | sort -r
docker run ubuntu:15.10 /bin/echo "Hello world"
文章评论 本文章有个评论