Date Modified Tags Docker

Introduction

更多关于Docker内容请查看Docker学习笔记目录

简单记录下自己用docker的一些经历和经验。并不对docker进行介绍,可自行查阅下面的链接。 先发一些阅读的的东西。 推荐这个简短的小教程: Docker 从入门到实践 这是官方的文档: Docker Documents 后面的一些安装也是按照官方文档里说的进行。

安装

我的运行环境: Debian 8 Jessie x86_64 参考官方安装文档

卸载旧的docker

Older versions of Docker were called docker or docker-engine. If these are installed, uninstall them:

$ sudo apt-get remove docker docker-engine docker.io

Add Docker’s official GPG key:

$ curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg | sudo apt-key add -

安装

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
   $(lsb_release -cs) \
   stable"

# 从源里安装
sudo apt-get update
sudo apt-get install docker-ce

如果apt安装特别慢,可以考虑使用国内的镜像源,比如中科大的。

修改/etc/apt/sources.list里关于docker的部分,比如debian 9可以改成:

deb [arch=amd64] http://ipv6.mirrors.ustc.edu.cn/docker-ce/linux/debian/ stretch stable

最后把你的用户添加到docker group里,注销以后重新登录,就不需要加sudo再运行docker了。

sudo usermod -aG docker username

获取镜像

先从官网的hub查看都有哪些镜像可以用 比如点到debian的详细信息,可以看到支持很多版本:8.3,8,7,等等。右边有提示命令:

docker pull debian

默认都是latest版本,想要下载指定版本可以通过在后面添加:version

docker pull debian:stretch

等待下载完成即可

管理镜像

docker images

在列出信息中,可以看到几个字段信息

  • 来自于哪个仓库,比如 ubuntu
  • 镜像的标记,比如 14.04
  • 它的 ID 号(唯一)
  • 创建时间
  • 镜像大小

其中镜像的 ID 唯一标识了镜像,注意到 ubuntu:14.04 和 ubuntu:trusty 具有相同的镜像 ID,说明它们实际上是同一镜像。

TAG 信息用来标记来自同一个仓库的不同镜像。例如 ubuntu 仓库中有多个镜像,通过 TAG 信息来区分发行版本,例如 10.04、12.04、12.10、13.04、14.04 等。

进入镜像

下面的命令指定使用镜像debian:stretch来启动一个容器。

docker run -t -i debian:stretch /bin/bash

如果不指定具体的标记,则默认使用 latest 标记信息。-t是指明tag,-i是绑定tty到当前的命令行终端。不然的话,就直接运行一下就结束了,不能产生交互效果。 默认里面啥也没有,所以先apt update,然后装自己需要的就行。因为默认的镜像源很慢,所以跟修改普通的一个系统一样,修改sourcelist,再重新update。

进入容器

可以通过docker ps -a获取当前所有的容器,包括已经退出的。获得容器的ID之后,可以通过

docker start -i contain_id

后台运行

# docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 2; done"
ae60c4b642058fefcc61ada85a610914bed9f5df0e2aa147100eab85cea785dc

它将直接把启动的container挂起放在后台运行,并且会输出一个CONTAINER ID,通过docker ps可以看到这个容器的信息,可在container外面查看它的输出docker logs ae60c4b64205,也可以通过docker attach ae60c4b64205连接到这个正在运行的终端,此时在Ctrl+C退出container就消失了,按Ctrl+P 然后Ctrl+Q可以退出到宿主机,而保持container仍然在运行 另外,如果-d启动但后面的命令执行完就结束了,如/bin/bashecho test,则container做完该做的时候依然会终止。而且-d不能与--rm同时使用 可以通过这种方式来运行memcached、apache等。

卸载

官网文档给的很详细, To uninstall the Docker package:

sudo apt-get purge docker-engine

To uninstall the Docker package and dependencies that are no longer needed:

sudo apt-get autoremove --purge docker-engine

The above commands will not remove images, containers, volumes, or user created configuration files on your host. If you wish to delete all images, containers, and volumes run the following command:

rm -rf /var/lib/docker

You must delete the user created configuration files manually.

与宿主机共享数据

官网挂载宿主机文件夹到docker的说明 简单解释:

docker run -v /Users/<path>:/<container path>

符合from to的参数规则,先写from路径,冒号,to路径,注意每个路径后面没有/,如果路径不存在,docker会自动创建。注意,这个参数最好写在前面,起码是在-ti参数前面。 默认挂载是rw模式,可读可写。

端口绑定

我们使用-P标记创建一个容器,将容器的内部端口随机映射到主机的高端口49000到49900。这时我们可以使用docker ps来看到端口5000绑定主机端口49155。

$ sudo docker ps nostalgic_morse
CONTAINER ID  IMAGE                   COMMAND       CREATED        STATUS        PORTS                    NAMES
bc533791f3f5  training/webapp:latest  python app.py 5 seconds ago  Up 2 seconds  0.0.0.0:49155->5000/tcp  nostalgic_morse

我们也可以使用-p标识来指定容器端口绑定到主机端口,:前是宿主机端口,后是容器端口

docker run -it -p 5000:5000 ubuntu /bin/bash

删除容器或者镜像

删除任何容器之前,要保证它已经停止了,通过docker stop contain_id,否则会提示你未停止,不能删除。 删除容器:

docker rm container_id

删除任何镜像之前,要保证没有任何容器是基于它构建的。 删除镜像:

docker rmi image_id_or_name

删除停止的容器

Since Docker 1.13.x you can use Docker container prune:

docker container prune

This will remove all stopped containers and should work on all platforms the same way.

There is also a Docker system prune:

docker system prune

which will clean up all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes, in one command.

Reference

docker专题(2):docker常用管理命令(上) 连接容器

Docker —— 从入门到实践

Docker 和一个正常的虚拟机有何区别? 初探Docker - Docker 跟 LXC 以及一般Hypervisor有何差別? 全面了解Docker https://stackoverflow.com/questions/17236796/how-to-remove-old-docker-containers


文章版权归 FindHao 所有丨本站默认采用CC-BY-NC-SA 4.0协议进行授权|
转载必须包含本声明,并以超链接形式注明作者 FindHao 和本文原始地址:
https://findhao.net/easycoding/1700.html

Comments