Docker快速入门以及安装使用

1、Docker简介

docker通过内核虚拟化技术(namespace及cgroups等)来提供容器的资源隔离与安全保障等,由于docker通过操作系统层的虚拟化实现隔离,所以docker容器在运行时,不需要类似虚拟机额外的操作系统开销,提供资源利用率。

2、Docker vs kvm

3、Docker组件

镜像、容器、仓库

4、Docker安装

[root@tiejiang ~]#tee /etc/yum.repos.d/docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF
[root@tiejiang ~]# yum install docker-engine -y

5、Docker基础操作

[root@tiejiang ~]# systemctl enable docker.service
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@tiejiang ~]# systemctl start docker.service
[root@tiejiang ~]# 
[root@tiejiang ~]# docker pull centos  #拉取镜像
[root@tiejiang ~]# docker pull daocloud.io/library/nginx

有时候拉取速度很慢,采用国内源加速

[root@tiejiang ~]# vim /usr/lib/systemd/system/docker.service

增加下面这行

EnvironmentFile=/etc/sysconfig/docker

新建配置文件

[root@tiejiang ~]# vim /etc/sysconfig/docker

https://dashboard.daocloud.io/ 注册,然后点击加速器生成加速链接

OPTIONS=--registry-mirror=curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s

[root@tiejiang ~]# docker search nginx  #搜索镜像

[root@tiejiang ~]# docker images 

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

centos              latest              0584b3d2cf6d        2 weeks ago         196.5 MB

导出镜像

[root@tiejiang ~]# docker save -o nginx.tar daocloud.io/library/nginx

[root@tiejiang ~]# docker save -o cnetos.tar centos

导入镜像

[root@tiejiang ~]# docker load --input cnetos.tar 或者 [root@docker ~]# docker load < cnetos.tar

删除镜像

[root@tiejiang ~]# docker rmi 0584b3d2cf6d (镜像ID)

