安装Docker
这里用的是阿里巴巴镜像站提供的方法安装Docker-CE(社区版的Docker,免费的)
# step 1: 安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3: 更新并安装Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce
# Step 4: 开启Docker服务
sudo service docker start
更换国内的镜像源,拉镜像的速度快一些
# 编辑如下文件
vi /etc/docker/daemon.json
#直接添加如下镜像源
{
"registry-mirrors": ["http://hub-mirror.c.163.com"]
}
基于Dockerfile构建我们自己的镜像
Dockerfile是Docker提供的可以构建我们自己需要的镜像的一个工具。
- 首先,我把项目目录放在/data/web/里,cd就是我的项目的文件目录。
[root@localhost web] ls
cd
- 创建,编写Dockerfile文件,Dockerfile文件用于构建我自己的镜像。
[root@localhost web] vi Dockerfle
填写以下内容
# 表示我的镜像是基于nginx修改的
FROM nginx
# 创作者 yanggeol
MAINTAINER yanggeol
# 把我的项目cd/里的所有文件复制到/usr/share/nginx/html/ cd/可以改成自己的项目目录
COPY cd/ /usr/share/nginx/html/
# 把Nginx的配置文件覆盖镜像里面的nginx.conf文件
COPY nginx.conf /etc/nginx/nginx.conf
# 镜像构建完成后输出的提示
RUN echo 'echo init ok!!'
- 编写nginx.conf
注意:index index.html index.htm 中的 index.html为自己网站主页的名称
[root@localhost web] vi nginx.conf
填写以下内容
worker_processes 3;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm; # index.html为网站主页的名称
}
error_page 500 502 503 504 /50x.html;
}
}
- 构建我自己要的镜像
首先,经过上面创建文件,我的web目录有如下cd文件夹,Dockerfile,nginx.conf文件
[root@localhost web] ls
cd Dockerfile nginx.conf
然后,构建镜像,test是构建的镜像的名称,v1相当于版本号,标记的作用
docker build . -t test:v1
构建成功有如下信息展示
echo init ok!!
Removing intermediate container 9e868b716d0f
---> 90ba31664e47
Successfully built 90ba31664e47 # 镜像ID
Successfully tagged test:v1 # 镜像名称,版本号
如下,第一个就是刚才创建成功的镜像
[root@localhost web] docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test v1 90ba31664e47 27 minutes ago 141MB
nginx latest bc9a0695f571 8 days ago 133MB
运行
表示用docker内部的80端口映射到服务器的80端口,启动后就可以通过IP+端口访问自己的网站了
docker run -p 80:80 test:v1
如下,网站正在运行
[root@localhost web] docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8a969df68b33 test:v1 "/docker-entrypoint.…" 29 minutes ago Up 28 minutes 0.0.0.0:80->80/tcp distracted_borg