天天看點

CentOS中部署Docker并配置Nginx

檢視系統版本

[root@dc02 ~]# cat /etc/redhat-release

centos linux release 7.2.1511 (core)

檢視核心版本

[root@dc02 ~]# uname -r

3.10.0-327.el7.x86_64

關閉selinux和防火牆

setenforce 0

sed -i 's#selinux=enforcing#selinux=disabled#' /etc/selinux/config

systemctl disable firewalld.service

systemctl stop firewalld.service

下載下傳docker

[root@dc02 ~]# yum install -y docker

替換國内加速器

[root@dc02 ~]# cat >/etc/docker/daemon.json <<eof

{ "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"] } eof [root@dc02 ~]# cat /etc/docker/daemon.json

加載生效

[root@dc02 ~]# systemctl daemon-reload

自啟動

[root@dc02 ~]# systemctl enable docker

[root@dc02 ~]# systemctl start docker

檢視狀态

[root@dc02 ~]# systemctl status docker

下載下傳官方centos鏡像

[root@dc02 ~]# docker pull centos

using default tag: latest

trying to pull repository docker.io/library/centos ...

latest: pulling from docker.io/library/centos

7dc0dca2b151: pull complete

digest: sha256:b67d21dfe609ddacf404589e04631d90a342921e81c40aeaf3391f6717fa5322

status: downloaded newer image for docker.io/centos:latest

檢視所有下載下傳鏡像

[root@dc02 ~]# docker images

repository tag image id created size

docker.io/centos latest 49f7960eb7e4 7 weeks ago 200 mb

啟動并進入鏡像

[root@dc02 ~]# docker run -it --name mynginx centos /bin/bash

安裝常用指令

[root@9d76d200aae9 /]# yum install -y wget vim

[root@9d76d200aae9 /]# wget -o /etc/yum.repos.d/centos-base.repo http://mirrors.aliyun.com/repo/centos-7.repo

[root@9d76d200aae9 /]# wget -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

安裝nginx

[root@9d76d200aae9 /]# yum install -y nginx

#docker預設禁止背景運作nginx

[root@9d76d200aae9 /]# vim /etc/nginx/nginx.conf

#在"user nginx;"下面加上一行"daemon off;"

檢查nginx配置檔案文法

[root@9d76d200aae9 /]# nginx -t

啟動nginx

[root@9d76d200aae9 /]# nginx

啟動後指令行是夯築的,是以打開一個新的ssh連接配接檢視鏡像

[root@dc02 ~]# docker ps

container id image command created status ports names

9d76d200aae9 centos "/bin/bash" 14 minutes ago up 14 minutes mynginx

送出鏡像并檢視

[root@dc02 ~]# docker commit -m "add new docker images" mynginx test/nginx:v1

sha256:5e28845136a33dc8e6175248086a90f9195dda4bdfc014a89b7ee32e37cc24d0

repository tag image id created size

test/nginx v1 5e28845136a3 about a minute ago 420 mb

docker.io/centos latest 49f7960eb7e4 7 weeks ago 200 mb

測試nginx連通性

[root@dc02 ~]# docker run -p 80:80 --name test_nginx 5e28845136a3 nginx

原文出處轉載連結