Docker 一文读懂
Posted 在奋斗的大道
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker 一文读懂相关的知识,希望对你有一定的参考价值。
Docker基本组成
Docker 中有非常重要的三个基本概念,理解了这三个概念,就理解了 Docker 的整个生命周期。
- 镜像(Image)
- 容器(Container)
- 仓库(Repository)
Docker运行流程、原理和整体架构
Docker运行流程
Docker运行原理
Docker整体架构
Docker常用命令
Docker镜像常用命令
Docker创建容器可选参数
Docker 容器限制参数
Docker基本指令
Docker 版本、系统信息(包含镜像和容器的数量信息)和帮助指令
docker version #查看docker的版本信息
docker info #查看docker的系统信息,包括镜像和容器的数量
docker 命令 --help #帮助命令(可查看可选的参数)
docker COMMAND --help
MobaXterm 实际操作
[root@localhost etc]# docker version
Client:
Version: 17.12.0-ce
API version: 1.35
Go version: go1.9.2
Git commit: c97c6d6
Built: Wed Dec 27 20:10:14 2017
OS/Arch: linux/amd64
Server:
Engine:
Version: 17.12.0-ce
API version: 1.35 (minimum version 1.12)
Go version: go1.9.2
Git commit: c97c6d6
Built: Wed Dec 27 20:12:46 2017
OS/Arch: linux/amd64
Experimental: false
[root@localhost etc]# docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 17.12.0-ce
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 89623f28b87a6004d4b785663257362d1658a729
runc version: b2567b37d7b75eb4cf325b77297b140ea686ce8f
init version: 949e6fa
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-1160.66.1.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.777GiB
Name: localhost.localdomain
ID: KG26:QA7R:ZVE7:ITKF:7I6Z:U2XM:HSGR:ZHS6:EPCM:DUOZ:IA4P:LYJS
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://oalqrqxl.mirror.aliyuncs.com/
Live Restore Enabled: false
[root@localhost etc]# docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
[root@localhost etc]#
Docker 镜像命令
docker images 查看本地主机的所有镜像
MobaXterm 实际操作
[root@localhost etc]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost etc]#
列表参数介绍:
# 字段解释:
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小
image指令可选参数:
# 可选参数
-a/--all 列出所有镜像
-q/--quiet 只显示镜像的id
docker search 搜索镜像
MobaXterm 实际操作
[root@localhost etc]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 12729 [OK]
mariadb MariaDB Server is a high performing open sou… 4882 [OK]
percona Percona Server is a fork of the MySQL relati… 579 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 555 [OK]
bitnami/mysql Bitnami MySQL Docker Image 71 [OK]
linuxserver/mysql-workbench 36
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 35
ubuntu/mysql MySQL open source fast, stable, multi-thread… 34
circleci/mysql MySQL is a widely used, open-source relation… 26
google/mysql MySQL server for Google Compute Engine 21 [OK]
rapidfort/mysql RapidFort optimized, hardened image for MySQL 13
vmware/harbor-db Mysql container for Harbor 10
bitnami/mysqld-exporter 3
ibmcom/mysql-s390x Docker image for mysql-s390x 2
nasqueron/mysql 1 [OK]
vitess/mysqlctld vitess/mysqlctld 1 [OK]
newrelic/mysql-plugin New Relic Plugin for monitoring MySQL databa… 1 [OK]
mirantis/mysql 0
docksal/mysql MySQL service images for Docksal - https://d… 0
drud/mysql-local-57 ddev mysql local container 0
drud/mysql 0
drud/mysql-docker-local-57 This repo has been deprecated, new tags are … 0
drud/mysql-docker-local docker containers for local womysql rk 0 [OK]
cimg/mysql 0
silintl/mysql-backup-restore Simple docker image to perform mysql backups… 0 [OK]
[root@localhost etc]#
search指令可选参数:
Search the Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided 基于给定条件过滤
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25) 分页大小
--no-trunc Don't truncate output
检索收藏数大于3000的MySQL镜像且返回一条记录
[root@localhost etc]# docker search mysql --filter=STARS=3000 --limit 1
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 12729 [OK]
docker pull 镜像名[:tag] 下载镜像
MobaXterm 实际操作:
[root@localhost etc]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
[root@localhost etc]#
docker rmi 删除镜像
#1.删除指定的镜像id
[root@localhost /]# docker rmi -f 镜像id
#2.删除多个镜像id
[root@localhost /]# docker rmi -f 镜像id 镜像id 镜像id
#3.删除全部的镜像id
[root@localhost /]# docker rmi -f $(docker images -aq)
Docker 容器命令
docker run [可选参数] image :运行容器
run指令可选参数:
#参数说明
--name="名字" 指定容器名字
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口
( -p ip:主机端口:容器端口 配置主机端口映射到容器端口
-p 主机端口:容器端口
-p 容器端口)
-P 随机指定端口(大写的P)
MobaXterm 实际操作:
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 5 months ago 448MB
[root@localhost /]# docker run c20987f18b13
2022-06-15 03:01:14+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.36-1debian10 started.
2022-06-15 03:01:14+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2022-06-15 03:01:14+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.36-1debian10 started.
2022-06-15 03:01:14+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
You need to specify one of the following:
- MYSQL_ROOT_PASSWORD
- MYSQL_ALLOW_EMPTY_PASSWORD
- MYSQL_RANDOM_ROOT_PASSWORD
[root@localhost /]#
docker run [可选参数] image :进入容器
MobaXterm 实际操作:
[root@localhost /]# docker run -it c20987f18b13 /bin/bash
exit :退出容器
MobaXterm 实际操作:
#exit 停止并退出容器(后台方式运行则仅退出)
#Ctrl+P+Q 不停止容器退出
root@38f095df58c1:/# exit
exit
[root@localhost /]#
docker ps:列出容器
MobaXterm 实际操作:
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost /]#
ps 指令可选参数:
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all Show all containers (default shows just running) 显示全部运行(默认显示正在运行)
-f, --filter filter Filter output based on conditions provided 基于给定条件进行过滤
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states) (default -1) (显示最近创建容器记录数)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display numeric IDs (显示容器编码)
-s, --size Display total file sizes (显示容器文件大小)
# 显示容器运行全部记录
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
38f095df58c1 c20987f18b13 "docker-entrypoint.s…" 9 minutes ago Exited (0) 7 minutes ago youthful_clarke
a7963718f711 c20987f18b13 "docker-entrypoint.s…" 13 minutes ago Exited (1) 13 minutes ago friendly_kepler
[root@localhost /]#
docker rm[可选参数] image :删除容器
rm 可选参数:
Remove one or more containers
Options:
-f, --force Force the removal of a running container (uses SIGKILL) (移除正在的容器)
-l, --link Remove the specified link (移除指定连接)
-v, --volumes Remove the volumes associated with the container (移除容器关联数据卷)
docker rm 容器id # 删除指定的容器,不能删除正在运行的容器,强制删除使用 rm -f
docker rm -f $(docker ps -aq) # 删除所有的容器
docker ps -a -q | xargs docker rm # 删除所有的容器
Docker启动和重启容器命令
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前运行的容器
docker kill 容器id #强制停止当前容器
其他命令
后台启动容器
#命令 docker run -d 镜像ID
[root@localhost /]# docker run -d c20987f18b13
c587f4387b92560788b2591d6d4fcc83c5d2149b1d9d5362544aadfd42a89017
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost /]#
#问题docker ps,发现MySQL停止了
#常见的坑,docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用了,就会自动停止
#nginx,容器启动后发现自己没有提供服务,就会立刻停止,就是没有程序了
Docker 查看日志
logs 可选参数:
[root@localhost /]# docker logs --help
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options:
--details Show extra details provided to logs
-f, --follow Follow log output # 跟踪日志输出
--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes) # 显示某个开始时间的所有日志
--tail string Number of lines to show from the end of the logs (default "all")
-t, --timestamps Show timestamps # 显示时间戳
--until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
[root@localhost /]#
docker logs -tf 容器id
docker logs --tail number 容器id #nummber为要显示的日志条数
docker logs -n number 容器id #number为要显示的日志条数
MobaXterm 实际操作:
[root@localhost /]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 5 months ago 448MB
centos latest 5d0da3dc9764 9 months ago 231MB
#docker容器后台运行,必须要有一个前台的进程,否则会自动停止
#编写shell脚本循环执行,使得centos容器保持运行状态
[root@localhost /]# docker run -d centos /bin/sh -c "while true;do echo hi;sleep 5;done"
6c94b90f2ca7fcd77b77ada2974ec9582a130315a43d107d968926840bc1c6ef
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c94b90f2ca7 centos "/bin/sh -c 'while t…" 6 seconds ago Up 5 seconds friendly_noyce
[root@localhost /]# docker logs -tf --tail 10 6c94b90f2ca7fcd
2022-06-15T03:39:56.014839022Z hi
2022-06-15T03:40:01.019979705Z hi
2022-06-15T03:40:06.025769245Z hi
2022-06-15T03:40:11.030432112Z hi
2022-06-15T03:40:16.036156839Z hi
2022-06-15T03:40:21.041203308Z hi
2022-06-15T03:40:26.048672225Z hi
2022-06-15T03:40:31.058255127Z hi
2022-06-15T03:40:36.061984758Z hi
2022-06-15T03:40:41.075577602Z hi
2022-06-15T03:40:46.083983779Z hi
2022-06-15T03:40:51.096404381Z hi
2022-06-15T03:40:56.109763375Z hi
2022-06-15T03:41:01.118786583Z hi
2022-06-15T03:41:06.122710210Z hi
2022-06-15T03:41:11.131146109Z hi
2022-06-15T03:41:16.136816858Z hi
2022-06-15T03:41:21.140617379Z hi
Docker查看容器中进程信息
# 命令 docker top 容器id
[root@localhost /]# docker top 6c94b90f2ca7
UID PID PPID C STIME TTY TIME CMD
root 54382 54367 0 11:39 ? 00:00:00 /bin/sh -c while true;do echo hi;sleep 5;done
root 55986 54382 0 11:44 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 5
[root@localhost /]#
Docker查看镜像的元数据
$ docker inspect 容器id
#docker inspect : 获取容器/镜像的元数据。
#语法
docker inspect [OPTIONS] NAME|ID [NAME|ID...]
inspect 可选参数:
[root@localhost ~]# docker inspect --help
Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Return low-level information on Docker objects
Options:
-f, --format string Format the output using the given Go template (指定返回值的模板文件。)
-s, --size Display total file sizes if the type is container (显示总的文件大小。)
--type string Return JSON for specified type (为指定类型返回JSON。)
[root@localhost ~]#
MobaXterm 实际操作:
[root@localhost ~]# docker inspect 6c94b90f2ca7
[
"Id": "6c94b90f2ca7fcd77b77ada2974ec9582a130315a43d107d968926840bc1c6ef",
"Created": "2022-06-15T03:39:55.433118825Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo hi;sleep 5;done"
],
"State":
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 54382,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-06-15T03:39:56.031108092Z",
"FinishedAt": "0001-01-01T00:00:00Z"
,
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/6c94b90f2ca7fcd77b77ada2974ec9582a130315a43d107d968926840bc1c6ef/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/6c94b90f2ca7fcd77b77ada2974ec9582a130315a43d107d968926840bc1c6ef/hostname",
"HostsPath": "/var/lib/docker/containers/6c94b90f2ca7fcd77b77ada2974ec9582a130315a43d107d968926840bc1c6ef/hosts",
"LogPath": "/var/lib/docker/containers/6c94b90f2ca7fcd77b77ada2974ec9582a130315a43d107d968926840bc1c6ef/6c94b90f2ca7fcd77b77ada2974ec9582a130315a43d107d968926840bc1c6ef-json.log",
Docker进入当前正在运行的容器
产生的原因:容器使用后台方式运行时, 需要进入容器,修改一些配置。
方式一:通过 docker exex[可选参数] 容器ID Command指令
#语法
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
#exec 可选参数:
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
Options:
-d, --detach Detached mode: run command in the background (分离模式: 在后台运行)
--detach-keys string Override the key sequence for detaching a container
-e, --env list Set environment variables(设置环境变量)
-i, --interactive Keep STDIN open even if not attached (即使没有附加也保持STDIN 打开)
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY (分配一个伪终端)
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
-w, --workdir string Working directory inside the container
MobaXterm 实际操作:
[root@localhost ~]# docker exec -it 6c94b90f2ca7 /bin/bash
[root@6c94b90f2ca7 /]# pwd
/
[root@6c94b90f2ca7 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 05:32 ? 00:00:00 /bin/sh -c while true;do echo
root 385 0 0 06:04 pts/0 00:00:00 /bin/bash
root 400 1 0 06:04 ? 00:00:00 /usr/bin/coreutils --coreutils
root 401 385 0 06:04 pts/0 00:00:00 ps -ef
[root@6c94b90f2ca7 /]#
方式二:通过 docker attach[可选参数] 容器ID (不推荐使用)
#语法
docker attach[OPTIONS] CONTAINER
#attach可选参数:
Usage: docker attach [OPTIONS] CONTAINER
Attach local standard input, output, and error streams to a running container
Options:
--detach-keys string Override the key sequence for detaching a container
--no-stdin Do not attach STDIN
--sig-proxy Proxy all received signals to the process (default true)
MobaXterm 实际操作:
[root@localhost ~]# docker attach 6c94b90f2ca7
hi
hi
hi
hi
拷贝容器文件到主机
docker cp 容器id:容器内路径 目的主机路径
MobaXterm 实际操作:
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c94b90f2ca7 centos "/bin/sh -c 'while t…" 3 hours ago Up 3 hours friendly_noyce
[root@localhost ~]# docker exec -it 6c94b90f2ca7 /bin/bash
[root@6c94b90f2ca7 /]# cd /home/
[root@6c94b90f2ca7 home]# ls -a
. ..
# 在指定容器内的/home 文件夹下创建test.java 文件
[root@6c94b90f2ca7 home]# vi test.java
[root@6c94b90f2ca7 home]# docker cp 6c94b90f2ca7:/home/test.java /home
bash: docker: command not found
# 退出指定容器
[root@6c94b90f2ca7 home]# exit
exit
# 执行容器文件拷贝至宿主主机(/home 文件夹下)
[root@localhost ~]# docker cp 6c94b90f2ca7:/home/test.java /home
[root@localhost ~]# cd /home
[root@localhost home]# ls -a
. .. test.java
[root@localhost home]#
常用命令小结
Docker图形化管理工具
Docker UI
运行命令:
docker search dockerui
docker pull abh1nav/dockerui
docker run -d --privileged --name dockerui -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock abh1nav/dockerui
#放开物理机的9000端口对应Docker容器的9000端口
MobaXterm 实际操作:
[root@localhost home]# docker search dockerui
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
abh1nav/dockerui An updated version of crosbymichael/dockerui… 99 [OK]
kevan/dockerui Deprecated: Use uifd/ui-for-docker 15 [OK]
microbox/dockerui Trusted Automated dockerui image (16MB size) 9
madhavkobal/dockerui Docker Updated Version having Search, Pull, … 7
mgtsai/dockerui.base-xpra Provide base docker images for X application… 2
elegoev/dockerui dockerui image based on crosbymichael/docker… 1 [OK]
rediceli/dockerui Dockerui with nginx for basic auth 1
navionics/dockerui Docker UI 1 [OK]
winking/dockerui The latest DockerUI build, see https://githu… 0
******
[root@localhost home]# docker pull abh1nav/dockerui
Using default tag: latest
latest: Pulling from abh1nav/dockerui
a3ed95caeb02: Pull complete
5d3df020ecd3: Pull complete
bebf5a3b4dfb: Pull complete
e4452c0fe72b: Pull complete
6167d9726b07: Pull complete
53ebae19a314: Pull complete
Digest: sha256:a9c6c5393f561a0f42f41cfa80572b666e745d9b419569c42bac1e5cf9ceda32
Status: Downloaded newer image for abh1nav/dockerui:latest
[root@localhost home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 5 months ago 448MB
centos latest 5d0da3dc9764 9 months ago 231MB
abh1nav/dockerui latest 6e4d05915b2a 7 years ago 470MB
[root@localhost home]# docker run -d --privileged --name dockerui -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock abh1nav/dockerui
WARNING: IPv4 forwarding is disabled. Networking will not work.
97f024c55d667e6b2ea6efa0e019a1118358f396f3bc112adf2d77a7fc17a95a
访问地址:http://宿主主机ip地址:9000
界面截图:
常见容器部署:Nginx, Tomcat, Elasticsearch
Nginx
MobaXterm 实际操作:
[root@localhost etc]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 5 months ago 448MB
centos latest 5d0da3dc9764 9 months ago 231MB
abh1nav/dockerui latest 6e4d05915b2a 7 years ago 470MB
# 查找nginx 镜像服务
[root@localhost etc]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 16950 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 169
bitnami/nginx Bitnami nginx Docker Image 131 [OK]
ubuntu/nginx Nginx, a high-performance reverse proxy & we… 51
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 18 [OK]
******
# 下载nginx 镜像
[root@localhost etc]# 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
[root@localhost etc]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 5 months ago 141MB
mysql 5.7 c20987f18b13 5 months ago 448MB
centos latest 5d0da3dc9764 9 months ago 231MB
abh1nav/dockerui latest 6e4d05915b2a 7 years ago 470MB
# 运行nginx
[root@localhost etc]# docker run -d --name nginx -p 3344:80 nginx
a3eb2528b46c0e1a5742429e45877c3f7ab601c3b77b17977772732c901a7e29
# 进入正在运行nginx 容器后台
[root@localhost etc]# docker exec -it nginx /bin/bash
root@a3eb2528b46c:/# where is nginx
bash: where: command not found
root@a3eb2528b46c:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@a3eb2528b46c:/# cd /etc/nginx
root@a3eb2528b46c:/etc/nginx# ls -a
. conf.d mime.types nginx.conf uwsgi_params
.. fastcgi_params modules scgi_params
root@a3eb2528b46c:/etc/nginx# cat nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events
worker_connections 1024;
******
# 退出容器
root@a3eb2528b46c:/etc/nginx# exit
[root@localhost etc]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a3eb2528b46c nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 0.0.0.0:3344->80/tcp nginx
97f024c55d66 abh1nav/dockerui "./dockerui" About an hour ago Up About an hour 0.0.0.0:9000->9000/tcp dockerui
# 停止容器
[root@localhost etc]# docker stop a3eb2528b46c
a3eb2528b46c
测试访问:
核心指令:
# 查找nginx 镜像服务
[root@localhost etc]# docker search nginx
# 下载nginx 镜像
[root@localhost etc]# docker pull nginx
# 运行nginx
[root@localhost etc]# docker run -d --name nginx -p 3344:80 nginx
# 备注
# -d 后台运行
# --name 给容器命名
# -p 3334:80 将宿主机的端口3334映射到该容器的80端口
Tomcat
MobaXterm 实际操作:
[root@localhost etc]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 5 months ago 141MB
mysql 5.7 c20987f18b13 5 months ago 448MB
centos latest 5d0da3dc9764 9 months ago 231MB
abh1nav/dockerui latest 6e4d05915b2a 7 years ago 470MB
# 查账tomcat 镜像
[root@localhost etc]# docker search tomcat
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
tomcat Apache Tomcat is an open source implementati… 3341 [OK]
tomee Apache TomEE is an all-Apache Java EE certif… 97 [OK]
******
# 下载tomcat 指定版本8.0
[root@localhost etc]# docker pull tomcat:8.0
8.0: Pulling from library/tomcat
f189db1b88b3: Pull complete
3d06cf2f1b5e: Pull complete
edd0da9e3091: Pull complete
eb7768aae14e: Pull complete
e2780f585e0f: Pull complete
e5ed720afeba: Pull complete
d9e134700cfc: Pull complete
e4804b33d02a: Pull complete
b9df0c24315e: Pull complete
49fdae8eaa20: Pull complete
1aea3d9a32e6: Pull complete
Digest: sha256:8ecb10948deb32c34aeadf7bf95d12a93fbd3527911fa629c1a3e7823b89ce6f
Status: Downloaded newer image for tomcat:8.0
[root@localhost etc]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 5 months ago 141MB
tomcat latest fb5657adc892 5 months ago 680MB
mysql 5.7 c20987f18b13 5 months ago 448MB
centos latest 5d0da3dc9764 9 months ago 231MB
tomcat 8.0 ef6a7c98d192 3 years ago 356MB
abh1nav/dockerui latest 6e4d05915b2a 7 years ago 470MB
# 启动tomcat 指定版本8.0
[root@localhost etc]# docker run -d -p 3355:8080 --name tomcat ef6a7c98d192
5722a9fd9fbb0be727954399b939d2a89e43b84791a792e04216c0dcd9d65d2c
# 进入容器
[root@localhost etc]# docker exec -it tomcat /bin/bash
root@65b4405c78c7:/usr/local/tomcat# whereis tomcat
tomcat: /usr/local/tomcat
root@65b4405c78c7:/usr/local/tomcat# pwd
/usr/local/tomcat
root@65b4405c78c7:/usr/local/tomcat# ls -a
. LICENSE RUNNING.txt logs webapps.dist
.. NOTICE bin native-jni-lib work
BUILDING.txt README.md conf temp
CONTRIBUTING.md RELEASE-NOTES lib webapps
root@65b4405c78c7:/usr/local/tomcat# cd webapps
root@65b4405c78c7:/usr/local/tomcat/webapps# ls -a
. ..
root@65b4405c78c7:/usr/local/tomcat/webapps# cp -r webapps.dist/* webapp
cp: cannot stat 'webapps.dist/*': No such file or directory
root@65b4405c78c7:/usr/local/tomcat/webapps# cp -r webapps.dist/* webapps
cp: cannot stat 'webapps.dist/*': No such file or directory
root@65b4405c78c7:/usr/local/tomcat/webapps# ls -a
. ..
root@65b4405c78c7:/usr/local/tomcat/webapps# cd ..
root@65b4405c78c7:/usr/local/tomcat# ls -a
. LICENSE RUNNING.txt logs webapps.dist
.. NOTICE bin native-jni-lib work
BUILDING.txt README.md conf temp
CONTRIBUTING.md RELEASE-NOTES lib webapps
# 发现问题:1.linux命令少了, 2. webapps下内容为空,阿里云默认是最小的镜像,所有不必要的都剔除了
# 保证最小可运行环境即可
root@65b4405c78c7:/usr/local/tomcat# cp -r webapps.dist/* webapps
# 网站恢复访问
root@65b4405c78c7:/usr/local/tomcat# exit
exit
测试访问:
核心指令:
# 查账tomcat 镜像
[root@localhost etc]# docker search tomcat
# 下载tomcat 指定版本8.0
[root@localhost etc]# docker pull tomcat:8.0
# 启动tomcat 指定版本8.0
[root@localhost etc]# docker run -d -p 3355:8080 --name tomcat ef6a7c98d192
# 进入容器
[root@localhost etc]# docker exec -it tomcat /bin/bash
# 发现问题:1.linux命令少了, 2. webapps下内容为空,阿里云默认是最小的镜像,所有不必要的都剔除了
# 保证最小可运行环境即可
root@65b4405c78c7:/usr/local/tomcat# cp -r webapps.dist/* webapps
# 网站恢复访问
Elasticsearch
重点提示:由于Elasticsearch 占用虚拟机内存严重,需要添加’-e ES_JAVA_OPTS=“-Xms128m -Xmx512m” ‘ 配置ElasticSearch的虚拟机占用的内存大小。
MobaXterm 实际操作:
[root@localhost etc]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 5 months ago 141MB
tomcat latest fb5657adc892 5 months ago 680MB
mysql 5.7 c20987f18b13 5 months ago 448MB
centos latest 5d0da3dc9764 9 months ago 231MB
tomcat 8.0 ef6a7c98d192 3 years ago 356MB
abh1nav/dockerui latest 6e4d05915b2a 7 years ago 470MB
# 查询ElasticSearch 镜像
[root@localhost etc]# docker search elasticsearch
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
elasticsearch Elasticsearch is a powerful open source sear… 5657 [OK]
kibana Kibana gives shape to any kind of data — str… 2416 [OK]
bitnami/elasticsearch Bitnami Docker Image for Elasticsearch 53 [OK]
bitnami/elasticsearch-exporter Bitnami Elasticsearch Exporter Docker Image 4 [OK]
******
# 下载Elasticsearch指定版本7.6.2
[root@localhost etc]# docker pull elasticsearch:7.6.2
7.6.2: Pulling from library/elasticsearch
ab5ef0e58194: Pull complete
c4d1ca5c8a25: Pull complete
941a3cc8e7b8: Pull complete
43ec483d9618: Pull complete
c486fd200684: Pull complete
1b960df074b2: Pull complete
1719d48d6823: Pull complete
Digest: sha256:1b09dbd93085a1e7bca34830e77d2981521a7210e11f11eda997add1c12711fa
Status: Downloaded newer image for elasticsearch:7.6.2
[root@localhost etc]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 5 months ago 141MB
tomcat latest fb5657adc892 5 months ago 680MB
mysql 5.7 c20987f18b13 5 months ago 448MB
centos latest 5d0da3dc9764 9 months ago 231MB
elasticsearch 7.6.2 f29a1ee41030 2 years ago 791MB
tomcat 8.0 ef6a7c98d192 3 years ago 356MB
abh1nav/dockerui latest 6e4d05915b2a 7 years ago 470MB
# 运行Elasticsearch
[root@localhost etc]# docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2
测试访问:
核心指令:
# 查询ElasticSearch 镜像
[root@localhost etc]# docker search elasticsearch
# 下载Elasticsearch指定版本7.6.2
[root@localhost etc]# docker pull elasticsearch:7.6.2
# 直接启动Elasticsearch7.6.2
[root@localhost etc]# docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
# 启动后linux就卡住了,docker stats 查看cpu状态
[root@localhost etc]# docker stats
# es 暴露的端口很多
# es 十分的耗内存
# es 的数据一般需要放置到安全目录! 挂载
# --net somenetwork ? 网络配置
# 增加内存限制,修改配置文件 -e 环境配置修改
[root@localhost etc]# docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2
Docker镜像讲解
镜像是什么
Docker镜像是一种轻量级、可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含某个软件所需的所有内容,包括代码、库、环境变量、配置文件、运行时环境等。
所有的应用,直接打包成Docker镜像,然后通过镜像创建出容器,然后就可以直接跑起来。
如何得到镜像:
- 从远程仓库下载,比如docker hub、阿里云的镜像仓库等。
- 朋友拷贝给你。
- 自己制作一个镜像DockerFile。
通过对原有的镜像创建的容器进行一些修改(也可以不修改),然后通过Commit命令提交一个新的镜像。
UnionFS(联合文件系统)
- 联合文件系统(UnionFS)是一种分层、轻量级并且高性能的文件系统,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下。联合文件系统是 Docker 镜像的基础。镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。
- 特性:一次同时加载多个文件系统,但从外面看起来只能看到一个文件系统。联合加载会把各层文件系统叠加起来,这样最终的文件系统会包含所有底层的文件和目录。
Docker镜像加载原理
docker镜像实际上是由一层层的文件系统组成,这种层级文件系统就是联合文件系统。
- bootfs(boot file system)主要包含BootLoader和kernel,BootLoader主要负责引导加载kernel,Linux刚启动时会加载bootfs文件系统来引导内核的加载,Docker镜像的最底层就是bootfs。这一层与我们典型的unix系统是一样的,包含boot引导器和内核,当boot加载完成后整个内核就在内存中了,此时内存的使用权已经由bootfs转交给内核,此时系统会卸载bootfs。
- rootfs(root file system),包含典型Linux系统中的/dev,/proc,/bin,/etc等标准目录和文件。rootfs就是各种不同Unix操作系统发行版(Ubuntu,Centos等)。因为底层直接用Host的kernel,rootfs只包含最基本的命令,工具和程序就可以了。
我们平时安装的虚拟机centos镜像好几个G,Docker安装的才200多M,因为对于一个精简的OS,rootfs可以很小,只需包含最基本的命令,工具和程序库就行了,因为底层直接使用宿主机的内核,自己只需提供rootfs(相当于操作内核的客户端)就可以,由此可见不同发行版的bootfs基本是一致的,roorfs有差别,因此不同的发行版可以公有bootfs。虚拟机是分钟级别,容器是秒级。
第一个图仅仅是bootfs+rootfs,然后如果要制作一个emacs环境的镜像,就在这个基础上新加一层emacs镜像,如图二。如果要在添加一个Apache环境,那就再图二基础上加一个apache镜像。如图三。图中的每一层镜像都能进行复用。
分层理解
[root@localhost etc]# docker pull elasticsearch:7.6.2
7.6.2: Pulling from library/elasticsearch
ab5ef0e58194: Pull complete
c4d1ca5c8a25: Pull complete
941a3cc8e7b8: Pull complete
43ec483d9618: Pull complete
c486fd200684: Pull complete
1b960df074b2: Pull complete
1719d48d6823: Pull complete
Digest: sha256:1b09dbd93085a1e7bca34830e77d2981521a7210e11f11eda997add1c12711fa
Status: Downloaded newer image for elasticsearch:7.6.2
比如:上面的elasticsearch镜像。使用docker inspect elasticsearch镜像的ID 命令查看镜像的元信息,找到layer信息。
由上图可以看到下载的Elasticsearch镜像是由7个镜像一层层组成的。
这些镜像都是一个个独立可复用的镜像,如果下载其他镜像是,某一层镜像是已经存在本地的了,就不用在下载,直接复用该镜像,节省空间。比如上面下载redis镜像时,提示某个镜像已经存在。
温馨提示:Docker镜像都是只读的,用镜像创建容器启动时,实际上是在原本的镜像上新建了一层可写层到原本镜像的顶部,这一层我们叫作容器层,容器层之下的叫作镜像层。
如上图,使用ElasticSearch镜像创建容器后,会在ElasticSearch镜像的基础上新建一个可写层,容器的写入是在可写层进行记录,然后使用commit命令把该容器创建一个新的镜像,实际上新的镜像是ElasticSearch镜像+可写层镜像,以ElasticSearch镜像为基础。通过下面介绍使用容器构建镜像,可以更好地理解。
镜像提交
常用命令:
docker commit 提交容器成为一个新的副本
docker commit -m="描述信息,类似git提交的信息" -a="作者" 容器id 目标镜像名:[TAG]$ 编辑容器后提交容器成为一个新镜像a
MobaXterm 实际操作:
#1.启动一个默认的tomcat
docker run -it -p 8080:8080 tomcat
#2.发现这个默认的tomcat是没有webapps的应用,镜像的原因,官方的镜像默认webapps下面是没有文件的
#3.自己拷贝进去基本的文件
cp -r webapps.dist/* webapps
#4.将修改后的容器通过commit提交为新的镜像~我们以后就使用我们自己制作的镜像了
docker commit =a="zzg" -m="add webapps app" 容器ID tomcat02:1.0
容器数据卷
什么是容器数据卷
为了实现数据持久化,使容器之间可以共享数据。可以将容器内的目录,挂载到宿主机上或其他容器内,实现同步和共享的操作。即使将容器删除,挂载到本地的数据卷也不会丢失。
Docker理念
将应用和环境打包成一个镜像
数据如果都在容器中,那么我们容器一旦删除,数据就会丢失 需求:数据可以持久化
MySQL,容器删了,删库跑路 需求:MySQL数据可以存储在本地
容器之间可以有一个数据共享的技术,Docker容器中产生的数据同步到本地
这就是卷技术,目录的挂载将我们容器内的目录挂载到Linux上面
容器的持久化和同步操作,容器间也是可以做数据共享的。
使用数据卷
方式一:直接使用命令来挂载 -v
语法:
docker run -it -v 主机目录:容器内目录
MobaXterm 实际操作:
[root@localhost etc]# pwd
/etc
[root@localhost etc]# cd /home
# 查询/home 目录下相关文件
[root@localhost home]# ls -a
. .. test.java
# 容器卷挂载
[root@localhost ceshi]# docker run -dit -v /home/ceshi:/home centos /bin/sh -c "while true;do echo hi;sleep 5;done"
# 查看容器状态
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fe52a516f65e centos "/bin/sh -c 'while t…" 47 seconds ago Up 46 seconds reverent_bassi
2688accce5f9 elasticsearch:7.6.2 "/usr/local/bin/dock…" About an hour ago Up About an hour 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp elasticsearch
5722a9fd9fbb ef6a7c98d192 "catalina.sh run" 2 hours ago Up 2 hours 0.0.0.0:3355->8080/tcp tomcat
97f024c55d66 abh1nav/dockerui "./dockerui" 4 hours ago Up 3 hours 0.0.0.0:9000->9000/tcp dockerui
# 进入正在运行的容器后台
[root@localhost ~]# docker exec -it fe52a516f65e /bin/bash
[root@fe52a516f65e /]# cd /home
[root@fe52a516f65e home]# ls -a
. ..
# 在/home 文件目录新增volumn.txt 文件
[root@fe52a516f65e home]# vi volumn.txt
[root@fe52a516f65e home]# exit
exit
# 查看宿主主机上的/home/ceshi 文件夹下是否有新增volumn.txt文件
[root@localhost ~]# cd /home/ceshi
[root@localhost ceshi]# ls -a
. .. volumn.txt
#查看挂载情况
[root@localhost ceshi]# docker inspect fe52a516f65e
实战:安装MySQL
1.安装 Mysql 5.7 docker镜像
docker pull mysql:5.7
2.查看镜像库
docker images
3.创建mysql容器
3.1 在本地创建mysql的映射目录
mkdir -p /root/mysql/data /root/mysql/logs /root/mysql/conf
在/root/mysql/conf中创建 *.cnf 文件(叫什么都行)
touch my.cnf
3.2 创建容器,将数据,日志,配置文件映射到本机
docker run -p 3306:3306 --name mysql -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/logs:/logs -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
-d: 后台运行容器
-p 将容器的端口映射到本机的端口
-v 将主机目录挂载到容器的目录
-e 设置参数
3.3 查看/root/mysql/data目录是否有数据文件
MobaXterm 实际操作:
[root@localhost ceshi]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 5 months ago 141MB
tomcat latest fb5657adc892 5 months ago 680MB
mysql 5.7 c20987f18b13 5 months ago 448MB
centos latest 5d0da3dc9764 9 months ago 231MB
elasticsearch 7.6.2 f29a1ee41030 2 years ago 791MB
tomcat 8.0 ef6a7c98d192 3 years ago 356MB
abh1nav/dockerui latest 6e4d05915b2a 7 years ago 470MB
[root@localhost ceshi]# cd /
[root@localhost /]# mkdir -p /root/mysql/data /root/mysql/logs /root/mysql/conf
[root@localhost /]# cd /root/mysql/conf
[root@localhost conf]# touch my.cnf
[root@localhost conf]# docker run -p 3306:3306 --name mysql -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/logs:/logs -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
dacc4ce950337e1a3e7ec39451340a2428d75b5b36deb08ba9d6f0fb3cf386cd
[root@localhost conf]# cd /root/mysql/data
[root@localhost data]# ls -a
. auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sys
.. ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem
[root@localhost data]#
使用Native For MySQL工具连接测试:
具名和匿名挂载
#匿名挂载
-v 容器内挂载
docker run -d -P --name nginx01 -v /etc/nginx nginx $ -P 随机指定端口
#查看所有的volume的情况
$ docker volume ls
DRIVER VOLUME NAME
local 8026aa409bf4851f9056cc3092e27493db1ee654b461b1ea05c3bedfafed3e32
#具名挂载
$ docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
72bf479b190b74d53256311e626bab459630af2028b9c9e1a83064c2ff745d9e
$ docker volume ls
DRIVER VOLUME NAME
local 8026aa409bf4851f9056cc3092e27493db1ee654b461b1ea05c3bedfafed3e32
local 04862392ad0fcff6ecabd65989ae57e109a9963de0d517f4e8b305b44700795a
local d7111fd00a6d3704ec4bd5419eee07ba6c4bac699abe227b908f6d385188c472
local jumping-nginx
# 通过-v 卷名:容器内的路径
# 查看一下这个卷
# docker volume inspect 卷名
$ docker volume inspect juming-nginx
[
"CreatedAt": "2022-04-19T14:52:24+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
"Name": "jumping-nginx",
"Options": null,
"Scope": "local"
]
知识点:
#如何确定是具名挂载还是匿名挂载,还是指定路径挂载
-v 容器内路径 #匿名挂载
-v 卷名:容器内路径 #具名挂载
-v /宿主机路径::容器内路径 #指定路径挂载
拓展:
#通过-v容器内路径,ro rw改变读写权限
ro read only #只读
rw read write #可读可写
#一旦这个设置了容器权限,容器对我们挂载出来的内容就有限定了
$ docker run -d -P --name nginx02 -v jumping-nginx:/etc/nginx:ro nginx
$ docker run -d -P --name nginx02 -v jumping-nginx:/etc/nginx:rw nginx
#ro 只要看到ro就说明这个路径是只能通过宿主机来操作,容器内部是无法操作
DockerFile
DockerFile介绍
Dockerfile是用来构建docker镜像的文件,命令参数脚本
构建步骤:
- 构建一个dockerfile文件
- docker build 构建成为一个镜像
- docker run 运行镜像
- docker push 发布镜像(DockerHub、阿里云镜像仓库)
查看一下官方是怎么做的
很多官方镜像都是基础包,我们通常会自己搭建自己的镜像。
官方既然可以制作镜像,那我们也可以自己构建。
DockerFile构建过程
基础知识:
-
每个保留关键字(指令)都必须是大写字母
-
执行顺序从上到下
-
#表示注释
-
每一个指令都会创建提交一个新的镜像层,并提交!
Docker镜像原理
Dockerfile概念及作用
Dockerfile 关键字
FROM # 基础镜像
MAINTAINER # 镜像谁写的,姓名+邮箱
RUN # 镜像构建的时候需要运行的命令
ADD # 步骤:centos镜像,centos压缩包!添加内容
WORKDIR # 镜像的工作目录
VOLUME # 挂载的目录
EXPOSE # 暴露端口配置
CMD # 指定这个容器启动的时候运行的命令,只有最后一个会生效,可以被替代
ENTRYPOINT # 指定这个容器启动的时候要运行的命令,可以追加命令
ONBUILD # 当构建一个被继承 DockerFile 这个时候就会运行 ONBUILD 的指令。触发指令
COPY # 类似ADD,将我们文件拷贝到镜像中
ENV # 构建的时候设置环境变量
实战测试
Docker Hub中99%镜像都是从这个基础镜像过来的FROM scratch
,然后配置需要的软件和配置来进行构建。
- 创建一个自己的centos
MobaXterm 实际操作:
# 编辑dockerfile-centos 文件
[root@localhost ~]# vim dockerfile-centos
[root@localhost ~]# ls -a
. .. anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cshrc dockerfile-centos mysql .pki .tcshrc .viminfo
# 编译dockerfile-centos 文件
[root@localhost ~]# docker build -f dockerfile-centos -t mycentos:0.1 .
Sending build context to Docker daemon 219.8MB
Step 1/8 : FROM centos:latest
---> 5d0da3dc9764
Step 2/8 : MAINTAINER zhouzhiwengang<zhouzhiwengang@163.com>
---> Using cache
---> c7877d372938
Step 3/8 : ENV MYPATH /user/local
---> Using cache
---> d6232b2fbf27
Step 4/8 : WORKDIR $MYPATH
---> Using cache
---> 1f73cff95efe
Step 5/8 : EXPOSE 80
---> Running in d90f6378290a
Removing intermediate container d90f6378290a
---> 397d16dc87d1
Step 6/8 : CMD echo $MYPATH
---> Running in 815375f27354
Removing intermediate container 815375f27354
---> 7c6fa3907394
Step 7/8 : CMD echo "-----end-----"
---> Running in 300078bc3e8c
Removing intermediate container 300078bc3e8c
---> ccb4425fb27a
Step 8/8 : CMD /bin/bash
---> Running in 58cb10d51d54
Removing intermediate container 58cb10d51d54
---> d42edc289e9c
Successfully built d42edc289e9c
Successfully tagged mycentos:0.1
# 查询镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 0.1 d42edc289e9c 7 seconds ago 231MB
nginx latest 605c77e624dd 5 months ago 141MB
tomcat latest fb5657adc892 5 months ago 680MB
mysql 5.7 c20987f18b13 5 months ago 448MB
centos latest 5d0da3dc9764 9 months ago 231MB
elasticsearch 7.6.2 f29a1ee41030 2 years ago 791MB
tomcat 8.0 ef6a7c98d192 3 years ago 356MB
abh1nav/dockerui latest 6e4d05915b2a 7 years ago 470MB
# 运行自定义镜像文件
[root@localhost ~]# docker run -it mycentos:0.1
[root@6c55f06e8eea local]# pwd
/user/local
[root@6c55f06e8eea local]# exit
编辑dockerfile-centos 内容:
[root@localhost ~]# cat dockerfile-centos
FROM centos:latest
MAINTAINER zhouzhiwengang<zhouzhiwengang@163.com>
ENV MYPATH /user/local
WORKDIR $MYPATH
EXPOSE 80
CMD echo $MYPATH
CMD echo "-----end-----"
CMD /bin/bash
[root@localhost ~]#
CMD 和 ENTRYOINT 的区别
CMD # 指定这个容器启动的时候运行的命令,只有最后一个会生效,可以被替代
ENTRYPOINT # 指定这个容器启动的时候要运行的命令,可以追加命令
实战:Tomcat镜像
- 准备 tomcat 和JDK压缩包
从Oracle JDK 官网找到JDK8版本。
从Tomcat 8 官网找到Tomcat8版本。
将下载好的apache-tomcat-8.5.81.tar.gz 和 jdk-8u333-linux-x64.tar.gz 上传CentOS 服务器的/usr/local目录下。
2.编写dockerfile文件,我这里将它明明为:dockerfile-tomcat
MobaXterm 实际操作:
# 编辑dockerfile-tomcat文件
[root@localhost local]# vim dockerfile-tomcat
# 编译dockerfile-tomcat 文件
[root@localhost local]# docker build -f dockerfile-tomcat -t mytocmat:0.1 .
Sending build context to Docker daemon 543.9MB
Step 1/14 : FROM centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7
---> eeb6ee3f44bd
Step 2/14 : MAINTAINER zhouzhiwengang<zhouzhiwengang@163.com>
---> Running in 28155bdd30d5
Removing intermediate container 28155bdd30d5
---> 112ca76a8227
Step 3/14 : ADD jdk-8u333-linux-x64.tar.gz /usr/local
---> f1f6b9c1b86a
Step 4/14 : ADD apache-tomcat-8.5.81.tar.gz /usr/local
---> 8ca2bbb1b2fc
Step 5/14 : RUN yum -y install vim
---> Running in ecca98c27761
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.ustc.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package vim-enhanced.x86_64 2:7.4.629-8.el7_9 will be installed
--> Processing Dependency: vim-common = 2:7.4.629-8.el7_9 for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Processing Dependency: which for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Processing Dependency: perl(:MODULE_COMPAT_5.16.3) for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Processing Dependency: libperl.so()(64bit) for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Processing Dependency: libgpm.so.2()(64bit) for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Running transaction check
---> Package gpm-libs.x86_64 0:1.20.7-6.el7 will be installed
---> Package perl.x86_64 4:5.16.3-299.el7_9 will be installed
--> Processing Dependency: perl(Socket) >= 1.3 for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Scalar::Util) >= 1.10 for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl-macros for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Proces以上是关于Docker 一文读懂的主要内容,如果未能解决你的问题,请参考以下文章