作业练习:
Docker 安装nginx
# 搜索镜像,建议去docker hub网站搜索,这样可以看到很多帮助文档信息
# https://hub.docker.com/search?q=nginx&type=image&operating_system=linux
[root@localhost docker]# docker search ngnix
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
userxy2015/ngnix ngnix 12
merpso/ngnix-plus-k8s-ingress NGNIX+ kubernetes-ingess 2
ludwringliccien/ngnix-php7.1 ngnix-php7.1 1 [OK]
jhuiting/ngnix 1
sushanth53/ngnix_server ngnix server 0
# 下载镜像
[root@localhost docker]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
启动nginx
# 启动nginx ,-d 后台运行,--name为容器命名,-p 指定一个宿主机端口3344映射容器端口80
[root@localhost docker]# docker run -d --name nginx01 -p 3344:80 nginx
0b32d7af3fac83f167050ae25787721467086185c376e46cc5a9cdbfcb5e823a
# 本机访问nginx
[root@localhost docker]# curl localhost:3344
<!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>
# 进入容器
[root@localhost docker]# docker exec -it nginx01 /bin/bash
root@0b32d7af3fac:/# wherei s nginx
bash: wherei: command not found
root@0b32d7af3fac:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@0b32d7af3fac:/# cd /etc/nginx/
root@0b32d7af3fac:/etc/nginx# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
端口暴露的概念
-p 3344:80 # 3344宿主机端口 80 容器内端口
思考题:我们每次改动nginx配置文件,都需要进入容器内部?麻烦,如何在容器外部提供一个映射路径,达到在容器内修改文件名,容器内自动修改。
-v 数据卷
docker 安装一个tomcat
# 官方的使用
[root@localhost]$sudo docker run -it --rm tomcat:9.0
# 我们之前的启动都是后台,停止了容器之后,容器可以查到,docker run -it --rm 一般用来测试,用完即删
# 下载之后启动
[root@localhost]#docker pull tomcat
# 启动运行
[root@localhost]# dockerrun -d -p 3355:8080 --name tomcat01 tomcat
# 测试访问
http://172.16.12.154:3355
# 进入容器
[root@localhost]# docker exec -it tomcat01 /bin/bash
# 发现问题
1.linux命令少了
2.没有webapps,解决办法:从webapps.dist文件夹下的所有文件拷贝到webapps文件夹下,或者直接修改webapps.dis名字
3.阿里云镜像的原因,默认是最小的镜像,所有不必要的都省略了。保证最小可运行的环境。