探索云原生技术之基石-Docker容器入门篇(2)【与云原生的故事】
❤️💛🧡💚💔🖤💜💙❤💕💞💓💗❣️💝💘💖❤️💛🧡💚💔🖤💜💙❤💕💞💓💗❣️💝💘💖💖💖 💖
❤️作者简介:2022新星计划第三季云原生与云计算赛道Top5🏅、华为云享专家🏅、云原生领域潜力新星🏅 💖
💛博客首页:C站个人主页🌞 💖
💗作者目的:如有错误请指正,将来会不断的完善笔记,帮助更多的Java爱好者入门,共同进步! 💖
❤️💛🧡💚💔🖤💜💙❤💕💞💓💗❣️💝💘💖❤️💛🧡💚💔🖤💜💙❤💕💞💓💗❣️💝💘💖💖💖 💖
@[toc]
探索云原生技术之基石-Docker容器入门篇(2)
本博文一共有6篇,如下
- 探索云原生技术之基石-Docker容器入门篇(1)
- 探索云原生技术之基石-Docker容器入门篇(2)
- 探索云原生技术之基石-Docker容器入门篇(3)
等你对Docker有一定理解的时候可以看高级篇,不过不太建议。
- 探索云原生技术之基石-Docker容器高级篇(1)
- 探索云原生技术之基石-Docker容器高级篇(2)
- 探索云原生技术之基石-Docker容器高级篇(3)-视情况而定
剧透:未来将出云原生技术-Kubernetes(k8s),此时的你可以对Docker进行统一管理、动态扩缩容等等。
看完之后你会对Docker有一定的理解,并能熟练的使用Docker进行容器化开发、以及Docker部署微服务、Docker网络等等。干起来!
什么是云原生
Pivotal公司的Matt Stine于2013年首次提出云原生(Cloud-Native)的概念;2015年,云原生刚推广时,Matt Stine在《迁移到云原生架构》一书中定义了符合云原生架构的几个特征:12因素、微服务、自敏捷架构、基于API协作、扛脆弱性;到了2017年,Matt Stine在接受InfoQ采访时又改了口风,将云原生架构归纳为模块化、可观察、可部署、可测试、可替换、可处理6特质;而Pivotal最新官网对云原生概括为4个要点:DevOps+持续交付+微服务+容器。
总而言之,符合云原生架构的应用程序应该是:采用开源堆栈(K8S+Docker)进行容器化,基于微服务架构提高灵活性和可维护性,借助敏捷方法、DevOps支持持续迭代和运维自动化,利用云平台设施实现弹性伸缩、动态调度、优化资源利用率。
(此处摘选自《知乎-华为云官方帐号》)
什么是Docker
- Docker 是一个开源的应用容器引擎,基于Go语言开发。
- Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。
- 容器是完全使用沙箱机制(容器实例相互隔离),容器性能开销极低(高性能)。
总而言之:
Docker是一个高性能的容器引擎;
可以把本地源代码、配置文件、依赖、环境通通打包成一个容器即可以到处运行;
使用Docker安装软件十分方便,而且安装的软件十分精简,方便扩展。
docker命令
docker start命令
docker start命令参数与格式
[root@aubin /]# docker start --help
Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]
Start one or more stopped containers
Options:
-a, --attach Attach STDOUT/STDERR and forward signals
--detach-keys string Override the key sequence for detaching a container
-i, --interactive Attach container's STDIN
-
命令格式:docker start 容器id
-
作用:开启一个或多个状态为Exited(已经退出)的容器实例
-
先查询退出的容器:
[root@aubin ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c49a87af8c9 ubuntu "bash" 18 seconds ago Exited (0) 8 seconds ago cranky_ride
- 使用docker start启动容器
docker start 2c49a87af8c9
- 启动成功后就可以用docker exec去进入容器了。
docker restart命令
docker restart命令参数与格式
[root@aubin /]# docker restart --help
Usage: docker restart [OPTIONS] CONTAINER [CONTAINER...]
Restart one or more containers
Options:
-t, --time int Seconds to wait for stop before killing the container (default 10)
- 命令格式:docker restart 容器id
- 作用:重新启动容器实例
docker restart 2c49a87af8c9
docker stop命令
docker stop命令参数与格式
[root@aubin /]# docker stop --help
Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
Stop one or more running containers
Options:
-t, --time int Seconds to wait for stop before killing it (default 10)
- 命令格式:docker stop 容器id
- 作用:停止容器实例,把容器实例状态变成Exited
docker stop 2c49a87af8c9
docker kill命令
docker kill命令参数与格式
[root@aubin /]# docker kill --help
Usage: docker kill [OPTIONS] CONTAINER [CONTAINER...]
Kill one or more running containers
Options:
-s, --signal string Signal to send to the container (default "KILL")
- 命令格式:docker kill 容器id
- 作用:杀死一个或多个正在运行的容器实例
docker kill 2c49a87af8c9
docker rm命令
docker rm命令参数与格式
[root@aubin /]# docker rm --help
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
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 anonymous volumes associated with the container
- 命令格式:docker rm -f 容器id
- -f:force(强制移除容器),可选项
- 作用:移除一个或多个容器实例
docker rm -f 5b474c2309d9
docker run -d命令
docker run -d命令参数与格式
[root@aubin /]# docker run -d --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
-a, --attach list Attach to STDIN, STDOUT or STDERR
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--blkio-weight-device list Block IO weight (relative device weight) (default [])
--cap-add list Add Linux capabilities
--cap-drop list Drop Linux capabilities
--cgroup-parent string Optional parent cgroup for the container
--cgroupns string Cgroup namespace to use (host|private)
'host': Run the container in the Docker host's cgroup namespace
'private': Run the container in its own private cgroup namespace
'': Use the cgroup namespace as configured by the
default-cgroupns-mode option on the daemon (default)
省略......
- 命令格式:docker run -d REPOSITORY镜像仓库名
- -d:后台模式(守护模式)
- 作用:后台启动一个容器
docker run -d redis
docker logs命令
docker logs命令参数与格式
[root@aubin /]# 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:37Z) or relative (e.g. 42m for 42 minutes)
-n, --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:37Z) or relative (e.g. 42m for 42 minutes)
- 命令格式:docker logs 容器id
- 作用:获取容器日志
[root@aubin ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e1b9afb7322 redis "docker-entrypoint.s…" About a minute ago Up About a minute 6379/tcp recursing_faraday
[root@aubin ~]# docker logs 0e1b9afb7322
- 输出
1:C 06 Apr 2022 07:22:51.888 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 06 Apr 2022 07:22:51.888 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 06 Apr 2022 07:22:51.888 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 06 Apr 2022 07:22:51.889 * monotonic clock: POSIX clock_gettime
1:M 06 Apr 2022 07:22:51.890 * Running mode=standalone, port=6379.
1:M 06 Apr 2022 07:22:51.890 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 06 Apr 2022 07:22:51.890 # Server initialized
1:M 06 Apr 2022 07:22:51.890 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 06 Apr 2022 07:22:51.890 * Ready to accept connections
- 可以看到Redis已经启动与Redis版本等信息。
docker inspect命令
docker inspect命令参数与格式
[root@aubin /]# 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
- 命令格式:docker inspect 容器id/镜像id
- 作用:获取容器或者镜像的元数据。
docker inspect 0e1b9afb7322
- 输出:
[
{
"Id": "0e1b9afb7322c6f6f91684b5e97f3fc4bb826d56c2ed82c621f60973ea991cc9",
"Created": "2022-04-06T07:22:51.498243986Z",
"Path": "docker-entrypoint.sh",
"Args": [
"redis-server"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 6614,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-04-06T07:22:51.839540264Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:7614ae9453d1d87e740a2056257a6de7135c84037c367e1fffa92ae922784631",
"ResolvConfPath": "/var/lib/docker/containers/0e1b9afb7322c6f6f91684b5e97f3fc4bb826d56c2ed82c621f60973ea991cc9/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/0e1b9afb7322c6f6f91684b5e97f3fc4bb826d56c2ed82c621f60973ea991cc9/hostname",
"HostsPath": "/var/lib/docker/containers/0e1b9afb7322c6f6f91684b5e97f3fc4bb826d56c2ed82c621f60973ea991cc9/hosts",
"LogPath": "/var/lib/docker/containers/0e1b9afb7322c6f6f91684b5e97f3fc4bb826d56c2ed82c621f60973ea991cc9/0e1b9afb7322c6f6f91684b5e97f3fc4bb826d56c2ed82c621f60973ea991cc9-json.log",
......
]
docker exec命令
docker exec命令参数与格式
[root@aubin /]# docker exec --help
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
--env-file list Read in a file of environment variables
-i, --interactive Keep STDIN open even if not attached
--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
- 命令格式:docker exec -it 容器id
- 作用:进入一个状态为Up的容器(这个容器一定不能被关闭,不然进不去)
docker exec -it 0e1b9afb7322 /bin/bash
docker cp命令
docker cp命令参数与格式
[root@aubin /]# docker cp --help
Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Copy files/folders between a container and the local filesystem
Use '-' as the source to read a tar archive from stdin
and extract it to a directory destination in a container.
Use '-' as the destination to stream a tar archive of a
container source to stdout.
Options:
-a, --archive Archive mode (copy all uid/gid information)
-L, --follow-link Always follow symbol link in SRC_PATH
-
命令格式:docker cp 容器id:容器文件路径 主机路径
-
作用:当我们不在容器内,可以用docker cp把指定容器内的文件拷贝到主机路径中。
docker cp 5e464b07dd68:b.txt /var
docker export命令
docker export命令参数与格式
[root@aubin /]# docker export --help
Usage: docker export [OPTIONS] CONTAINER
Export a container's filesystem as a tar archive
Options:
-o, --output string Write to a file, instead of STDOUT
- 命令格式:docker export 容器id > xxx.tar
- 作用:把指定容器导出成tar包。
[root@aubin /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e464b07dd68 ubuntu "bash" About an hour ago Up About an hour heuristic_elbakyan
[root@aubin /]# docker export 5e464b07dd68 > abc.tar
docker import命令
docker import命令参数与格式
[root@aubin /]# docker import --help
Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Import the contents from a tarball to create a filesystem image
Options:
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Set commit message for imported image
--platform string Set platform if server is multi-platform capable
- 命令格式:docker import tar包名 自定义镜像名:自定义tag名
- 作用:把tar包导入成镜像
docker import abc.tar myubuntu:5.2
docker commit命令
docker commit命令参数与格式
[root@aubin /]# docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
安装vim命令
apt-get update
apt-get -y install vim
commit提交成镜像
-
命令格式:docker commit 容器id 自定义镜像仓库名:自定义tag
-
作用:把容器提交成镜像
docker commit f82b9b4f63b0 myubuntu:6.6
[root@aubin ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myubuntu 6.6 5a035af5ac0c 5 seconds ago 176MB
mbuntu 1.3 b31fa70be935 3 hours ago 72.8MB
tomcat latest fb5657adc892 3 months ago 680MB
redis latest 7614ae9453d1 3 months ago 113MB
mysql 5.7 c20987f18b13 3 months ago 448MB
rabbitmq latest d445c0adc9a5 3 months ago 220MB
ubuntu latest ba6acccedd29 5 months ago 72.8MB
- 可以看到我们只添加一个vim命令后的Ubuntu比docker hub拉取的Ubuntu大了差不多100MB
- 由于可见docker hub里面的镜像都是精简版的。
❤️💛🧡本章结束,我们下一章见❤️💛🧡
【与云原生的故事】有奖征文火热进行中:https://bbs.huaweicloud.com/blogs/345260
- 点赞
- 收藏
- 关注作者
评论(0)