文档章节

快速启动 containers

Ryan-瑞恩
 Ryan-瑞恩
发布于 2015/12/21 11:57
字数 1036
阅读 25
收藏 1

快速启动containers

This quickstart assumes you have a working installation of Docker. To verify Docker is installed, use the following command:

# 检查Docker 是否安装
$ docker info 

If you get docker: command not found or something like/var/lib/docker/repositories: permission denied you may have an incomplete Docker installation or insufficient privileges to access Docker on your machine. With the default installation of Docker docker commands need to be run by a user that is in the docker group or by the root user.

Depending on your Docker system configuration, you may be required to preface each docker command with sudo. One way to avoid having to use sudo with thedocker commands is to create a Unix group called docker and add users that will be entering docker commands to the ‘docker’ group.

For more information about installing Docker or sudo configuration, refer to theinstallation instructions for your operating system.

下载已建立 image

# 下载 ubuntu 镜像
$ docker pull ubuntu

This will find the ubuntu image by name on Docker Hub and download it fromDocker Hub to a local image cache.

Note: When the image is successfully downloaded, you see a 12 character hash539c0211cd76: Download complete which is the short form of the image ID. These short image IDs are the first 12 characters of the full image ID - which can be found using docker inspect or docker images --no-trunc=true.

运行交互式 shell

使用ubuntu镜像运行交互式Shell:

$ docker run -i -t ubuntu /bin/bash

The -i flag starts an interactive container. The -t flag creates a pseudo-TTY that attaches stdin and stdout.

To detach the tty without exiting the shell, use the escape sequence Ctrl-p +Ctrl-q. The container will continue to exist in a stopped state once exited. To list all containers, stopped and running, use the docker ps -a command.

Bind Docker to another host/port or a Unix socket

Warning: Changing the default docker daemon binding to a TCP port or Unixdocker user group will increase your security risks by allowing non-root users to gain root access on the host. Make sure you control access to docker. If you are binding to a TCP port, anyone with access to that port has full Docker access; so it is not advisable on an open network.

With -H it is possible to make the Docker daemon to listen on a specific IP and port. By default, it will listen on unix:///var/run/docker.sock to allow only local connections by the root user. You could set it to 0.0.0.0:2375 or a specific host IP to give access to everybody, but that is not recommended because then it is trivial for someone to gain root access to the host where the daemon is running.

Similarly, the Docker client can use -H to connect to a custom port. The Docker client will default to connecting to unix:///var/run/docker.sock on Linux, andtcp://127.0.0.1:2376 on Windows.

-H accepts host and port assignment in the following format:

tcp://[host]:[port][path] or unix://path

For example:

  • tcp:// -> TCP connection to 127.0.0.1 on either port 2376 when TLS encryption is on, or port 2375 when communication is in plain text.
  • tcp://host:2375 -> TCP connection on host:2375
  • tcp://host:2375/path -> TCP connection on host:2375 and prepend path to all requests
  • unix://path/to/socket -> Unix socket located at path/to/socket

-H, when empty, will default to the same value as when no -H was passed in.

-H also accepts short form for TCP bindings:

`host:` or `host:port` or `:port` 

Run Docker in daemon mode:

$ sudo <path to>/docker daemon -H 0.0.0.0:5555 &

Download an ubuntu image:

$ docker -H :5555 pull ubuntu

You can use multiple -H, for example, if you want to listen on both TCP and a Unix socket

# Run docker in daemon mode $ sudo <path to>/docker daemon -H tcp://127.0.0.1:2375 -H unix:///var/run/docker.sock & # Download an ubuntu image, use default Unix socket $ docker pull ubuntu # OR use the TCP port $ docker -H tcp://127.0.0.1:2375 pull ubuntu

Starting a long-running worker process

# Start a very useful long-running process $ JOB=$(docker run -d ubuntu /bin/sh -c "while true; do echo Hello world; sleep 1; done") # Collect the output of the job so far $ docker logs $JOB # Kill the job $ docker kill $JOB

列表 containers查看

$ docker ps # 列出所有运行中镜像
$ docker ps -a # 列表所有镜像

Controlling containers

# Start a new container $ JOB=$(docker run -d ubuntu /bin/sh -c "while true; do echo Hello world; sleep 1; done") # Stop the container $ docker stop $JOB # Start the container $ docker start $JOB # Restart the container $ docker restart $JOB # SIGKILL a container $ docker kill $JOB # Remove a container $ docker stop $JOB # Container must be stopped to remove it $ docker rm $JOB

