Docker基本操作

docker安装

Ubuntu14.04安装docker 摘自aliyun官网
https://yq.aliyun.com/articles/110806?spm=5176.8351553.0.0.691e1991DpXzU5

# step 1: 安装必要的一些系统工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: 安装GPG证书
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add –
# Step 3: 写入软件源信息
sudo add-apt-repository “deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable”
# Step 4: 更新并安装 Docker-CE
sudo apt-get -y update
sudo apt-get -y install docker-ce

# 安装指定版本的Docker-CE:
# Step 1: 查找Docker-CE的版本:
# apt-cache madison docker-ce
#   docker-ce | 17.03.1~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
#   docker-ce | 17.03.0~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
# Step 2: 安装指定版本的Docker-CE: (VERSION 例如上面的 17.03.1~ce-0~ubuntu-xenial)
# sudo apt-get -y install docker-ce=[VERSION]

查看版本
root@aaa:~# docker version
Client:
Version:  17.12.1-ce
API version: 1.35
Go version:  go1.9.4
Git commit:  7390fc6
Built: Tue Feb 27 22:17:56 2018
OS/Arch:  linux/amd64

Server:
Engine:
Version: 17.12.1-ce
API version: 1.35 (minimum version 1.12)
Go version: go1.9.4
Git commit: 7390fc6
Built:   Tue Feb 27 22:16:28 2018
OS/Arch: linux/amd64
Experimental:   false

docker基本操作

