docker学习记录第3节:docker容器&镜像(基于腾讯云 centos7.6)
下载nginx
docker pull nginx
查看镜像
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c20060033e06 7 days ago 187MB
打标签
docker tag c20060033e06 docker.io/myimages/nginx:latest
查看镜像
DOCKER IMAGES
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c20060033e06 7 days ago 187MB
myimages/nginx latest c20060033e06 7 days ago 187MB
运行 nginx 容器
[root@VM-8-6-centos /]# docker run -itd --rm --name mynginx myimages/nginx
35057f7b480ee7bf98a8c0bc971ed99e97b557e15d34d2034db02e0208df7025
进入容器访问url
docker exec -ti 35057 /bin/bash
curl 127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
-p 映射端口
我们想在外部访问容器里面的 nginx 这个时候我们就需要映射端口
我们可以通过 -p 参数来映射端口
docker run -p 容器外端口:容器内端口 容器 ID
例子1:
使用myimages/nginx镜像 启动一个新的容器 取名mynginx 映射端口80到80
docker run -itd --rm -p 80:80 --name mynginx myimages/nginx
例子2:
指定容器id 映射80到80
docker run -itd --rm -p 80:80 cbefb811a2ae
Docker 部署 Nginx 挂载数据
基本语法:
docker run -v 容器外目录:容器内目录 容器 ID
完整例子:
docker中默认路径
conf
/etc/nginx/nginx.conf
html
/usr/share/nginx/html
log
/var/log/nginx
启动临时容器
docker run --name tmp-nginx-container -d nginx
新建本地目录
mkdir -p /docker/nginx/
拷贝临时容器默认文件到默认路径
docker cp tmp-nginx-container:/etc/nginx/nginx.conf /docker/nginx/nginx.conf
docker cp -a tmp-nginx-container:/usr/share/nginx/html /docker/nginx
docker cp -a tmp-nginx-container:/etc/nginx/conf.d /docker/nginx
删除临时容器
docker rm -f tmp-nginx-container
重新映射容器启动
docker run -itd --name mynignx -p 80:80 -v /docker/nginx/html:/usr/share/nginx/html -v /docker/nginx/nginx.conf:/etc/nginx/nginx.conf -v /docker/nginx/conf.d:/etc/nginx/conf.d -v /docker/nginx/logs:/var/log/nginx nginx