glcui 发表于 2019-2-21 09:35:40

Docker入门实操

  一、安装级运行docker


[*]  安装
# curl -sSL https://get.daocloud.io/docker | sh

[*]运行
# which docker
/usr/bin/docker
# docker version
Client:
Version:         18.06.0-ce
API version:       1.38
Go version:      go1.10.3
Git commit:      0ffa825
Built:             Wed Jul 18 19:08:18 2018
OS/Arch:         linux/amd64
Experimental:      false
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
  报错

# systemctl status docker.service
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: failed (Result: start-limit) since 二 2018-07-31 11:29:01 CST; 46s ago
Docs: https://docs.docker.com
Process: 35176 ExecStart=/usr/bin/dockerd (code=exited, status=1/FAILURE)
Main PID: 35176 (code=exited, status=1/FAILURE)
7月 31 11:29:01 localhost.localdomain systemd: Failed to start Docker Application Container Engine.
7月 31 11:29:01 localhost.localdomain systemd: Unit docker.service entered failed state.
7月 31 11:29:01 localhost.localdomain systemd: docker.service failed.
7月 31 11:29:01 localhost.localdomain systemd: docker.service holdoff time over, scheduling restart.
7月 31 11:29:01 localhost.localdomain systemd: start request repeated too quickly for docker.service
7月 31 11:29:01 localhost.localdomain systemd: Failed to start Docker Application Container Engine.
7月 31 11:29:01 localhost.localdomain systemd: Unit docker.service entered failed state.
7月 31 11:29:01 localhost.localdomain systemd: docker.service failed.
  这个可以通过如下方式解决:
查看文件系统 /etc/docker/daemon.json 有没有这个文件,没有测创建它包括二级目录 docker
在daemon.json文件中输入以下内容:

{
"storage-driver"
:
"devicemapper"
}
  如果daemon.json文件包含格式不正确的JSON,Docker将无法启动。
  再次启动docker,正常

# systemctl start docker
查看状态
# docker version
Client:
Version:         18.06.0-ce
API version:       1.38
Go version:      go1.10.3
Git commit:      0ffa825
Built:             Wed Jul 18 19:08:18 2018
OS/Arch:         linux/amd64
Experimental:      false
Server:
Engine:
Version:          18.06.0-ce
API version:      1.38 (minimum version 1.12)
Go version:       go1.10.3
Git commit:       0ffa825
Built:            Wed Jul 18 19:10:42 2018
OS/Arch:          linux/amd64
Experimental:   false
  3 查看帮助

docker help
  4 查看本地仓库的镜像

# docker images
REPOSITORY          TAG               IMAGE ID            CREATED             SIZE
  5 在DockerHub 上搜索共享镜像

# docker search centos7
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ansible/centos7-ansible         Ansible on Centos7                              114                                    
centos/mysql-57-centos7         MySQL 5.7 SQL database server                   35                                    
openshift/base-centos7            A Centos7 derived base image for Source-To-I…   29
(省略)
  6 拉取共享镜像
在拉取前需要更改/etc/docker/daemon.json(国内访问dockerhub速度你懂得,在这里使用了ustc的镜像加速)

# more /etc/docker/daemon.json
{"registry-mirrors":["https://docker.mirrors.ustc.edu.cn"]}
# docker pull docker.io/ansible/centos7-ansible
Using default tag: latest
latest: Pulling from ansible/centos7-ansible
45a2e645736c: Pull complete
1c3acf573616: Pull complete
edcb61e55ccc: Pull complete
cbae31bad30a: Pull complete
aacbdb1e2a62: Pull complete
fdeea4fb835c: Pull complete
Digest: sha256:39eff7d56b96530d014083cd343f7314c23acbd1ecf37eb75a71a2f6584d0b02
Status: Downloaded newer image for ansible/centos7-ansible:latest
  7.再次查看本地仓库:

