微服务之——Docker高级操作

Docker高级操作

 

映射端口:                          doker run -p 容器外端口: 容器里端口

挂载数据卷:                      docker run -v 容器外目录: 容器里目录

传递环境变量                   docker run -e key=value
docker run -e key=value -e key=value # 传多个环境变量

 

一.映射端口

1.先下载 nginx 镜像

2.打好标签

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

[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 feixiangkeji974907/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 宿主机端口:容器内端口

1

2

[root@localhost ~]# docker run –name mynginx -d -p 80:80 feixiangkeji974907/nginx:v1.12.2

89d670a4bc27e3576fe8900aaa17a36a572daff46c12d4995c2fcf35d5107a87

图片[1]-微服务之——Docker高级操作-飞翔沫沫情

查看宿主机端口是否起来了 netstat -ntlup | grep 80

[root@localhost ~]# netstat -ntlup | grep 80

tcp6 0 0 :::80 :::* LISTEN 20013/docker-proxy

curl 一下 curl 127.0.0.1:80

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

[root@localhost ~]# curl 127.0.0.1:80

<!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>

二.挂载数据(以nginx为例)

4.在家目录创建文件夹

1

2

3

4

[root@localhost ~]# cd

[root@localhost ~]# mkdir html

[root@localhost ~]# cd html/

[root@localhost html]#

5.下载一个html

1 [root@localhost html]# wget www.baidu.com -O index.html

6.启动nginx

-v 宿主机目录: 容器内目录

[root@localhost html]# docker run –name nginx_with_baidu -d -p 82:80 -v /root/html:/usr/share/nginx/html feixiangkeji974907/nginx:v1.12.2

2fc055d4ceddf0fa30e6c5d78038c5d888bd286c6c31f0cd0cf52309d7535c91

图片[2]-微服务之——Docker高级操作-飞翔沫沫情

 

三.传递环境变量

7.启动镜像

-e key键=value值

[root@localhost html]# docker run –rm -e E_hehe=world –name test feixiangkeji974907/nginx:v1.12.2 printenv

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

HOSTNAME=e306a8baf34b

E_hehe=world

NGINX_VERSION=1.12.2-1~stretch

NJS_VERSION=1.12.2.0.1.14-1~stretch

HOME=/root

图片[3]-微服务之——Docker高级操作-飞翔沫沫情

四.容器里安装软件

8.先进入到一个容器

1 [root@localhost ~]# docker exec -ti nginx_with_baidu /bin/bash

9.假如 curl 命令是容器里没有的

1

2

root@c09cc1eeb5df:/# curl

bash: curl: command not found

10.我们要想给容器转一个 curl 命令就要先去更新 apt 的一个源

1

2

3

4

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

图片[4]-微服务之——Docker高级操作-飞翔沫沫情

 

这下源就更新了

11.下载 curl 了

root@2fc055d4cedd:/# apt-get update && apt-get install -y curl

再安装个lrzsz

root@2fc055d4cedd:/# apt-get install lrzsz -y

12.固化镜像 # 把刚刚下载过工具的镜像给固化

1 docker commit -p c09cc1eeb5df feixiangkeji974907/nginx:curl

13. 推送到仓库

1 docker push feixiangkeji974907/nginx:curl

五、容器自启动

Docker提供了restart policy机制,可以在容器退出或者Docker重启时控制容器能够自启动。这种Restart policy可以保证相关容器按照正确顺序启动。虽然也可以通过进程监控的方式(如systemd)来完成这种动作,但Docker还是建议尽量避免使用进程监控的方式来 “自启动” 容器。

在使用docker run启动容器时,使用–restart参数来设置:

docker run -m 512m –memory-swap 1G -it -p 58080:8080–restart=always

–name bvrfis

–restart具体参数值详细信息:

no –  容器退出时,不重启容器;

on-failure – 只有在非0状态退出时才从新启动容器;

always – 无论退出状态是如何,都重启容器;

如果创建时未指定 –restart=always ,可通过update 命令设置

docker update –restart=always 容器名称

六、容器拷贝文件

1、从容器里面拷文件到宿主机

答:在宿主机里面执行以下命令

docker cp 容器名:要拷贝的文件在容器里面的路径       要拷贝到宿主机的相应路径

示例: 假设容器名为testtomcat,要从容器里面拷贝的文件路为:/usr/local/tomcat/webapps/test/js/test.js,  现在要将test.js从容器里面拷到宿主机的/opt路径下面,那么命令应该怎么写呢?

答案:在宿主机上面执行命令

docker cp testtomcat:/usr/local/tomcat/webapps/test/js/test.js /opt

2、从宿主机拷文件到容器里面

答:在宿主机里面执行如下命令

docker cp:要拷贝的文件路径 容器名:要拷贝到容器里面对应的路径

示例:假设容器名为testtomcat,现在要将宿主机/opt/test.js文件拷贝到容器里面的/usr/local/tomcat/webapps/test/js路径下面,那么命令该怎么写呢?

答案:在宿主机上面执行如下命令

docker cp /opt/test.js testtomcat:/usr/local/tomcat/webapps/test/js

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容