一、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 ([email protected]) (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
......
6.停止容器
[root@lamp sky9890]# docker stop 1f77ab03073f
1f77ab03073f
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
参数说明:
--name nginx-test:容器名称。
-p 8080:80: 端口进行映射,将本地 8080 端口映射到容器内部的 80 端口。
-d nginx: 设置容器在在后台一直运行。
测试
五、在容器中的相关操作
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
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:/# 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 速度慢的问题
将docker镜像源修改为国内的阿里云:
#vi /etc/docker/daemon.json 文件中添加以下参数(没有该文件则新建):
{
"registry-mirrors": ["https://9cpn8tt6.mirror.aliyuncs.com"]
}
#systemctl daemon-reload
#systemctl restart docker