文章目录
- Docker引擎的安装
- Docker比vm虚拟机快
- Docker常用命令
- 帮助启动类命令
- 镜像命令
- docker images
- docker search
- docker pull
- docker system df
- docker rmi
- 容器命令
- redis前台交互式启动
- redis后台守护式启动
- Nginx容器运行
- ubuntu交互式运行
- tomcat交互式运行
- 对外暴露访问端口
Docker引擎的安装
安装Docker需要的依赖gcc gcc-c++
yum -y install gcc
yum -y install gcc-c++
因为我之前已经安装了,所以现在提示"Nothing to do"
[root@localhost ~]# yum -y install gcc
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile* base: mirrors.aliyun.com* extras: mirrors.aliyun.com* updates: mirrors.aliyun.com
Package gcc-4.8.5-44.el7.x86_64 already installed and latest version
Nothing to do[root@localhost ~]# yum -y install gcc-c++
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile* base: mirrors.aliyun.com* extras: mirrors.aliyun.com* updates: mirrors.aliyun.com
Package gcc-c++-4.8.5-44.el7.x86_64 already installed and latest version
Nothing to do
非root用户需要使用sudo提权
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
安装Docker
sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
查看docker版本
docker version
[root@localhost ~]# docker version
Client: Docker Engine - CommunityVersion: 26.0.0API version: 1.45Go version: go1.21.8Git commit: 2ae903eBuilt: Wed Mar 20 15:21:09 2024OS/Arch: linux/amd64Context: defaultServer: Docker Engine - CommunityEngine:Version: 26.0.0API version: 1.45 (minimum version 1.24)Go version: go1.21.8Git commit: 8b79278Built: Wed Mar 20 15:20:06 2024OS/Arch: linux/amd64Experimental: falsecontainerd:Version: 1.6.28GitCommit: ae07eda36dd25f8a1b98dfbf587313b99c0190bbrunc:Version: 1.1.12GitCommit: v1.1.12-0-g51d5e94docker-init:Version: 0.19.0GitCommit: de40ad0
运行hello-world
docker run hello-world
[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:53641cd209a4fecfc68e21a99871ce8c6920b2e7502df0a20671c6fccc73a7c6
Status: Downloaded newer image for hello-world:latestHello from Docker!
This message shows that your installation appears to be working correctly.To generate this message, Docker took the following steps:1. The Docker client contacted the Docker daemon.2. The Docker daemon pulled the "hello-world" image from the Docker Hub.(amd64)3. The Docker daemon created a new container from that image which runs theexecutable that produces the output you are currently reading.4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.To try something more ambitious, you can run an Ubuntu container with:$ docker run -it ubuntu bashShare images, automate workflows, and more with a free Docker ID:https://hub.docker.com/For more examples and ideas, visit:https://docs.docker.com/get-started/
Docker比vm虚拟机快
为什么Docker会比vm虚拟机快
1.docker有着比虚拟机更少的抽象层由于docker不需要Hypervisor(虚拟机)实现硬件资源虚拟化,运行在docker容器上的程序直接使用的都是实际物理机的硬件资源,因此在cpu、内存利用率上docker将会在效率上有明显优势。2.docker利用的是宿主机的内核,而不是需要加载操作系统os内核当新建一个容器时,docker不需要和虚拟机一样重新加载一个操作系统内核。
Docker常用命令
帮助启动类命令
systemctl start docker
systemctl stop docker
systemctl status docker
systemctl restart docker
systemctl enable docker
docker info
docker --help
docker command(具体命令) --help
镜像命令
docker images
docker images #列出本地主机上的镜像
[root@localhost ~]# docker images
REPOSITORY(表示镜像的仓库源) TAG(镜像的标签版本号) IMAGE ID(镜像ID) CREATED(镜像创建时间) SIZE(镜像大小)
hello-world latest d2c94e258dcb 11 months ago 13.3kB
docker search
docker search 镜像名称
docker search redis
[root@localhost ~]# docker search redis
NAME DESCRIPTION STARS OFFICIAL
redis Redis is an open source key-value store that… 12764 [OK]
redislabs/redisearch Redis With the RedisSearch module pre-loaded… 63
redislabs/redisinsight RedisInsight - The GUI for Redis 101
redis/redis-stack-server redis-stack-server installs a Redis server w… 72
redis/redis-stack redis-stack installs a Redis server with add… 103
redislabs/rebloom A probablistic datatypes module for Redis 27
redislabs/redis Clustered in-memory database engine compatib… 40
......name 镜像名称
DESCRIPTION 镜像说明
STATRS 点赞数量
OFFICIAL 是否官方认证docker search --limit 5 redis #搜索前五名
docker pull
下载镜像
docker pull 镜像名字[:TAG] #没有TAG就是最新版
docker system df
查看镜像/容器/数据卷所占的空间
docker rmi
删除镜像
docker rmi -f 镜像ID #删除单个
docker rmi -f 镜像名1:TAG 镜像名2:TAG #删除多个
docker rmi -f$(docker images -qa) #删除全部
容器命令
[root@localhost ~]# docker run --helpUsage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Create and run a new container from an image
Aliases:docker container run, docker runOptions:-i, --interactive Keep STDIN open even if not attached-t, --tty Allocate a pseudo-TTY-d, --detach Run container in background and print container ID
redis前台交互式启动
前台启动很容易被意外终止,窗口一关就没了
docker run -it redis:6.0.8
redis后台守护式启动
docker run -d redis:6.0.8
查看容器内运行的进程
Nginx容器运行
保存拉取好的nginx镜像
删除nginx镜像,通过上一步保存好的nginx.tar包再将nginx加载回来
创建nginx容器
docker run -d --name nginx -p 80:80 nginx
[root@localhost ~]# docker run -d --name nginx -p 80:80 nginx
c37c23e22fcb0001e16f0da7e4d23f7787673f1e222e788826ea843c55d26437
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c37c23e22fcb nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
停掉nginx容器
docker stop nginx
启动nginx容器
docker start nginx
查看nginx日志
docker logs nginxdocker logs -f nginx #持续刷新日志
进入nginx容器内部
docker exec -it nginx bash
ubuntu交互式运行
当我们直接使用docker run(不加任何参数)运行某个容器,使用ps命令查看容器状态时,STATUS显示的是‘Exited’,表示已经退出了。当我们希望有交互式的输入命令时,需要使用到‘-it’参数
docker run -it --name myubuntu2 ubuntu /bin/bash
交互式时,容器的状态是’Up’
tomcat交互式运行
docker run --name mytomcat2 -it tomcat:8.5.49
当运行完以上命令后,以下为截取的部分tomcat启动运行日志,可以看到tomcat启动成功了。
[root@localhost ~]# docker run --name mytomcat2 -it tomcat:8.5.49
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/openjdk-8
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
.......
ProtocolHandler ["http-nio-8080"]
11-Apr-2024 07:33:02.042 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
11-Apr-2024 07:33:02.058 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 571 ms
查看容器状态时,可以看到COMMAND为“catalina.sh run”,次命令可以参考tomcat的Dockerfile
既然tomcat都启动成功了,那tomcat就可以访问了吗,此时我们访问下这个主机的8080端口试一下,
哦NOT,汤姆小猫并没有出来,此时你需要对外暴露访问端口。
对外暴露访问端口
专用术语称之为端口映射,将宿主机的8080端口映射为8081,也就是对外我们访问8081端口就等于访问宿主机的8080端口。
docker run --name mytomcat3 -it -p 8081:8080 tomcat:8.5.49
-p, --publish list Publish a container's port(s) to the host# -p参数用于指定发布端口
[root@localhost ~]# docker run --name mytomcat3 -it -p 8081:8080 tomcat:8.5.49
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/openjdk-8
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
11-Apr-2024 07:45:24.267 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/8.5.49
11-Apr-2024 07:45:24.269 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Nov 17 2019 18:45:30 UTC
11-Apr-2024 07:45:24.269 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 8.5.49.0
11-Apr-2024 07:45:24.269 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
11-Apr-2024 07:45:24.269 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 3.10.0-1160.el7.x86_64
11-Apr-2024 07:45:24.270 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
11-Apr-2024 07:45:24.270 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/local/openjdk-8/jre
11-Apr-2024 07:45:24.270 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_232-b09
11-Apr-2024 07:45:24.270 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation
11-Apr-2024 07:45:24.270 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat
11-Apr-2024 07:45:24.270 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat
......11-Apr-2024 07:45:24.990 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/manager] has finished in [16] ms
11-Apr-2024 07:45:24.998 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
11-Apr-2024 07:45:25.009 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
11-Apr-2024 07:45:25.013 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 594 ms
此时我们访问8081端口:
噢耶,tomcat小猫咪出来了。