一、Docker简介
二、Docker安装
[root@lamp sky9890]# yum install docker
[root@lamp sky9890]# systemctl start docker.service #启动Docker服务
[root@lamp sky9890]# systemctl enable docker.service #随系统启动自动加载
[root@lamp sky9890]# docker #查看docker客户端
[root@lamp sky9890]# docker -v #版本
Docker version 1.13.1, build 4ef4b30/1.13.1
三、获取镜像
镜像是一个模板,可以从镜像服务器上获取做好的镜像。docker使用docker pull命令来获取需要的镜像。
下别分别获取centos、ubuntu、tomcat、nginx、mysql、mongo等镜像。
1.从docker镜像中查找可用nginx镜像版本获取镜像
[root@lamp sky9890]# docker search centos
[root@lamp sky9890]# docker search ubuntu
[root@lamp sky9890]# docker search tomcat
[root@lamp sky9890]# docker search nginx
2.从docker镜像中下载需要的版本
[root@lamp sky9890]# docker pull centos
[root@lamp sky9890]# docker pull mysql
[root@lamp sky9890]# docker pull tomcat
[root@lamp sky9890]# docker pull mysql:latest
[root@lamp sky9890]# docker pull python:3.5
[root@lamp sky9890]# docker pull mongo:latest
3.显示本地已经下载的镜像
[root@lamp sky9890]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/tomcat latest 4e7840b49fad 3 days ago 529 MB
docker.io/nginx latest a1523e859360 4 days ago 127 MB
docker.io/python 3.5 0320ef7199ca 4 days ago 909 MB
docker.io/mysql latest c8ad2be69a22 4 days ago 465 MB
docker.io/php latest e66ae809d99a 4 days ago 405 MB
docker.io/httpd latest c5a012f9cf45 4 days ago 165 MB
docker.io/mongo latest bcef5fd2979d 9 days ago 386 MB
docker.io/ubuntu latest 72300a873c2c 9 days ago 64.2 MB
docker.io/centos latest 470671670cac 6 weeks ago 237 MB
三、如何使用容器
1.使用 docker run 命令来在容器内运行一个应用程序。
[root@lamp sky9890]# docker run ubuntu /bin/echo "Hello World"
Hello World
2.运行交换式的容器
[root@lamp sky9890]# docker run -i -t ubuntu /bin/bash
root@1c7c38a6f99a:/#
root@1c7c38a6f99a:/# cat /proc/version
Linux version 3.10.0-862.11.6.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ) #1
SMP Tue Aug 14 21:49:04 UTC 2018
root@1c7c38a6f99a:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@1c7c38a6f99a:/usr# exit
exit
3.启动容器(后台模式)
[root@lamp sky9890]# docker run -d ubuntu /bin/sh -c "while true;do echo hello world; sleep 1; done"
1f77ab03073f192d18737110bf97ec04b72524fb34e3a570571d4cac6b4e5b31
4.查看docker进程
[root@lamp sky9890]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f77ab03073f ubuntu "/bin/sh -c 'while..." 38 seconds ago Up 36 seconds competent_ramanujan
8778ad75ade3 ubuntu "/bin/bash" 14 minutes ago Up 14 minutes ubuntu-test
7757db1528de ubuntu "/bin/bash" 19 minutes ago Up 15 minutes distracted_austin
输出详情介绍:
CONTAINER ID: 容器 ID。
IMAGE: 使用的镜像。
COMMAND: 启动容器时运行的命令。
CREATED: 容器的创建时间。
STATUS: 容器状态。
状态有7种:
created(已创建)
restarting(重启中)
running(运行中)
removing(迁移中)
paused(暂停)
exited(停止)
dead(死亡)
PORTS: 容器的端口信息和使用的连接类型(tcp\udp)。
NAMES: 自动分配的容器名称。
5.查看容器的标准输出
[root@lamp sky9890]# docker logs 1f77ab03073f
hello world
hello world
......
6.停止容器
[root@lamp sky9890]# docker stop 1f77ab03073f
1f77ab03073f
[root@lamp sky9890]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8778ad75ade3 ubuntu "/bin/bash" 18 minutes ago Up 17 minutes ubuntu-test
7757db1528de ubuntu "/bin/bash" 22 minutes ago Up 18 minutes distracted_austin
7.删除容器
[root@lamp sky9890]#docker rm -f 1f77ab03073f
四、Docker实例安装
1.Docker安装CentOS
[root@lamp sky9890]# docker run -itd --name centos-test centos
f2896d1d87c996685c7c98fb9c85241a10483fb1f2dcd843a4b0d6cab212d636
#通过exec运行容器
[root@lamp sky9890]# docker exec -it centos-test /bin/bash
[root@f2896d1d87c9 /]# exit
[root@lamp sky9890]# docker ps #安装成功
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f2896d1d87c9 centos "/bin/bash" About a minute ago Up About a minute centos-test
8778ad75ade3 ubuntu "/bin/bash" 34 minutes ago Up 34 minutes ubuntu-test
7757db1528de ubuntu "/bin/bash" 39 minutes ago Up 35 minutes distracted_austin
2.Docker安装nginx
#运行容器
[root@lamp sky9890]# docker run --name nginx-test -p 8080:80 -d nginx
5f4aa091f8244b4f21a585b8426240218312b90f46dc23f8cfdc825f1a981b6e
参数说明:
测试
五、在容器中的相关操作
1.启动CentOS容器
[root@lamp sky9890]# docker run -it centos /bin/bash
[root@b90d59c23a38 /]# lsb_release
bash: lsb_release: command not found
[root@b90d59c23a38 /]# yum install redhat-lsb -y
[root@b90d59c23a38 /]# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-
amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 8.1.1911 (Core)
Release: 8.1.1911
Codename: Core
2.启动Ubuntu容器
root@737d61040022:/# lsb-release -a
bash: lsb-release: command not found
root@737d61040022:/# apt-get install lsb-release
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package lsb-release
#出现上述原因,说明需要更新APT库
root@737d61040022:/# apt-get update
Reading package lists... Done
E: Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)
E: Unable to lock directory /var/lib/apt/lists/
#出现上述原因,是由于资源被锁的原因,可能是上次安装时没正常完成,而导致出现此状况。需要执行下面两条命令:
root@737d61040022:/# rm /var/cache/apt/archives/lock
root@737d61040022:/# rm /var/lib/apt/lists/lock
root@737d61040022:/# apt-get update #更新软件列表
root@737d61040022:/# apt-get upgrade #更新软件
root@737d61040022:/# apt-get install lsb-release
Reading package lists... Done
.........
root@737d61040022:/# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.4 LTS
Release: 18.04
Codename: bionic
root@737d61040022:/# apt-get install lsb-core
六、解决docker pull 速度慢的问题
#vi /etc/docker/daemon.json 文件中添加以下参数(没有该文件则新建):
{
"registry-mirrors": ["https://9cpn8tt6.mirror.aliyuncs.com"]
}
#systemctl daemon-reload
#systemctl restart docker