[root@tiejiang ~]# docker run centos /bin/echo "Hello world"
Hello world
[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
5e381e68a385        centos              "/bin/echo 'Hello wor"   6 seconds ago       Exited (0) 5 seconds ago                       clever_lamarr

[root@docker ~]# docker run --name mydocker -t -i centos /bin/bash
[root@1a67f4c92b6e /]# 

[root@1a67f4c92b6e /]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 18:56 ?        00:00:00 /bin/bash
root         14      1  0 18:56 ?        00:00:00 ps -ef

[root@1a67f4c92b6e /]# exit
exit
[root@tiejiang ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
1a67f4c92b6e        centos              "/bin/bash"              12 minutes ago      Exited (0) 6 seconds ago                        mydocker
5e381e68a385        centos              "/bin/echo 'Hello wor"   16 minutes ago      Exited (0) 16 minutes ago                       clever_lamarr

[root@tiejiang ~]# docker run --name docker-demo -d centos /bin/bash    -d代表放入后台执行
6c5a777467b9552714f9cd3322e677750e2b8b5b0bd2d81e79094ad560828a5e
[root@tiejiang ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
6c5a777467b9        centos              "/bin/bash"              11 seconds ago      Exited (0) 11 seconds ago                       docker-demo
1a67f4c92b6e        centos              "/bin/bash"              17 minutes ago      Exited (0) 4 minutes ago                        mydocker
5e381e68a385        centos              "/bin/echo 'Hello wor"   21 minutes ago      Exited (0) 21 minutes ago                       clever_lamarr

[root@tiejiang ~]# docker stop mydocker    停止容器
[root@tiejiang ~]# docker start 1a67f4c92b6e   启动容器

[root@tiejiang ~]# docker run -d --name mynginx daocloud.io/library/nginx
225a9b0459630c62dcf2199d6244b16a74ad9412471abf0be03755768df3ae63
[root@tiejiang ~]# 
[root@tiejiang ~]# docker ps
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS              PORTS               NAMES
225a9b045963        daocloud.io/library/nginx   "nginx -g 'daemon off"   6 seconds ago   
    Up 5 seconds        80/tcp, 443/tcp     mynginx


进入容器
[root@tiejiang ~]# cat docker_in.sh 
#!/bin/bash

docker_in(){

  NAME_ID=$1
  PID=$(docker inspect --format "{{ .State.Pid }}" $NAME_ID)
  nsenter -t $PID -m -u -i -n -p

}


docker_in $1

6、docker网络

[root@tiejiang ~]# docker run -d -P --name nginx-test1 daocloud.io/library/nginx
9b1d36d40127fe2c84bbe7750802e435a817a15b4159b24fc49bfb1107a2cb74

[root@tiejiang ~]# docker ps -l
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS              PORTS                                           NAMES
9b1d36d40127        daocloud.io/library/nginx   "nginx -g 'daemon off"   2 minutes ago       Up 2 minutes        0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp   nginx-test1

[root@tiejiang ~]# netstat -lntup|grep 32768
tcp6       0      0 :::32768                :::*                    LISTEN      11213/docker-proxy  

[root@tiejiang ~]# curl -I http://172.16.80.132:32769
HTTP/1.1 200 OK
Server: nginx/1.11.5
Date: Thu, 24 Nov 2016 05:58:47 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 11 Oct 2016 15:03:01 GMT
Connection: keep-alive
ETag: "57fcff25-264"
Accept-Ranges: bytes

转换前

[root@tiejiang ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
DOCKER-ISOLATION  all  --  0.0.0.0/0            0.0.0.0/0           
DOCKER     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain DOCKER (1 references)
target     prot opt source               destination         

Chain DOCKER-ISOLATION (1 references)
target     prot opt source               destination         
RETURN     all  --  0.0.0.0/0            0.0.0.0/0

转换后

[root@tiejiang ~]# docker run -d -P --name nginx-test1 daocloud.io/library/nginx
42783cf5053639383004f82b9e72fe0223c7c028d2754b2d0f74429824715f05
[root@tiejiang ~]# docker ps -l
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS              PORTS                                           NAMES
42783cf50536        daocloud.io/library/nginx   "nginx -g 'daemon off"   9 seconds ago       Up 7 seconds        0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp   nginx-test1
[root@tiejiang ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
DOCKER-ISOLATION  all  --  0.0.0.0/0            0.0.0.0/0           
DOCKER     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain DOCKER (1 references)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            172.17.0.2           tcp dpt:443
ACCEPT     tcp  --  0.0.0.0/0            172.17.0.2           tcp dpt:80

Chain DOCKER-ISOLATION (1 references)
target     prot opt source               destination         
RETURN     all  --  0.0.0.0/0            0.0.0.0/0           

[root@tiejiang ~]# sh docker_in.sh nginx-test1
root@42783cf50536:/# 
root@42783cf50536:/# 
root@42783cf50536:/# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
4: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:2/64 scope link 
       valid_lft forever preferred_lft forever

7、Docker数据存储

[root@tiejiang ~]# docker run -d --name nginx-volume-test1 -v /data daocloud.io/library/nginx
88b24d79a4f3b021325592ceac20e86291166d675b213d60db017548c4d9d960

[root@tiejiang ~]# sh docker_in.sh nginx-volume-test1
root@88b24d79a4f3:/# cd /data/
root@88b24d79a4f3:/data# ls
root@88b24d79a4f3:/data# touch hehe
root@88b24d79a4f3:/data# ls -l      
total 0
-rw-r--r-- 1 root root 0 Nov 24 06:30 hehe

[root@tiejiang ~]# cd /var/lib/docker/
[root@tiejiang docker]# ll
total 32
drwx------ 6 root root 4096 Nov 24 14:28 containers
drwx------ 5 root root 4096 Nov 24 02:05 devicemapper
drwx------ 3 root root 4096 Nov 24 01:20 image
drwxr-x--- 3 root root 4096 Nov 24 01:20 network
drwx------ 2 root root 4096 Nov 24 01:20 swarm
drwx------ 2 root root 4096 Nov 24 10:09 tmp
drwx------ 2 root root 4096 Nov 24 01:20 trust
drwx------ 3 root root 4096 Nov 24 14:28 volumes
[root@tiejiang docker]# cd volumes/
[root@tiejiang volumes]# ls
4c60775f938a840e5484aee9088bc612977c25a01f5bd164690d94c962e66e60  metadata.db
[root@tiejiang volumes]# cd 4c60775f938a840e5484aee9088bc612977c25a01f5bd164690d94c962e66e60/
[root@tiejiang 4c60775f938a840e5484aee9088bc612977c25a01f5bd164690d94c962e66e60]# ls
_data
[root@tiejiang 4c60775f938a840e5484aee9088bc612977c25a01f5bd164690d94c962e66e60]# cd _data/    容器内的文件实际在物理机上面的的保存目录
[root@tiejiang _data]# ls
hehe

[root@tiejiang ~]# docker run -d --name nginx-volume-test2 -v /data/mysql:/mysql daocloud.io/library/nginx   
f7278ce9bd88c26a0c5aaefcb2b39f1f9df0066bc94edb7a530213815e166f5e

#-v /data/mysql:/mysql  表示把物理机的/data/mysql目录挂载到容器内的/mysql目录下面


[root@tiejiang ~]# docker run -d --name nginx-volumes -v /data/mysql:/mysql daocloud.io/library/nginx
28c616e44352fc4eafeb2f87dbbb7b6eb9df447235afe027034efa96df1c5071

[root@tiejiang ~]# 
[root@tiejiang ~]# docker run -d --name web-node1 --volumes-from nginx-volumes daocloud.io/library/nginx
0f022ce56e8b800cb1a4ac76bb8a326d42e198093146e8661ad3ac8925ad317d
[root@tiejiang ~]# 
[root@tiejiang ~]# docker run -d --name web-node2 --volumes-from nginx-volumes daocloud.io/library/nginx
03d5e88c15f6604eeee2b8af500b8f356ba69adc34710f3c19b813530f19dc3d

原文链接:

编辑于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏散尽浮华

centos7下部署iptables环境纪录(关闭默认的firewalle)

CentOS7默认的防火墙不是iptables,而是firewall. 由于习惯了用iptables作为防火墙,所以在安装好centos7系统后,会将默认的fi...

1915
来自专栏linux驱动个人学习

进程状态

state域能够取5个互为排斥的值(通俗一点就是这五个值任意两个不能一起使用,只能单独使用)。系统中的每个进程都必然处于以上所列进程状态中的一种。 状态描述...

3004
来自专栏云计算与大数据

Linux命令-top

top 命令是 Linux 下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器。

713
来自专栏北京马哥教育

Linux 进程管理之四大名捕

一、四大名捕 四大名捕,最初出现于温瑞安创作的武侠小说,是朝廷中正义力量诸葛小花的四大徒弟,四人各怀绝技,分别是轻功暗器高手“无情”、内功卓越的高手“铁手”、腿...

2784
来自专栏CaiRui

Centos6和7的区别

1、init系统 Linux 操作系统的启动首先从 BIOS 开始,接下来进入 boot loader,由 bootloader 载入内核,进行内核初始化。内核...

3419
来自专栏搜云库

操作系统和数据库基础

进程与线程的差别 进程是程序的一次执行。线程可以理解为进程中执行的一段程序片段。在一个多任务环境下中下面的概念可以帮助我们理解两者的区别。 进程间是独立的...

17410
来自专栏米扑专栏

Linux iptables 防火墙常用规则

2597
来自专栏腾讯技术工程官方号的专栏

鹅厂 TDSQL XA 事务隔离级别的奥秘

TDSQL XA 全局事务(global transaction)是指用户客户端连接到 TDSQL XA 分布式数据库系统后发起和执行的事务,也就是 TDSQL...

2832
来自专栏乐沙弥的世界

MySQL 复制简要描述及示例

    主从复制技术在MySQL中被广泛使用,主要用于同步一台服务器上的数据至多台从服务器,可以用于实现负载均衡,高可用和故障切换,以及提供备份等等。MySQL...

701
来自专栏蓝天

Linux操作系统下IPTables配置方法详解

Chain RH-Firewall-1-INPUT (0 references)

773

扫码关注云+社区