一、Docker常用命令小结
Docker命令帮助文档(重要)
attach Attach local standard input, output, and error streams to a running container #当前shell下 attach连接指定运行的镜像
build Build an image from a Dockerfile # 通过Dockerfile定制镜像
commit Create a new image from a container changes #提交当前容器为新的镜像
cp Copy files/folders between a container and the local filesystem # 拷贝文件
create Create a new container #创建一个新的容器
diff Inspect changes to files or directories on a container filesystem #查看docker容器的变化
events Get real time events from the server # 从服务获取容器实时时间
exec Run a command in a running container # 在运行中的容器上运行命令
export Export a container's filesystem as a tar archive #导出容器文件系统作 为一个tar归档文件[对应import]
history Show the history of an image # 展示一个镜像形成历史
images List images #列出系统当前的镜像
import Import the contents from a tarball to create a filesystem image # 从tar包中导入内容创建一个文件系统镜像
info Display system-wide information # 显示全系统信息
inspect Return low-level information on Docker objects #查看容器详细信息
kill Kill one or more running containers # kill指定docker容器
load Load an image from a tar archive or STDIN #从一个tar包或标准输入中加载 一个镜像[对应save]
login Log in to a Docker registry # 注册或登录一个源服务器
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
二、Docker部署Nginx
#1. 搜索镜像 search 建议大家去docker搜索,可以看到帮助文档
#2. 拉取镜像 pull
#3. 运行测试
# -d 后台运行
# --name 给容器命名
# -p 宿主机端口:容器内部端口
[root@VM-12-16-centos ~]# 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
[root@VM-12-16-centos ~]# docker images;
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 3 days ago 141MB
mysql 5.7 c20987f18b13 12 days ago 448MB
centos latest 5d0da3dc9764 3 months ago 231MB
[root@VM-12-16-centos ~]# docker run -d --name nginx01 -p 3344:80 nginx
f594e620f359671d98239248188a3f3defbb9de3b2a16882b19200623df37412
[root@VM-12-16-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f594e620f359 nginx "/docker-entrypoint.…" 9 seconds ago Up 8 seconds 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01
4d241f98e11a centos "/bin/bash -c 'while…" 23 hours ago Up 23 hours nice_dewdney
[root@VM-12-16-centos ~]# 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@VM-12-16-centos ~]# docker exec -it nginx01 /bin/bash
root@f594e620f359:/# ls
bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
去服务器上开放3344端口(用命令开启也可以),不然访问不到:
访问成功!
暴露端口流程图:
思考问题:我们每次改动nginx配置文件,都需要进入容器内部?十分的麻烦,要是可以在容器外部提供一个映射路径,达到在容器修改文件名,容器内部就可以自动修改?→ 数据卷!
三、Docker部署Tomcat
[root@VM-12-16-centos ~]# docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
0e29546d541c: Already exists
9b829c73b52b: Already exists
cb5b7ae36172: Already exists
6494e4811622: Already exists
668f6fcc5fa5: Already exists
dc120c3e0290: Already exists
8f7c0eebb7b1: Already exists
77b694f83996: Already exists
0f611256ec3a: Pull complete
4f25def12f23: Pull complete
Digest: sha256:9dee185c3b161cdfede1f5e35e8b56ebc9de88ed3a79526939701f3537a52324
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest
[root@VM-12-16-centos ~]# docker run -d --name tomcat01 -p 8080:8080 tomcat
43c0eba854494c9c018ee1b00a613b3b9d486a7192247d0a3153840a3e13f2e1
[root@VM-12-16-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43c0eba85449 tomcat "catalina.sh run" 5 seconds ago Up 4 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat01
f594e620f359 nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01
4d241f98e11a centos "/bin/bash -c 'while…" 23 hours ago Up 23 hours nice_dewdney
[root@VM-12-16-centos ~]# curl localhost:8080
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/10.0.14</h3></body></html>
[root@VM-12-16-centos ~]# docker exec -it tomcat01 /bin/bash
root@43c0eba85449:/usr/local/tomcat# ls
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE README.md RELEASE-NOTES RUNNING.txt bin conf lib logs native-jni-lib temp webapps webapps.dist work
root@43c0eba85449:/usr/local/tomcat# cd /usr/local/tomcat/
root@43c0eba85449:/usr/local/tomcat# ls
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE README.md RELEASE-NOTES RUNNING.txt bin conf lib logs native-jni-lib temp webapps webapps.dist work
root@43c0eba85449:/usr/local/tomcat# cd webapps
root@43c0eba85449:/usr/local/tomcat/webapps# ls
发现问题:1、linux命令少了。 2.没有webapps
阿里云镜像(阉割版),它为保证最小镜像,将不必要的都剔除了→保证最小可运行环境!
此时访问8080肯定就是404了,下面我们把tomcat的主页面拷贝到webapps下面即可成功显示。
root@43c0eba85449:/usr/local/tomcat/webapps# exit
exit
[root@VM-12-16-centos ~]# docker exec -it tomcat01 /bin/bash
root@43c0eba85449:/usr/local/tomcat# ls
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE README.md RELEASE-NOTES RUNNING.txt bin conf lib logs native-jni-lib temp webapps webapps.dist work
root@43c0eba85449:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@43c0eba85449:/usr/local/tomcat# cd webapps
root@43c0eba85449:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
思考问题:我们以后要部署项目,如果每次都要进入容器是不是十分麻烦?要是可以在容器外部提供一个映射路径,webapps,我们在外部放置项目,就自动同步内部就好了!