docker快速入门测试
########################################## #docker安装配置 #环境centos7 #配置docker阿里源 echo ‘#Docker [docker-ce-stable] name=Docker CE - Aliyun baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/ enabled=1 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg ‘>/etc/yum.repos.d/docker-ce.repo yum makecache fast #安装必要的系统工具 yum install -y device-mapper-persistent-data lvm2 #安装 docker-ce yum install -y docker-ce #启动docker systemctl enabled docker systemctl start docker docker info docker version #默认镜像源慢,配置镜像加速 #申请阿里云加速地址https://dev.aliyun.com/ #docker阿里云加速配置 mkdir -p /etc/docker tee /etc/docker/daemon.json <<-‘EOF‘ { "registry-mirrors": ["https://56px195b.mirror.aliyuncs.com"] } EOF systemctl daemon-reload systemctl restart docker ########################################## #测试运行 hello-world docker run hello-world ##########################################
#运行容器
########################################## #运行容器 #安装nginx #搜索、下载镜像 docker search nginx docker pull nginx docker images nginx #运行容器mynginx docker run -p 80:80 --name mynginx -d nginx #查看端口 netstat -antp|grep 80 #访问测试 curl 127.0.0.1 #外部浏览器访问ip正常,部署成功 #进入Nginx容器 docker exec -it mynginx /bin/sh #退出容器 exit Ctrl+d #快捷键 #列出容器 docker ps -a #删除容器 docker rm mynginx ##########################################
运行Nginx部署网站
########################################## #接下来思考问题: #Nginx配置、查看日志、部署网站 #需要把外部的目录或文件映射到docker容器 #创建目录 Ngdir=/www/docker/nginx mkdir -p $Ngdir/{www,log,conf/conf.d} #创建配置(采用默认配置去注释) echo ‘#man config user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘ ‘$status $body_bytes_sent "$http_referer" ‘ ‘"$http_user_agent" "$http_x_forwarded_for"‘; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; include /etc/nginx/conf.d/*.conf; } ‘>$Ngdir/conf/nginx.conf # echo ‘#nginx server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 404 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } ‘>$Ngdir/conf/conf.d/default.conf #html echo ‘Welcome to nginx!‘>$Ngdir/www/index.html echo ‘error_page 500 502 503 504 404‘>$Ngdir/www/50x.html #使用nginx镜像,创建容器mynginx docker run -p 80:80 --name mynginx -v $Ngdir/conf/nginx.conf:/etc/nginx/nginx.conf:ro -v $Ngdir/conf/conf.d:/etc/nginx/conf.d:ro -v $Ngdir/www:/usr/share/nginx/html:rw -v $Ngdir/log:/var/log/nginx:rw -d nginx #测试html内容 curl 127.0.0.1 curl 127.0.0.1/123 #查看error.log cat $Ngdir/log/error.log #测试成功 ^_^ # 参数说明: # -p 80:80:本地80端口:映射docker容器80端口 # -v $Ngdir/log:/var/log/nginx 主机log目录挂载到容器log/nginx ##########################################
########################################## #常用命令 #获取容器PID docker inspect --format "{{.State.Pid}}" myngix #docker查看容器IP docker inspect --format=‘{{.NetworkSettings.IPAddress}}‘ $(docker ps -a -q) #查看变量 docker exec -it mynginx env 列出容器:docker ps -a 查看最近生成的容器:docker ps -l 查看正在运行的容器:docker ps 显示容器的标准输出:docker logs CONTAINERID 使用docker run的-i -t参数就会响应这种交互 停止正在运行的容器:docker stop CONTAINERID 查看容器或镜像的详细信息:docker inspect CONTAINERID|IMAGE 删除容器:docker rm CONTAINERID 查看所有容器ID:docker ps -a -q 删除所有的容器:docker rm $(docker ps -a -q) 删除镜像:docker rmi IMAGE ##########################################