容器介绍

容器是Docker的另一个核心组件。

简单的说,容器是镜像的一个运行实例。如果认为虚拟机是模拟运行的一整套操作系统(提供了运行态环境和其他系统环境)和跑在上面的应用。那么Docker容器就是独立运行的一个或一组应用,以及它们的必需运行环境。

创建容器

通过镜像,创建容器,命令格式: docker create [OPTIONS] IMAGE [COMMAND] [ARG...]

关键Options

--name string Assign a name to the container

-p, --publish list Publish a container's port(s) to the host

-t, --tty Allocate a pseudo-TTY

--ulimit ulimit Ulimit options (default [])

-v, --volume list Bind mount a volume

--volume-driver string Optional volume driver for the container

--volumes-from list Mount volumes from the specified container(s)

-w, --workdir string Working directory inside the container

案例:创建 redis 的容器

root@ubuntu:/home/guanfuchang# docker create -p 16379:6379 --name redis redis:5.0
07bf73ec9f73d6d7a2c4bad79813bf1ae63b853e2b3a22a92d58d7ecfe24ca2e

创建成功后,会返回容器ID

[info]Tips:命令中的端口 16379:6379,冒号前面的端口16379是虚拟机的端口,6379是容器内的端口,通过该设置,是将操作系统的16379映射到容器内的6379,我们后面使用redis客户端连接redis时,连接的是16379哦。

查看容器列表

查看正在运行的容器列表,命令 docker ps

查看所有的本地容器,命令 docker ps -a

root@ubuntu:/home/guanfuchang# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root@ubuntu:/home/guanfuchang# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07bf73ec9f73 redis:5.0 "docker-entrypoint.s…" 4 minutes ago Created redis

启动容器

启动容器,命令格式:docker start 容器名或容器ID,其中容器的id,只需要输入前几位即可。

案例:启动redis容器

root@ubuntu:/home/guanfuchang# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07bf73ec9f73 redis:5.0 "docker-entrypoint.s…" 4 minutes ago Created redis
root@ubuntu:/home/guanfuchang#
root@ubuntu:/home/guanfuchang# docker start 07bf73ec9f73
07bf73ec9f73
root@ubuntu:/home/guanfuchang#
root@ubuntu:/home/guanfuchang# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07bf73ec9f73 redis:5.0 "docker-entrypoint.s…" 8 minutes ago Up 42 seconds 0.0.0.0:16379->6379/tcp redis
root@ubuntu:/home/guanfuchang#

通过上面创建后,主机的16379端口上已经运行了一个redis服务。

案例:通过redis客户端进行测试

Redis可视化管理工具(Redis Desktop Manager])

链接:https://pan.baidu.com/s/1sOiOm7bEALZKA0-GpkZ3_Q 密码:ruxk

下载安装后,连接redis服务器,配置如下:

连接成功如下:

创建并运行容器

上面通过docker create 创建了容器,然后通过docker start 来启动容器。

由于创建容器并且启动容器的操作非常频繁,docker client 提供了更加便捷的命令 docker run 一步创建并且启动容器。

命令格式:docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

案例:创建并运行一个redis容器

root@ubuntu:/home/guanfuchang# docker run -p 16380:6379 --name redis2 redis:5.0
1:C 15 Nov 2018 07:01:49.153 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 15 Nov 2018 07:01:49.154 # Redis version=5.0.1, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 15 Nov 2018 07:01:49.154 # 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 15 Nov 2018 07:01:49.155 * Running mode=standalone, port=6379.
1:M 15 Nov 2018 07:01:49.155 # 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 15 Nov 2018 07:01:49.156 # Server initialized
1:M 15 Nov 2018 07:01:49.156 # 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 15 Nov 2018 07:01:49.156 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 15 Nov 2018 07:01:49.156 * Ready to accept connections

上面例子,启动redis容器后,一直在前台运行,如果想要让容器后台运行,加入参数-d,如:

root@ubuntu:/home/guanfuchang# docker run -p 16380:6379 -d --name redis3 redis:5.0
e4a4aae7860f9f5a2bca62cb806e33ef356939708f9655c95af122565d0db3c2

停止容器

停止容器有2种方式

  • docker stop 容器名或容器id
  • docker kill 容器名或容器id