# docker images
REPOSITORY                TAG               IMAGE ID            CREATED             SIZE
ansible/centos7-ansible   latest            688353a31fde      19 months ago       447MB
  二、运行容器


[*]运行一个名字为ws的容器,并获取一个shell;
# docker run -it --name=ws docker.io/ansible/centos7-ansible /bin/bash
#
  此时我们进入ws容器的shell,在这里可以执行一些基本命令。
安装ip命令

# yum -y install iproute
Loaded plugins: fastestmirror, ovl
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base                                                                               | 3.6 kB00:00:00   
epel/x86_64/metalink                                                               | 5.8 kB00:00:00   
epel                                                                               | 3.2 kB00:00:00   
extras                                                                           | 3.4 kB00:00:00   
updates                                                                            | 3.4 kB00:00:00   
(1/7): base/7/x86_64/group_gz                                                      | 166 kB00:00:00   
(2/7): epel/x86_64/group_gz                                                      |88 kB00:00:00   
(3/7): extras/7/x86_64/primary_db                                                | 172 kB00:00:00   
(4/7): epel/x86_64/updateinfo                                                      | 930 kB00:00:01   
(5/7): epel/x86_64/primary                                                         | 3.6 MB00:00:01   
(6/7): base/7/x86_64/primary_db                                                    | 5.9 MB00:00:02   
(7/7): updates/7/x86_64/primary_db            
(省略)
# ip a
1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
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
24: eth0@if25:mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
  看着和在宿主机上没有区别?

  从docker里面可以直接进入物理机

# ssh 192.168.10.138
The authenticity of host '192.168.10.138 (192.168.10.138)' can't be established.
ECDSA key fingerprint is 0c:23:1d:1b:c7:95:e8:18:b4:4c:6d:de:98:03:9e:6e.
Are you sure you want to continue connecting (yes/no)?
  2.离开容器(ctrl+p+q)

# docker ps
CONTAINER ID      IMAGE                     COMMAND             CREATED             STATUS            PORTS               NAMES
ca8bd201ee4b      ansible/centos7-ansible   "/bin/bash"         2 hours ago         Up 2 hours                              ws
  容器ID使用镜像执行命令创建时间名字


[*]  连接正在运行的容器

# docker attach ws
#
[*]退出容器
# exit
exit
# docker ps
CONTAINER ID      IMAGE               COMMAND             CREATED             STATUS            PORTS               NAMES
  退出容器后,docker ps 已经看不到,使用-a参数

# docker ps -a
CONTAINER ID      IMAGE                     COMMAND             CREATED             STATUS                            PORTS               NAMES
ca8bd201ee4b      ansible/centos7-ansible   "/bin/bash"         2 hours ago         Exited (255) About a minute ago                     ws

[*]启动一个退出的容器
# docker start ws
ws
# docker ps
CONTAINER ID      IMAGE                     COMMAND             CREATED             STATUS            PORTS               NAMES
ca8bd201ee4b      ansible/centos7-ansible   "/bin/bash"         2 hours ago         Up 6 seconds                            ws
  6 使用宿主机停止容器

# docker ps
CONTAINER ID      IMAGE                     COMMAND             CREATED             STATUS            PORTS               NAMES
ca8bd201ee4b      ansible/centos7-ansible   "/bin/bash"         2 hours ago         Up About a minute                     ws
# docker stop ws
ws
# docker ps
CONTAINER ID      IMAGE               COMMAND             CREATED             STATUS            PORTS               NAMES

[*]删除容器(先停止再删除)
# docker ps -a
CONTAINER ID      IMAGE                     COMMAND             CREATED             STATUS                        PORTS               NAMES
ca8bd201ee4b      ansible/centos7-ansible   "/bin/bash"         2 hours ago         Exited (137) 46 seconds ago                     ws
# docker rm ca8bd201ee4b
ca8bd201ee4b
  参考https://mp.weixin.qq.com/s/sqsOraQmjNEP4OSPJi8g8Q



页: [1]
查看完整版本: Docker入门实操