一、docker环境搭建
1、卸载旧版本docker
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2、安装依赖和设置仓库
#安装依赖
yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
#设置仓库
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
3、安装docker
yum install docker-ce docker-ce-cli containerd.io
4、启动并加入开机启动
systemctl start docker #启动
systemctl enable docker #设置为开机自启
5、验证是否安装成功
docker version
docker run hello-world
显示以下内容则表示已经安装成功了
[root@iZ2ze68ge5c1uwlkmnb9ixZ zcapp]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:0e11c388b664df8a27a901dce21eb89f11d8292f7fca1b3e3c4321bf7897bffe
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
二、利用docker搭建nginx
1、拉取镜像
#获取nginx镜像
docker search nginx
#拉取nginx镜像到本地(注:默认选取官方最新镜像),其它版本可以去DockerHub查询
docker pull nginx
docker run hub.c.163.com/library/nginx
#查看镜像库,获取本地的nginx镜像信息
#注:记录下IMAGE ID前四位,后边使用
docker images
2、启动容器,部署nginx
(1)首先测试下nginx镜像是否可用
-d 指定容器以守护进程方式在后台运行 –name 指定容器名称,此处我指定的是nginx
-p 指定主机与容器内部的端口号映射关系,格式 -p [宿主机端口号]:[容器内部端口],此处我使用了主机80端口,映射容器80端口
#前台运行,ctrl + c 可退出
docker run hub.c.163.com/library/nginx
#在后台运行
docker run -d hub.c.163.com/library/nginx
命令执行后返回了容器ID,此时我们已经成功创建了nginx容器
使用docker ps命令,可以查看到正在运行的nginx容器
(2)实际访问端口
刚才我们在运行nginx镜像的时候并没有指定端口映射,所以我们这里停掉nginx容器
docker stop containerId
1、-p(小写)是开放一个容器的端口到主机上
在后台运行,开放主机8080端口映射到容器的80端口上.
docker run -d -p 8080:80 hub.c.163.com/library/nginx
、
2、查看8080端口是否开启
netstat -na | grep 8080
3、访问8080端口
ngix部署成功!!!