Getting started with docker basic deployment 2, the necessary technology for microservices, a monthly salary of tens of thousands is not a dream

Container technology-2
image management
commit packaged image

docker commit container id new image name: label

[root@node-0001 ~]# docker run -it centos:latest
[root@02fd1719c038 ~]# rm -f /etc/yum.repos.d/*.repo
[root@02fd1719c038 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.myhuaweicloud.com/repo/CentOS-Base-7.repo
[root@02fd1719c038 ~]# yum install -y net-tools vim-enhanced tree bash-completion iproute psmisc && yum clean all
[root@02fd1719c038 ~]# exit
[root@node-0001 ~]# docker commit 02fd1719c038 myos:latest

Dockerfile packaged image

Dockerfile syntax

Syntax instructions Grammar description
FROM Base mirror
RUN There can be multiple commands to be executed when making a mirror
ADD Copy files to the mirror, automatically decompress
COPY Copy the file to the mirror without decompressing
EXBOSE Declare open ports
ENV Set the environment variables after the container is started
WORKDIR Define the default working directory of the container (equal to cd)
CMD The command executed when the container is started can only have one CMD

Use Dockerfile to create a mirror

docker build -t image name: the directory where the label Dockerfile is located

Make apache mirror

The CMD command can view the startup command ExecStart of the service file (/lib/systemd/system/httpd.service)

ENV environment variable query the content of the file specified by the environment variable configuration file EnvironmentFile in the service file

[root@node-0001 ~]# mkdir web; cd web
[root@node-0001 web]# vim Dockerfile
FROM myos:latest
RUN  yum install -y httpd php
ENV  LANG=C
ADD  webhome.tar.gz  /var/www/html/
WORKDIR /var/www/html/
EXPOSE 80
CMD ["/usr/sbin/httpd", "-DFOREGROUND"]
# 拷贝 webhome.tar.gz 到当前目录中
[root@node-0001 web]# docker build -t myos:httpd .;

View and verify images

[root@node-0001 web]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myos                httpd               db15034569da        12 seconds ago      412MB
myos                latest              867409e412c8        2 hours ago         281MB
[root@node-0001 web]# docker rm -f $(docker ps -aq)
[root@node-0001 web]# docker run -itd myos:httpd
[root@node-0001 web]# curl http://172.17.0.2/info.php
<pre>
Array
(
    [REMOTE_ADDR] => 172.17.0.1
    [REQUEST_METHOD] => GET
    [HTTP_USER_AGENT] => curl/7.29.0
    [REQUEST_URI] => /info.php
)
php_host: 	6c9e124bee1a
1229

Make php-fpm mirror

[root@node-0001 ~]# yum install -y php-fpm
[root@node-0001 ~]# mkdir php; cd php
[root@node-0001 php]# cp /etc/php-fpm.d/www.conf ./
12:  listen = 0.0.0.0:9000
24:  ;listen.allowed_clients = 127.0.0.1
 
[root@node-0001 php]# vim Dockerfile
FROM myos:latest
RUN  yum install -y php-fpm
COPY www.conf /etc/php-fpm.d/www.conf
EXPOSE 9000
WORKDIR /usr/local/nginx/html
COPY info.php info.php
CMD ["/usr/sbin/php-fpm", "--nodaemonize"]
[root@node-0001 php]# docker build -t myos:php-fpm .

Make nginx mirror

[root@node-0001 ~]# yum install -y gcc make pcre-devel openssl-devel
[root@node-0001 ~]# useradd nginx
[root@node-0001 ~]# tar -zxvf nginx-1.12.2.tar.gz
[root@node-0001 ~]# cd nginx-1.12.2
[root@node-0001 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
[root@node-0001 nginx-1.12.2]# make && make install
[root@node-0001 nginx-1.12.2]# cd /usr/local/
 
[root@node-0001 local]# tar czf nginx.tar.gz nginx
[root@node-0001 local]# mkdir /root/nginx ;cd /root/nginx
[root@node-0001 nginx]# cp /usr/local/nginx.tar.gz ./
[root@node-0001 nginx]# vim Dockerfile 
FROM myos:latest
RUN  yum install -y pcre openssl && useradd nginx
ADD  nginx.tar.gz /usr/local/
EXPOSE 80
WORKDIR /usr/local/nginx/html
CMD  ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
[root@node-0001 nginx]# docker build -t myos:nginx .

Publish container service

External publishing service

Bind his node-0001 with a public IP

docker run -itd -p host port: container port image name: label

# 把 node-0001 变成 apache 服务
[root@node-0001 ~]# docker run -itd -p 80:80 myos:httpd
 
# 把 node-0001 变成 nginx 服务,首先必须停止 apache
[root@node-0001 ~]# docker stop $(docker ps -q)
[root@node-0001 ~]# docker run -itd -p 80:80 myos:nginx

Authentication method: Just visit through the browser

Container shared volume

docker run -itd -v host object: object in the container image name: label

Use the shared volume to dynamically modify the configuration file in the container

[root@node-0001 ~]# docker run -itd --name myphp myos:php-fpm
[root@node-0001 ~]# docker inspect myphp
[root@node-0001 ~]# mkdir /var/webconf
[root@node-0001 ~]# cp /usr/local/nginx/conf/nginx.conf /var/webconf/
[root@node-0001 ~]# vim /var/webconf/nginx.conf
... ...
    fastcgi_pass   172.17.0.xx:9000;
... ...
# 映射配置文件,并启动容器
[root@localhost ~]# docker run -itd -p 80:80 \
     -v /var/webconf/nginx.conf:/usr/local/nginx/conf/nginx.conf myos:nginx

Authentication method: Just visit through the browser

Network communication between containers

Experimental architecture legend

graph LR
  subgraph node-0001
    style node-0001 color:#00ff00,fill:#7777ff
      subgraph 容器1
      style 容器1 color:#00ff00,fill:#88aaff
        APP1(Nginx)
        NET1{
   
   {共享网络}}
      end
      subgraph 容器2
      style 容器2 color:#00ff00,fill:#88aaff
        APP2(PHP)
      end
    APP1 --> NET1
    APP2 --> NET1
  L((共享存储卷))
  APP1 -.-> L
  APP2 -.-> L
  end
U((用户)) --> APP1

Experimental steps

[root@node-0001 ~]# mkdir -p /var/{webroot,webconf}
[root@node-0001 ~]# cd kubernetes/docker-images
[root@node-0001 ~]# cp info.php info.html /var/webroot/
[root@node-0001 ~]# cp /usr/local/nginx/conf/nginx.conf /var/webconf/
[root@node-0001 ~]# vim /var/webconf/nginx.conf
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi.conf;
        }
# 启动前端 nginx 服务,并映射共享目录和配置文件
[root@node-0001 ~]# docker run -itd --name nginx -p 80:80 \
      -v /var/webconf/nginx.conf:/usr/local/nginx/conf/nginx.conf \
      -v /var/webroot:/usr/local/nginx/html myos:nginx
# 启动后端 php 服务,并映射共享目录
[root@node-0001 ~]# docker run -itd --network=container:nginx \
      -v /var/webroot:/usr/local/nginx/html myos:php-fpm
 
# 验证服务
[root@node-0001 ~]# curl http://node-0001/info.html
<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World</font>
  </marquee>
</html>
[root@node-0001 ~]# curl http://node-0001/info.php
<pre>
Array
(
    [REMOTE_ADDR] => 172.17.0.1
    [REQUEST_METHOD] => GET
    [HTTP_USER_AGENT] => curl/7.29.0
    [REQUEST_URI] => /info.php
)
php_host: 	f705f89b45f9
1229

docker private warehouse

Docker private warehouse legend

graph TB
  H1(容器服务器<br>node-0001)
  H2(容器服务器<br>node-0002)
  I{
   
   {镜像仓库}}
  style I fill:#77ff77
  H1 --> I
  H2 --> I

Private warehouse configuration

CPU name IP address Minimum configuration
registry 192.168.1.100 1CPU, 1G memory
[root@registry ~]# yum install -y docker-distribution
[root@registry ~]# systemctl enable --now docker-distribution
[root@registry ~]# curl http://192.168.1.100:5000/v2/_catalog
{"repositories":[]}

Docker client configuration

All node nodes need to be configured, here node-0001, node-0002 must be configured

native.cgroupdriver cgroup driver, docker defaults to cgroupfs

registry-mirrors downloads the warehouse by default, it can be faster to use domestic sources

insecure-registries private warehouse address (emphasis)

[root@node-0001 ~]# vim /etc/docker/daemon.json
{
    "exec-opts": ["native.cgroupdriver=systemd"],
    "registry-mirrors": ["https://hub-mirror.c.163.com"],
    "insecure-registries":["192.168.1.100:5000", "registry:5000"]
}
[root@node-0001 ~]# docker rm -f $(docker ps -aq)
[root@node-0001 ~]# systemctl restart docker

Upload image

# 上传 myos:latest, myos:httpd, myos:nginx, myos:php-fpm
[root@node-0001 ~]# docker tag myos:latest 192.168.1.100:5000/myos:latest
[root@node-0001 ~]# docker push 192.168.1.100:5000/myos:latest

Verification Test

curl http://Warehouse IP:5000/v2/_catalog

curl http://warehouse IP:5000/v2/mirror name/tags/list

[root@node-0002 ~]# curl http://192.168.1.100:5000/v2/_catalog
{"repositories":["myos"]}
[root@node-0002 ~]# curl http://192.168.1.100:5000/v2/myos/tags/list
{"name":"myos","tags":["latest"]}
# 使用远程镜像启动容器
[root@node-0002 ~]# docker run -it 192.168.1.100:5000/myos:latest
Unable to find image '192.168.1.100:5000/myos:latest' locally
latest: Pulling from myos
7dc0dca2b151: Pull complete 
95c297b4d705: Pull complete 
Digest: sha256:d61ffc053895e2dc16f63b8a2988dfe5f34207b48b1e74d397bb3267650ba4ce
Status: Downloaded newer image for 192.168.1.100:5000/myos:latest
[root@674ebe359e44 /]#

Guess you like

Origin blog.csdn.net/qq_30566629/article/details/110222574