一、安装级运行docker
-
安装
[root@localhost ~]# curl -sSL https://get.daocloud.io/docker | sh
- 运行
[root@localhost ~]# which docker /usr/bin/docker
[root@localhost ~]# 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?
报错
[root@localhost ~]# 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[1]: Failed to start Docker Application Container Engine.
7月 31 11:29:01 localhost.localdomain systemd[1]: Unit docker.service entered failed state.
7月 31 11:29:01 localhost.localdomain systemd[1]: docker.service failed.
7月 31 11:29:01 localhost.localdomain systemd[1]: docker.service holdoff time over, scheduling restart.
7月 31 11:29:01 localhost.localdomain systemd[1]: start request repeated too quickly for docker.service
7月 31 11:29:01 localhost.localdomain systemd[1]: Failed to start Docker Application Container Engine.
7月 31 11:29:01 localhost.localdomain systemd[1]: Unit docker.service entered failed state.
7月 31 11:29:01 localhost.localdomain systemd[1]: docker.service failed.
这个可以通过如下方式解决:
查看文件系统 /etc/docker/daemon.json 有没有这个文件,没有测创建它包括二级目录 docker
在daemon.json文件中输入以下内容:
{
"storage-driver"
:
"devicemapper"
}
如果daemon.json文件包含格式不正确的JSON,Docker将无法启动。
再次启动docker,正常
[root@localhost ~]# systemctl start docker
查看状态
[root@localhost ~]# 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 查看本地仓库的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
5 在DockerHub 上搜索共享镜像
[root@localhost ~]# docker search centos7
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ansible/centos7-ansible Ansible on Centos7 114 [OK]
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的镜像加速)
[root@localhost ~]# more /etc/docker/daemon.json
{"registry-mirrors":["https://docker.mirrors.ustc.edu.cn"]}
[root@localhost ~]# 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.再次查看本地仓库:
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ansible/centos7-ansible latest 688353a31fde 19 months ago 447MB
二、运行容器
- 运行一个名字为ws的容器,并获取一个shell;
[root@localhost ~]# docker run -it --name=ws docker.io/ansible/centos7-ansible /bin/bash [root@ca8bd201ee4b ansible]#
此时我们进入ws容器的shell,在这里可以执行一些基本命令。
安装ip命令[root@ca8bd201ee4b ansible]# 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 kB 00:00:00 epel/x86_64/metalink | 5.8 kB 00:00:00 epel | 3.2 kB 00:00:00 extras | 3.4 kB 00:00:00 updates | 3.4 kB 00:00:00 (1/7): base/7/x86_64/group_gz | 166 kB 00:00:00 (2/7): epel/x86_64/group_gz | 88 kB 00:00:00 (3/7): extras/7/x86_64/primary_db | 172 kB 00:00:00 (4/7): epel/x86_64/updateinfo | 930 kB 00:00:01 (5/7): epel/x86_64/primary | 3.6 MB 00:00:01 (6/7): base/7/x86_64/primary_db | 5.9 MB 00:00:02 (7/7): updates/7/x86_64/primary_db (省略)
[root@ca8bd201ee4b ansible]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> 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: <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 link-netnsid 0 inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever
看着和在宿主机上没有区别?
从docker里面可以直接进入物理机
[root@ca8bd201ee4b ansible]# 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)
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ca8bd201ee4b ansible/centos7-ansible "/bin/bash" 2 hours ago Up 2 hours ws
容器ID 使用镜像 执行命令 创建时间 名字
-
连接正在运行的容器
[root@localhost ~]# docker attach ws [root@ca8bd201ee4b ansible]#
- 退出容器
[root@ca8bd201ee4b ansible]# exit exit [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
退出容器后,docker ps 已经看不到,使用-a参数
[root@localhost ~]# 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
- 启动一个退出的容器
[root@localhost ~]# docker start ws ws [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ca8bd201ee4b ansible/centos7-ansible "/bin/bash" 2 hours ago Up 6 seconds ws
6 使用宿主机停止容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ca8bd201ee4b ansible/centos7-ansible "/bin/bash" 2 hours ago Up About a minute ws
[root@localhost ~]# docker stop ws
ws
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 删除容器(先停止再删除)
[root@localhost ~]# 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 [root@localhost ~]# docker rm ca8bd201ee4b ca8bd201ee4b
参考https://mp.weixin.qq.com/s/sqsOraQmjNEP4OSPJi8g8Q
转载于:https://blog.51cto.com/9201316/2152564