Bind a service on a TCP port

# Bind port 4444 of this container, and tell netcat to listen on it $ JOB=$(docker run -d -p 4444 ubuntu:12.10 /bin/nc -l 4444) # Which public port is NATed to my container? $ PORT=$(docker port $JOB 4444 | awk -F: '{ print $2 }') # Connect to the public port $ echo hello world | nc 127.0.0.1 $PORT # Verify that the network connection worked $ echo "Daemon received: $(docker logs $JOB)"

Committing (saving) a container state

Save your containers state to an image, so the state can be re-used.

When you commit your container, Docker only stores the diff (difference) between the source image and the current state of the container’s image. To list images you already have, use the docker images command.

# Commit your container to a new named image
$ docker commit <container> <some_name>

# List your images
$ docker images 

You now have an image state from which you can create new instances.

下一步去这里

© 著作权归作者所有

共有 人打赏支持
Ryan-瑞恩

Ryan-瑞恩

粉丝 145
博文 226
码字总数 176186
作品 0
西安
后端工程师
【卡片记忆】快速启动iOS模拟器

假定场景 查看模拟器的日志目录 模拟器目录 解决方案 引用链接:点击 获取帮助 创新源于一种感觉! http://go-swift.net

创意总监
2016/03/09
89
0
kubernetes 1.6.2部署

部署环境服务器:腾讯云 操作系统版本:centos 7.2 1、修改系统配置: [root@node1~]# cat /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables= 1net.bridge.bridge-nf-call-ipta......

小罗ge11
2017/06/19
0
0
Docer 和虚拟机的一些对比 [翻译]

本文翻译自:《Docker: Git for deployment -- Scout》,个人感觉它的内容和标题不对,所以就没有直译过来,要是我理解错了请帮忙纠正。 我听说了 多么令人惊叹,但是它并没有征服我的心,直...

BearCatYN
2015/03/30
0
0
一步步打造专属于自己的Kubernetes-1.8.4集群环境

一 kubernetes kubernetes 以下介绍摘自Wiki Kubernetes (通常称为K8s) 是用于自动部署、扩展和管理容器化(containerized)应用程序的开源系统。Google设计并捐赠给Cloud Native Computing ...

胖宝宝王
2017/12/08
0
0
微软 SQL Server 2016 SP1 开发者版入驻 Windows 容器

2月22日有消息称微软今天宣布在Windows Containers容器中上线SQL Server 2016 SP1开发者版。用户可以在Docker Hub中找到镜像,可用于Windows Server容器和Hyper-V容器中。 在Windows容器中,...

达尔文
2017/02/22
1K
9

没有更多内容

加载失败,请刷新页面

加载更多

腾讯投资最高1.75亿美元正式进军菲律宾移动支付市场

菲律宾长途电话公司(PLDT)公司今日宣布,中国互联网巨头腾讯和私募股权公司KKR将获得该公司旗下金融科技公司Voyager Innovations的少数股权。 PLDT在一份声明中称:“腾讯和KKR最多将分别收...

linuxCool
15分钟前
1
0
正则介绍及grep/egrep用法

10月16日任务 9.1 正则介绍_grep上 9.2 grep中 9.3 grep下 扩展 把一个目录下,过滤所有*.php文档中含有eval的行 grep -r --include="*.php" 'eval' /data 正则介绍 正则就是一串有规律的字符...

hhpuppy
26分钟前
0
0
J2Cache 中使用 Lettuce 替代 Jedis 管理 Redis 连接

一直以来 J2Cache 都是使用 Jedis 连接 Redis 服务的。Jedis 是一个很老牌的 Redis 的 Java 开发包,使用很稳定,作者维护很勤勉,社区上能搜到的文章也非常非常多。算是使用范围最广的 Redi...

红薯
今天
11
0
一个可能的NEO链上安全随机数解决方案

0x00 困境 链上安全随机数生成应该算是一个比较蛋疼的问题,哪怕你的系统再牛逼,合约程序困在小小的虚拟机里,哪怕天大的本事也施展不开。 更悲催的是,交易执行的时候,是在每一个节点都执...

暖冰
今天
1
0
【大福利】极客时间专栏返现二维码大汇总

我已经购买了如下专栏,大家通过我的二维码你可以获得一定额度的返现! 然后,再给大家来个福利,只要你通过我的二维码购买,并且关注了【飞鱼说编程】公众号,可以加我微信或者私聊我,我再...

飞鱼说编程
今天
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部