Docker简易安装及命令实例

docker ~ ~ ~

Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源。

Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。

容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。

Docker 从 17.03 版本之后分为 CE(Community Edition: 社区版) 和 EE(Enterprise Edition: 企业版),我们用社区版就可以了。

安装

centos安装:

前提条件

Docker 运行在 CentOS 7 上,要求系统为64位、系统内核版本为 3.10 以上。

Docker 运行在 CentOS-6.5 或更高的版本的 CentOS 上,要求系统为64位、系统内核版本为 2.6.32-431 或者更高版本。

1、安装依赖包

yum install -y yum-utils device-mapper-persistent-data lvm2

2、设置镜像源

yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 

3、更新yum缓存

yum makecache fast

4、安装docker-ce

yum -y install docker-ce

5、启动

systemctl start docker

 docker命令

查看docker程序是否存在,功能是否正常

[root@bogon docker]# docker info
Client:
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 19.03.1
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
..........

运行创建一个容器,-i 保证容器stdin开启,-t分配一个伪tty终端,检查是否有centos镜像没有拉取一个

[root@bogon docker]# docker run -i -t centos /bin/bash
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
d8d02d457314: Pull complete 
Digest: sha256:307835c385f656ec2e2fec602cf093224173c51119bbebd602c53c3653a3d6eb
Status: Downloaded newer image for centos:latest

创建成功后,执行、bin/bash进入容器,id是daa530b55cb4,并可以在容器内进行操作,exit;退出容器

[root@daa530b55cb4 /]# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 daa530b55cb4

列出容器,ps 只能看到正在运行的,-a 列出所有。ID、创建容器镜像、容器最后执行的命令、创建时间、退出状态、名称

[root@bogon docker]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

[root@bogon docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
daa530b55cb4        centos              "/bin/bash"         5 minutes ago       Exited (127) 31 seconds ago                       elastic_burnell

容器名称,创建时会自动生成一个名称,可以在创建时指定名称,方便用来识别和使用

docker run --name centos1 -i -t centos /bin/bash

启动已经停止的容器,后面可以使id或名称都可以。启动但不会进入。start 启动  restart重启 stop关闭

docker start/restart/stop centos1    

进入正在运行的容器

docker attach centos1

创建守护容器俗称后台运行的,使用-d 即可,后面的while用于测试

docker -i -t -d centos7.5 /bin/sh -c "while true; do echo hello; sleep 2; done"

docker logs 命令获取容器日志。可加 -f 来时事监控输出,ctrl+c 退出跟踪

[root@bogon docker]# docker logs ubuntu
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
........

top查看容器内进程

[root@bogon docker]# docker top ubuntu
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                3496                3480                0                   22:30               ?                   00:00:00            /bin/sh -c while true; do echo hello world; sleep 2; done
root                3687                3496                0                   22:35               ?                   00:00:00            sleep 2

stats统计信息,显示一个或者多个容器的cpu、内存等

[root@bogon docker]# docker stats ubuntu elastic_burnell

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
d486e9aa7754        ubuntu              0.15%               168KiB / 974.6MiB   0.02%               648B / 0B           0B / 0B             2
daa530b55cb4        elastic_burnell     0.00%               380KiB / 974.6MiB   0.04%               648B / 0B           41kB / 0B           1

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
d486e9aa7754        ubuntu              0.15%               168KiB / 974.6MiB   0.02%               648B / 0B           0B / 0B             2
daa530b55cb4        elastic_burnell     0.00%               380KiB / 974.6MiB   0.04%               648B / 0B           41kB / 0B           1

在容器中运行后台任务

[root@bogon docker]# docker exec -d ubuntu touch /home/yy

 删除容器 -f强制,1.62开始可以-f,以前的需要先stop

#删除指定
docker rm ID

#删除所有
docker rm `docker ps -a -q`

 查找镜像 仓库名 镜像描述 用户评价 是否官方 自动构建

[root@bogon file]# docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   5531                [OK]                
ansible/centos7-ansible            Ansible on Centos7                              122                                     [OK]
jdeathe/centos-ssh                 CentOS-6 6.10 x86_64 / CentOS-7 7.6.1810 x86…   111                                     [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   99                                      [OK]
centos/mysql-57-centos7            MySQL 5.7 SQL database server                   62                                      
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              57                                      [OK]
tutum/centos                       Simple CentOS docker image with SSH access      44                                      
centos/postgresql-96-centos7       PostgreSQL is an advanced Object-Relational …   39                                      
kinogmt/centos-ssh                 CentOS with SSH                                 29                                      [OK]
pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag names…   10                                      
nathonfowlie/centos-jre            Latest CentOS image with the JRE pre-install8                                       [OK]
drecom/centos-ruby                 centos ruby                                     6                                       [OK]
pivotaldata/centos                 Base centos, freshened up a little with a Do…   3                                       
darksheer/centos                   Base Centos Image -- Updated hourly             3                                       [OK]
mamohr/centos-java                 Oracle Java 8 Docker image based on Centos 7    3                                       [OK]
pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile t…   2                                       
pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated wi…   2                                       
miko2u/centos6                     CentOS6 日本語環境                                   2                                       [OK]
mcnaughton/centos-base             centos base image                               1                                       [OK]
blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                                       [OK]
indigo/centos-maven                Vanilla CentOS 7 with Oracle Java Developmen…   1                                       [OK]
pivotaldata/centos6.8-dev          CentosOS 6.8 image for GPDB development         0                                       
pivotaldata/centos7-dev            CentosOS 7 image for GPDB development           0                                       
smartentry/centos                  centos with smartentry                          0                                       [OK]
fortinj66/centos7-s2i-nodejs       based off of ryanj/centos7-s2i-nodejs.  Bigg…   0                                    
原文地址:https://www.cnblogs.com/aloneysir/p/11419126.html