案例:停止容器redis3

root@ubuntu:/home/guanfuchang# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4a4aae7860f redis:5.0 "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 0.0.0.0:16380->6379/tcp redis3
07bf73ec9f73 redis:5.0 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:16379->6379/tcp redis
root@ubuntu:/home/guanfuchang# docker stop redis3
redis3
root@ubuntu:/home/guanfuchang# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07bf73ec9f73 redis:5.0 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:16379->6379/tcp redis

删除容器

删除容器,命令格式:docker rm [OPTIONS] CONTAINER [CONTAINER...]

删除正在运行的容器,添加-f参数

案例:删除容器redis3

root@ubuntu:/home/guanfuchang# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4a4aae7860f redis:5.0 "docker-entrypoint.s…" 7 minutes ago Exited (0) 3 minutes ago redis3
4c371862865e redis:5.0 "docker-entrypoint.s…" 12 minutes ago Exited (0) 10 minutes ago redis2
07bf73ec9f73 redis:5.0 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:16379->6379/tcp redis
root@ubuntu:/home/guanfuchang#
root@ubuntu:/home/guanfuchang# docker rm redis3
redis3
root@ubuntu:/home/guanfuchang# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c371862865e redis:5.0 "docker-entrypoint.s…" 12 minutes ago Exited (0) 10 minutes ago redis2
07bf73ec9f73 redis:5.0 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:16379->6379/tcp redis

进入容器

有些时候,我们需要进入容器内,做一些操作,比如修改配置文件等

不推荐修改容器,后面会介绍如何挂载外部文件

进入容器,命令格式:docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

案例:进入容器redis

root@ubuntu:/home/guanfuchang# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07bf73ec9f73 redis:5.0 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:16379->6379/tcp redis
root@ubuntu:/home/guanfuchang#
root@ubuntu:/home/guanfuchang# docker exec -it redis /bin/bash
root@07bf73ec9f73:/data#
root@07bf73ec9f73:/data# cd /
root@07bf73ec9f73:/# ls
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@07bf73ec9f73:/# ls /usr/local/bin/
docker-entrypoint.sh gosu redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server

通过上面例子,我们可以得知,容器里面,包含了一个小型的linux系统,在linux系统上安装了redis服务。

[info] control+d 退出容器

查看日志

命令格式: docker logs [OPTIONS] CONTAINER

案例:查看redis容器日志

root@ubuntu:/home/guanfuchang# docker logs -f redis
1:C 15 Nov 2018 06:21:27.687 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 15 Nov 2018 06:21:27.689 # Redis version=5.0.1, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 15 Nov 2018 06:21:27.689 # 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 15 Nov 2018 06:21:27.690 * Running mode=standalone, port=6379.
1:M 15 Nov 2018 06:21:27.690 # 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 15 Nov 2018 06:21:27.690 # Server initialized
1:M 15 Nov 2018 06:21:27.690 # 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 15 Nov 2018 06:21:27.690 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 15 Nov 2018 06:21:27.690 * Ready to accept connections

:-:

微信扫一扫,关注“python测试开发圈”,了解更多测试教程!

