docker高级操作

映射端口
docker run -p 容器外端口: 容器里端口
挂载数据卷
docker run -p 容器外目录: 容器里目录
传递环境变量
docker run -e key=value docker run -e key=value -e key=value # 传多个环境变量
一.映射端口
1.先下载 nginx 镜像
2.打好标签
[root@localhost ~]# docker pull nginx:1.12.2 1.12.2: Pulling from library/nginx f2aa67a397c4: Pull complete e3eaf3d87fe0: Pull complete 38cb13c1e4c9: Pull complete Digest: sha256:72daaf46f11cc753c4eab981cbf869919bd1fee3d2170a2adeac12400f494728 Status: Downloaded newer image for nginx:1.12.2 docker.io/library/nginx:1.12.2 [root@localhost ~]# [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest fce289e99eb9 13 months ago 1.84kB nginx 1.12.2 4037a5562b03 22 months ago 108MB [root@localhost ~]# [root@localhost ~]# docker tag 4037a5562b03 shizhengwen/nginx:v1.12.2 [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest fce289e99eb9 13 months ago 1.84kB shizhengwen/nginx v1.12.2 4037a5562b03 22 months ago 108MB nginx 1.12.2 4037a5562b03 22 months ago 108MB
3.启动nginx镜像
-p宿主机端口:容器内端口
[root@localhost ~]# docker run --rm --name mynginx -d -p81:80 shizhengwen/nginx:v1.12.2 47204ab656a600e15e1eac645c4449a5175e96c1008e4300d543c57badd64ca5

查看端口是否起来了 netstat -luntp|grep 81
[root@localhost ~]# netstat -luntp|grep 81 tcp6 0 0 :::81 :::* LISTEN 6724/docker-proxy
curl 一下 curl 127.0.0.1:81
[root@localhost ~]# curl 127.0.0.1:81
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
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>
二.挂载数据
4.在家目录创建文件夹
[root@localhost ~]# cd [root@localhost ~]# mkdir html [root@localhost ~]# cd html/ [root@localhost html]#
5.下载一个html
[root@localhost html]# wget www.baidu.com -O index.html
6.启动nginx
-v宿主机目录: 容器内目录
docker run -d --rm --name nginx_with_baidu -d -p82:80 -v/root/html:/usr/share/nginx/html shizhengwen/nginx:v1.12.2
[root@localhost html]# docker run -d --rm --name nginx_with_baidu -d -p82:80 -v/root/html:/usr/share/nginx/html shizhengwen/nginx:v1.12.2 c09cc1eeb5dfd7a690b352975558a540f25303ae3dbc127f3eb958785ea06b6c
三.传递环境变量
7.启动镜像
-e key键=value值
docker run --rm -e E_HEHE=world -e NIHAO=nihao shizhengwen/alpine:5.5 printenv
docker run --rm -e E_HEHE=e_world shizhengwen/alpine:5.5 printenv docker run --rm -e DEV=dev -e PROD=prod shizhengwen/alpine:5.5 printenv
四.容器里安装软件
8.先进入到一个容器
[root@localhost ~]# docker exec -ti nginx_with_baidu /bin/bash
9.假如 curl 命令是容器里没有的
root@c09cc1eeb5df:/# curl bash: curl: command not found
10.我们要想给容器转一个 curl 命令就要先去更新 apt 的一个源
tee /etc/apt/sources.list << EOF deb http://mirrors.163.com/debian/ jessie main non-ffree contrib deb http://mirrirs.163.com/dobian/ jessie-updates main non-free contrib EOF

这下源就更新了
11.下载 curl 了
apt-get update && apt-get install -y curl
12.固化镜像 # 把刚刚下载过工具的镜像给固化
docker commit -p c09cc1eeb5df shizhengwen/nginx:curl
13. 推送到仓库
docker push shizhengwen/nginx:curl

浙公网安备 33010602011771号