Java学习之Docker命令练习
一、Docker安装Nginx
步骤:
- 搜索镜像,去docker搜索可以看到帮助文档
- 下载镜像
- 运行测试
# 搜索镜像,去docker搜索可以看到帮助文档
[root@master ~]$ docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 19409 [OK]
unit Official build of NGINX Unit: Universal Web … 19 [OK]
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 140
nginx/nginx-ingress NGINX and NGINX Plus Ingress Controllers fo… 87
[root@master ~]$ docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
24e221e92a36: Pull complete
58cc89079bd7: Pull complete
3799b53049f3: Pull complete
2a580edba2f4: Pull complete
cfe7877ea167: Pull complete
6f26751fc54b: Pull complete
c98494bb3682: Pull complete
Digest: sha256:2bdc49f2f8ae8d8dc50ed00f2ee56d00385c6f8bc8a8b320d0a294d9e3b49026
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
# 第一次没有指定端口
[root@master ~]$ docker run -d --name mynginx nginx
93511831ca8aeca420df6dfdd0bbcb85586cb392009ff8f61d704db87019b403
[root@master ~]$ curl 172.16.211.11:80
curl: (7) Failed connect to 172.16.211.11:80; 拒绝连接
[root@master ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
93511831ca8a nginx "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 80/tcp mynginx
# 第二次指定端口3003
[root@master ~]$ docker run -d --name mynginx -p 3003:80 nginx
docker: Error response from daemon: Conflict. The container name "/mynginx" is already in use by container "93511831ca8aeca420df6dfdd0bbcb85586cb392009ff8f61d704db87019b403". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
[root@master ~]$ docker run -d --name mynginx02 -p 3003:80 nginx
c075a035f05889ce7cae5f624b141a1e4bd21dbc0cbf8fa2b294c6eb8d5c3582
[root@master ~]$ curl localhost:3003
<!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@master ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c075a035f058 nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:3003->80/tcp, :::3003->80/tcp mynginx02
[root@master ~]$ docker exec it mynginx02 /bin/bash
Error response from daemon: No such container: it
[root@master ~]$ docker exec -it mynginx02 /bin/bash
root@c075a035f058:/#
root@c075a035f058:/$ whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@c075a035f058:/$ cd /etc/nginx/
root@c075a035f058:/etc/nginx$ ls
conf.d mime.types nginx.conf uwsgi_params
fastcgi_params modules scgi_params
root@c075a035f058:/etc/nginx$ vim nginx.conf
bash: vim: command not found
端口暴露的概念:
思考:由于每次改动nginx配置文件,都需要进入容器内部,这样导致大量时间的浪费以及事务的繁琐性,因此引入了数据卷的概念。
二、docker安装tomcat
# 官方文档使用命令
# 我们之前的启动都是后台,停止了容器之后,容器还是可以查到
# docker run -it --rm 一般是用来测试,用完即删除
$ docker run -it --rm tomcat:8.0
# 本地测试
[root@master ~]$ docker run -it --rm tomcat:8.0
Unable to find image 'tomcat:8.0' locally
8.0: Pulling from library/tomcat
16b1b4bf6981: Pull complete
002878cce7e1: Pull complete
972135300763: Pulling fs layer
a6fd479552ae: Download complete
1b60c13815e5: Download complete
24dc0ccbc7db: Download complete
67017a4b922c: Download complete
4253042c1f3e: Download complete
5990d953d6b3: Download complete
b5d0b582f218: Waiting
ae4f82866603: Download complete
进行普通的拉镜像,再进行run测试:
[root@master ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 8aea65d81da2 7 weeks ago 192MB
centos latest e6a0117ec169 2 years ago 272MB
[root@master ~]$ docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
7734efb8b826: Pull complete
a83d9c33e24c: Pull complete
de0379dcfaaa: Pull complete
2a8ebe9ea1fa: Pull complete
2a65c085f19e: Pull complete
8e2400b3d5ff: Pull complete
977abc591f43: Pull complete
9453b4318538: Pull complete
Digest: sha256:1107d758acf1bef7748969e0f84c1d071490c1da46a97ab2c04e5cd12f4e4a2b
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest
[root@master ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest 93b7b04e85ea 3 days ago 448MB
nginx latest 8aea65d81da2 7 weeks ago 192MB
centos latest e6a0117ec169 2 years ago 272MB
[root@master ~]$ docker run -d -p 3366:8080 --name tomcat01 tomcat
7d27423a9de291b6ca351342bba18604dccd2a9690c278211640e30cc4c34099
[root@master ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7d27423a9de2 tomcat "catalina.sh run" 5 seconds ago Up 4 seconds 0.0.0.0:3366->8080/tcp, :::3366->8080/tcp tomcat01
[root@master ~]$ docker exec -it tomcat01 /bin/bash
root@7d27423a9de2:/usr/local/tomcat#
root@7d27423a9de2:/usr/local/tomcat$ cp -r webapps.dist/* webapps
root@7d27423a9de2:/usr/local/tomcat$ cd webapps
root@7d27423a9de2:/usr/local/tomcat/webapps$ ll
total 4
drwxr-xr-x. 1 root root 81 Dec 19 21:53 ./
drwxr-xr-x. 1 root root 57 Dec 16 14:57 ../
drwxr-xr-x. 16 root root 4096 Dec 19 21:53 docs/
drwxr-xr-x. 7 root root 99 Dec 19 21:53 examples/
drwxr-xr-x. 6 root root 79 Dec 19 21:53 host-manager/
drwxr-xr-x. 6 root root 114 Dec 19 21:53 manager/
drwxr-xr-x. 3 root root 223 Dec 19 21:53 ROOT/
三、docker安装elasticsearch+kibina
[root@master /]$ docker pull elasticsearch
Using default tag: latest
latest: Pulling from library/elasticsearch
[DEPRECATION NOTICE] Docker Image Format v1, and Docker Image manifest version 2, schema 1 support will be removed in an upcoming release. Suggest the author of docker.io/library/elasticsearch:latest to upgrade the image to the OCI Format, or Docker Image manifest v2, schema 2. More information at https://docs.docker.com/go/deprecated-image-specs/
05d1a5232b46: Pull complete
5cee356eda6b: Pull complete
89d3385f0fd3: Pull complete
65dd87f6620b: Pull complete
78a183a01190: Pull complete
1a4499c85f97: Pull complete
2c9d39b4bfc1: Downloading 120.8MB/122.1MB
1b1cec2222c9: Download complete
59ff4ce9df68: Download complete
1976bc3ee432: Download complete
5af49e8af381: Download complete
42c8b75ff7af: Download complete
7e6902915254: Download complete
99853874fa54: Download complete
596fbad6fcff: Download complete