Docker05-容器
容器介绍
容器是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测试开发圈”,了解更多测试教程!