Docker05-容器的更多相关文章

  1. Docker 容器数据 持久化(系统学习Docker05)

    写在前面 本来是可以将数据存储在 容器内部 的.但是存在容器内部,一旦容器被删除掉或者容器毁坏(我亲身经历的痛,当时我们的大数据平台就是运行在docker容器内,有次停电后,不管怎样容器都起不来.以前 ...

  2. docker——容器安装tomcat

    写在前面: 继续docker的学习,学习了docker的基本常用命令之后,我在docker上安装jdk,tomcat两个基本的java web工具,这里对操作流程记录一下. 软件准备: 1.jdk-7 ...

  3. 网页提交中文到WEB容器的经历了些什么过程....

    先准备一个网页 <html><meta http-equiv="Content-Type" content="text/html; charset=gb ...

  4. [Spring]IoC容器之进击的注解

    先啰嗦两句: 第一次在博客园使用markdown编辑,感觉渲染样式差强人意,还是github的样式比较顺眼. 概述 Spring2.5 引入了注解. 于是,一个问题产生了:使用注解方式注入 JavaB ...

  5. 深入理解DIP、IoC、DI以及IoC容器

    摘要 面向对象设计(OOD)有助于我们开发出高性能.易扩展以及易复用的程序.其中,OOD有一个重要的思想那就是依赖倒置原则(DIP),并由此引申出IoC.DI以及Ioc容器等概念.通过本文我们将一起学 ...

  6. Docker笔记一:基于Docker容器构建并运行 nginx + php + mysql ( mariadb ) 服务环境

    首先为什么要自己编写Dockerfile来构建 nginx.php.mariadb这三个镜像呢?一是希望更深入了解Dockerfile的使用,也就能初步了解docker镜像是如何被构建的:二是希望将来 ...

  7. JS判断鼠标进入容器方向的方法和分析window.open新窗口被拦截的问题

    1.鼠标进入容器方向的判定 判断鼠标从哪个方向进入元素容器是一个经常碰到的问题,如何来判断呢?首先想到的是:获取鼠标的位置,然后经过一大堆的if..else逻辑来确定.这样的做法比较繁琐,下面介绍两种 ...

  8. docker4dotnet #2 容器化主机

    .NET 猿自从认识了小鲸鱼,感觉功力大增.上篇<docker4dotnet #1 前世今生&世界你好>中给大家介绍了如何在Windows上面配置Docker for Window ...

  9. 深入分析Spring 与 Spring MVC容器

    1 Spring MVC WEB配置 Spring Framework本身没有Web功能,Spring MVC使用WebApplicationContext类扩展ApplicationContext, ...

  10. Set容器--HashSet集合

    Set容器特点: ①   Set容器是一个不包含重复元素的Collection,并且最多包含一个null元素,它和List容器相反,Set容器不能保证其元素的顺序; ②   最常用的两个Set接口的实 ...

随机推荐

  1. .netcore里使用StackExchange.Redis TimeOut 情况解决方法

    在用StackExchange.Redis这个组件时候,时不时会出现异常TimeOut解决方法如下, 解决方法: 在Program的Main入口方法里添加一句话: System.Threading.T ...

  2. 【专】linux nameserver导致的故障

    前言: 大家都知道linux下添加dns服务器,修改/etc/resolv.conf,添加nameserver 119.29.29.29这样一行即可.但是胡乱添加nameserver也会导致故障 ,此 ...

  3. (转)How To Create a Sudo User on Ubuntu

    转自:https://linuxize.com/post/how-to-create-a-sudo-user-on-ubuntu/ The sudo command is designed to al ...

  4. k8s记录-k8s基本概念和术语

    每次个节点上当然都要运行Docker.Docker来负责所有具体的映像下载和容器运行. Kubernetes主要由以下几个核心组件组成: etcd保存了整个集群的状态: apiserver提供了资源操 ...

  5. 配置Pods和containers--为Containers和Pods分配内存资源

    指定内存请求和内存限制 要为容器指定内存请求,在容器的资源清单中使用resources:requests字段.要指定内存限制,使用resources:limits. memory-request-li ...

  6. bash命令检测Shell脚本中的语法错误和查看详细执行过程

    (1).bash命令检测Shell脚本中的语法错误 bash -v [脚本] [root@youxi1 ~]# vim a.sh #/bin/bash sum=$[$1+$2] echoo $sum ...

  7. 【tensorflow-v2.0】如何将模型转换成tflite模型

    前言 TensorFlow Lite 提供了转换 TensorFlow 模型,并在移动端(mobile).嵌入式(embeded)和物联网(IoT)设备上运行 TensorFlow 模型所需的所有工具 ...

  8. 查看表空间使用情况(SQL)

    1: --查询表空间使用情况 2: SELECT Upper(F.TABLESPACE_NAME)         "表空间名", 3:        D.TOT_GROOTTE_ ...

  9. BatchConfigTool批量配置工具

    海康批量配置工具BatchConfigTool是一款支持设备在线搜索.批量配置参数.批量升级等功能的软件,支持对大批量设备同时进行各参数的配置,极大的简化了操作过程! 软件功能 1.对在线设备进行搜索 ...

  10. mysql 启动 && 停止

    启动:service mysql start关闭:service mysql stop查进程:ps aux | grep mysql杀进程:kill -9 mysqlID Good Good Stud ...