登录阿里云容器镜像服务,创建自己仓库(配置加速器等参考https://cr.console.aliyun.com/cn-hangzhou/mirrors)

登入仓库

docker login 默认登入到hub.docker.com  docker login –username=xxxxxxxx@qq.com registry.cn-hangzhou.aliyuncs.com #指定登入到阿里云容器

以上均输入密码即可。

root@aaa:~/.docker# cat config.json  { “auths”: { “registry.cn-hangzhou.aliyuncs.com”: { “auth”: “MzkwOTqwdqdfmNvbTp6aGFuZ3RhbzE5ODg=” } }, “HttpHeaders”: { “User-Agent”: “Docker-Client/17.12.1-ce (linux)” } }

拉取镜像

首先登入hub.docker找到合适镜像并pull

root@aaa:~# docker pull centos:7.2.1511

7.2.1511: Pulling from library/centos f2d1d709a1da:

Already exists  Digest: sha256:29083aecbc86ed398ee3464f69433e529039d6f640d50171b6b385bb0d28230d Status: Downloaded newer image for centos:7.2.1511

root@aaa:~# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

centos 7.2.1511 4cbf48630b46 7 weeks ago 195MB

上传镜像

root@aaa:~# docker tag centos:7.2.1511 registry.cn-hangzhou.aliyuncs.com/momo/centos:7.2.1511

root@aaa:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos 7.2.1511 4cbf48630b46 7 weeks ago 195MB registry.cn-hangzhou.aliyuncs.com/momo/centos 7.2.1511 4cbf48630b46 7 weeks ago 195MB

root@aaa:~# docker push centos:7.2.1511 registry.cn-hangzhou.aliyuncs.com/momo/centos:7.2.1511

创建容器

root@aaa:~# docker run -it centos:7.2.1511 /bin/bash [root@fc7b209f4881 /]#

退出容器

exit 退出容器,并停止docker ps -a

ctrl+p+q 退出容器,但容器后台运行

进入容器

root@aaa:~# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b2699dcd1471 centos7.2:1511 “/bin/bash” About an hour ago Up About an hour volume

root@aaa:~# docker attach b2699dcd1471

[root@b2699dcd1471 ~]# ls anaconda-ks.cfg

root@aaa:~# docker exec -it b2699dcd1471 /bin/bash

[root@b2699dcd1471 /]# read escape sequence (按ctrl+p+q后显示)

容器启停

docker start/stop/restart docker-id

导入和导出容器

root@aaa:~# docker run -it centos7.2:1511 /bin/bash

[root@281429989f81 /]#

root@aaa:~#

root@aaa:~# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 281429989f81 centos7.2:1511 “/bin/bash” 9 seconds ago Up 8 seconds brave

root@aaa:~# docker export 281429989f81 >centos7_run.tar

root@aaa:~#

root@aaa:~# cat centos7_run.tar | docker import – centos7:7.2.1511 sha256:a61afb9845ebf64ec9b2db88eaecd7eb916323d9f27535bdb93a3bfb6abf9423

root@aaa:~# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

centos7 7.2.1511 a61afb9845eb 5 seconds ago 195MB centos7.2 1511 0a2bad7da9b5 12 months ago 195MB

删除容器

root@aaa:~# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

fc7b209f4881 centos:7.2.1511 “/bin/bash” About a minute ago Exited (0) 5 seconds ago heuristic_goldberg

b2699dcd1471 centos7.2:1511 “/bin/bash” About an hour ago Up About an hour volume

root@aaa:~# docker rm fc7b fc7b

root@aaa:~# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b2699dcd1471 centos7.2:1511 “/bin/bash” About an hour ago Up About an hour volume

删除镜像

root@aaa:~# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

registry.cn-hangzhou.aliyuncs.com/momo/centos 7.2.1511 4cbf48630b46 7 weeks ago 195MB

centos 7.2.1511 4cbf48630b46 7 weeks ago 195MB

root@aaa:~# docker rmi registry.cn-hangzhou.aliyuncs.com/momo/centos:7.2.1511 Untagged: registry.cn-hangzhou.aliyuncs.com/momo/centos:7.2.1511

保存本地和载入镜像

root@aaa:~# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

centos7.2 1511 0a2bad7da9b5 12 months ago 195MB

root@aaa:~# docker save -o centos7.2.1511.tar centos7.2:1511

root@aaa:~# ls  centos7.2.1511.tar

root@aaa:~#

测试载入

root@aaa:~# docker rmi centos7.2:1511 Untagged: centos7.2:1511

root@aaa:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE

root@aaa:~# docker load -i centos7.2.1511.tar  Loaded image: centos7.2:1511 root@aaa:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7.2 1511 0a2bad7da9b5 12 months ago 195MB

数据卷

挂载主机目录作为数据卷

root@aaa:~# mkdir /mnt/ceshi

root@aaa:~# docker run -it –name ceshi -v /mnt/ceshi:/opt/ceshi centos7.2:1511

–name 指定容器名字 /mnt/ceshi:/opt/ceshi 前面为宿主机目录,后面为容器内挂载点

[root@2d018b52fd75 /]# df -Th

Filesystem Type Size Used Avail Use% Mounted on

/dev/mapper/aaa–vg-root ext4 886G 3.6G 837G 1% /opt/ceshi [root@2d018b52fd75 /]# echo 123 > /opt/ceshi/aaa

[root@2d018b52fd75 /]# exit

root@aaa:~# cat /mnt/ceshi/aaa  123

数据卷容器

创建数据卷容器

root@aaa:~# docker run -it -v /dbdata –name dbdata centos7.2:1511 [root@feabca5f7c07 /]# exit

创建新的容器挂载容器卷(这里的容器卷不一定非要run运行状态,这里为exit状态)

root@aaa:~# docker run -it –volumes-from dbdata –name db1 centos7.2:1511 [root@505e202a7617 /]# df -Th

Filesystem Type Size Used Avail Use% Mounted on

/dev/mapper/aaa–vg-root ext4 886G 3.6G 837G 1% /dbdata

[root@505e202a7617 /]# echo db1 > /dbdata/aaa

[root@505e202a7617 /]# exit

root@aaa:~# docker start feabca5f7c07 feabca5f7c07

root@aaa:~# docker attach feabca5f7c07

[root@feabca5f7c07 /]# cat /dbdata/aaa  db1

[root@feabca5f7c07 /]# exit

 

link使用

docker的link机制可以通过一个name来和另一个容器通信,link机制方便了容器去发现其它的容器并且可以安全的传递一些连接信息给其它的容器 .正常情况下,容器之间是不会互通的。加上–link之后,容器之间便能互相通信。–link机制即将被淘汰,有新的网络处理机制替换。

docker run -it –name haproxy –link app1:app1 –link app2:app2 -p 6301:6301 -v ~/project/haproxy:/tmp haproxy /bin/bash

如上命令创建容器后,通过–link指定容器 然后进入到容器中查看hosts文件就可以看到ip 主机名 容器名之间的映射记录。

root@b5ce94fc1c1f:/usr/local/sbin

# cat /etc/hosts

127.0.0.1localhost ::1localhost

172.17.0.5  app1 44f4eb740fe6

172.17.0.6  app2 3eb6cd729eff

172.17.0.7  b5ce94fc1c1f

 

端口指定

通过主机某个端口与容器内指定端口互通后,外界可以通过主机端口访问容器内应用。

-P 主机端口随机指定

pull测试镜像

root@aaa:~# docker pull training/webapp

root@aaa:~# docker run -d -P training/webapp python app.py 8ac3787f2ea9bb42b9561d71124deb34de2f727058d718ba4851b9dd555f7c65 root@aaa:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8ac3787f2ea9 training/webapp “python app.py” 5 seconds ago Up 4 seconds 0.0.0.0:32770->5000/tcp confident_ptolemy

可以看到主机端口为32770 对应容器端口5000

通过主机访问

root@aaa:~# curl 127.0.0.1:32770

Hello world!

-p 指定主机端口

root@aaa:~# docker run -d -p 5555:5000 training/webapp python app.py c8c795a5c7137d64652ba4c7765c28c89b8c3f18ca333eac32b60e5fd0d23547 root@aaa:~# curl 127.0.0.1:5555

Hello world!

 

 

 

发布者

deelaaay

己所不欲,勿施于人。