系统版本
[root@linux-node3 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)

安装docker
[root@linux-node3 ~]# yum install docker-io -y

启动docker
[root@linux-node3 ~]# systemctl start docker

搜索docker镜像
[root@linux-node3 ~]# docker search centos
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/centos The official build of CentOS. 4564 [OK]
docker.io docker.io/ansible/centos7-ansible Ansible on Centos7 115 [OK]
docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.9 x86_64 / CentOS-7 7.4.1708 x8... 98 [OK]
docker.io docker.io/consol/centos-xfce-vnc Centos container with "headless" VNC sessi... 59 [OK]
docker.io docker.io/imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 44 [OK]
docker.io docker.io/tutum/centos Simple CentOS docker image with SSH access 43
docker.io docker.io/centos/mysql-57-centos7 MySQL 5.7 SQL database server 35
docker.io docker.io/gluster/gluster-centos Official GlusterFS Image [ CentOS-7 + Glu... 31 [OK]
docker.io docker.io/openshift/base-centos7 A Centos7 derived base image for Source-To... 31
docker.io docker.io/centos/python-35-centos7 Platform for building and running Python 3... 28
docker.io docker.io/centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relationa... 25
docker.io docker.io/kinogmt/centos-ssh CentOS with SSH 22 [OK]
docker.io docker.io/centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or b... 15
docker.io docker.io/openshift/jenkins-2-centos7 A Centos7 based Jenkins v2.x image for use... 14
docker.io docker.io/pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag nam... 7
docker.io docker.io/openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 ima... 6
docker.io docker.io/openshift/jenkins-1-centos7 DEPRECATED: A Centos7 based Jenkins v1.x i... 4
docker.io docker.io/openshift/wildfly-101-centos7 A Centos7 based WildFly v10.1 image for us... 4
docker.io docker.io/darksheer/centos Base Centos Image -- Updated hourly 3 [OK]
docker.io docker.io/pivotaldata/centos Base centos, freshened up a little with a ... 2
docker.io docker.io/blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
docker.io docker.io/pivotaldata/centos-mingw Using the mingw toolchain to cross-compile... 1
docker.io docker.io/jameseckersall/sonarr-centos Sonarr on CentOS 7 0 [OK]
docker.io docker.io/pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated ... 0
docker.io docker.io/smartentry/centos centos with smartentry 0 [OK]

将镜像拉到本地来
[root@linux-node3 ~]# docker pull centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
Digest: sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf
Status: Image is up to date for docker.io/centos:latest

查看本地镜像
[root@linux-node3 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 5182e96772bf 4 days ago 200 MB

运行docker容器
[root@linux-node3 ~]# docker run centos /bin/echo 'hello world'
hello world

查看容器
[root@linux-node3 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3d682a89841 centos "/bin/echo 'hello ..." 47 seconds ago Exited (0) 46 seconds ago mystifying_heisenberg

运行容器并进入终端
[root@linux-node3 ~]# docker run --name mydocker -it centos /bin/bash
[root@743b7ce7e900 /]#
--name 给容器取名
-i 让容器输入终端保持打开的状态
-t 开一个伪终端tty

[root@743b7ce7e900 ~]#ls
另开一个终端差段docker状态
[root@linux-node3 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
743b7ce7e900 centos "/bin/bash" 13 minutes ago Up 13 minutes mydocker
e3d682a89841 centos "/bin/echo 'hello ..." 22 minutes ago Exited (0) 22 minutes ago mystifying_heisenberg

退出容器再查看状态
[root@743b7ce7e900 ~]# exit
exit
[root@linux-node3 ~]#
另开一个终端差段docker状态
[root@linux-node3 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
743b7ce7e900 centos "/bin/bash" 13 minutes ago Exited (0) 2 seconds ago mydocker
e3d682a89841 centos "/bin/echo 'hello ..." 23 minutes ago Exited (0) 23 minutes ago mystifying_heisenberg

[root@linux-node3 ~]# docker run -d --name mynginx nginx
后台启动名为mynginx的docker容器
-d 后台启动
--name 指定名称
nginx 指定镜像

删除docker容器(删除对应的容器ID即可)
[root@linux-node3 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4d219df0f742 nginx "nginx -g 'daemon ..." 2 minutes ago Up 2 minutes 80/tcp mynginx
e3d682a89841 centos "/bin/echo 'hello ..." 39 minutes ago Exited (0) 39 minutes ago mystifying_heisenberg
[root@linux-node3 ~]# docker rm e3d682a89841
e3d682a89841
[root@linux-node3 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4d219df0f742 nginx "nginx -g 'daemon ..." 3 minutes ago Up 2 minutes 80/tcp mynginx

查看最后一个创建的容器
[root@linux-node3 ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4d219df0f742 nginx "nginx -g 'daemon ..." 9 minutes ago Up 9 minutes 80/tcp mynginx

进入容器(docker bug)
[root@linux-node3 ~]# docker attach 4d219df0f742
直接通过 attach 进入容器,无法进去,需要用到nsenter命令

获取容器的PID
[root@linux-node3 ~]# docker inspect --format "{{.State.Pid}}" mynginx
7784

进入容器:
[root@linux-node3 ~]# nsenter --target 7784 --mount --uts --ipc --net --pid