Release container server & private image warehouse | Cloud computing

1. Create a custom image

1.1 problems

This case requires two methods to create a custom image. The specific requirements are as follows:

  1. Starting containers with centos images
  2. Configure yum source in container
  3. Install software bash completion net tools iproute psmisc VIM enhanced
  4. Create a custom image myos:latest
  5. Validate custom image

1.2 steps

To implement this case, you need to follow the following steps.

Step 1: customize the image (you can operate on docker-0001 or docker-0002 hosts)

1) Use the commit method to create a custom image.

Use the existing image to start the container, modify it based on the container, and use commit to create a new image

[root@docker-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@docker-0001 ~]# docker commit 02fd1719c038 myos:latest

2) Create a custom image through Dockerfile.

The docker build command can generate an image based on the contents in the Dockerfile

The syntax format for writing Dockerfile is as follows:
FROM: basic image
RUN: there can be multiple commands executed when creating an image
ADD: copy the file to the image and decompress it automatically
COPY: COPY the file to the image without decompressing
Export: declare an open port
ENV: sets the environment variable after the container is started
WORKDIR: defines the default working directory of the container (equal to cd)
CMD: the command executed when the container is started. There can only be one CMD

The specific operation process is as follows:
Create directory mkdir mybuild
Write Dockerfile in directory

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

2. Create apache service image

2.1 problems

This case requires that you use Dockerfile to create apache service image myos:httpd to achieve the following objectives:

  1. Add default web site
  2. Set the default working directory / var/www/html

2.2 steps

To implement this case, you need to follow the following steps.

Step 1: check the help and get familiar with the command format (you can operate in docker-0001 or docker-0002)

[root@docker-0001 ~]# mkdir apache; cd apache
[root@docker-0001 apache]# 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"]
# Copy webhome tar. GZ to the current directory
[root@docker-0001 apache]# docker build -t myos:httpd .
# verification
[root@localhost web]# docker run -itd myos:httpd    
#Because it is a background service, use the - d parameter

3. Create nginx/php service image

This case requires using Dockerfile to create nginx/php service image:

3.1 steps

Step 1: create a php image

[root@docker-0001 ~]# mkdir php; cd php
[root@docker-0001 php]# vim Dockerfile
FROM myos:latest
RUN  yum install -y php-fpm
EXPOSE 9000
CMD ["/usr/sbin/php-fpm", "--nodaemonize"]
[root@docker-0001 php]# docker build -t myos:php-fpm .
# Authentication service
[root@docker-0001 ~]# docker run -itd myos:php-fpm
deb37734e52651161015e9ce7771381ee6734d1d36bb51acb176b936ab1b3196
[root@docker-0001 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS
deb37734e526        myos:php-fpm        "/usr/sbin/php-fpm -..."   17 seconds ago      Up 15 seconds       
[root@docker-0001 ~]# docker exec -it deb37734e526 /bin/bash
[root@deb37734e526 ~]# ss -ltun
Netid  State      Recv-Q     Send-Q        Local Address:Port         Peer Address:Port              
tcp    LISTEN     0          128                    *:9000                  *:*                  
[root@deb37734e526 ~]#

Step 2: create nginx image

# Compile package
[root@docker-0001 ~]# yum install -y gcc make pcre-devel openssl-devel
[root@docker-0001 ~]# useradd nginx
[root@docker-0001 ~]# tar -zxvf nginx-1.12.2.tar.gz
[root@docker-0001 ~]# cd nginx-1.12.2
[root@docker-0001 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
[root@docker-0001 nginx-1.12.2]# make && make install
[root@docker-0001 nginx-1.12.2]# # Copy docker images / info HTML and info PHP to nginx/html directory
[root@docker-0001 nginx-1.12.2]# cd /usr/local/
[root@docker-0001 local]# tar czf nginx.tar.gz nginx
# Make image
[root@docker-0001 local]# mkdir /root/nginx ;cd /root/nginx
[root@docker-0001 nginx]# cp /usr/local/nginx.tar.gz ./
[root@docker-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@docker-0001 nginx]# docker build -t myos:nginx .
[root@docker-0001 nginx]#
# Authentication service
[root@docker-0001 ~]# docker rm -f $(docker ps -aq)
deb37734e526
[root@docker-0001 ~]# docker run -itd myos:nginx
e440b53a860a93cc2b82ad0367172c344c7207def94c4c438027c60859e94883
[root@docker-0001 ~]# curl http://172.17.0.2/info.html
<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World</font>
  </marquee>
</html>
[root@docker-0001 ~]#

4. Publish container service

4.1 problems

This case exercise tests the port binding and host volume mapping services of docker to achieve the following objectives:

  1. Publish services through mapping ports
  2. Create / var/webroot, / var/webconf
  3. Map the configuration file into the container and publish the service externally
  4. Share network namespace and configure nginx + php container service

4.2 steps

To implement this case, you need to follow the following steps.

Step 1: publish the service through the mapping port (you can operate in docker-0001 or docker-0002)

We use the - p parameter to bind the container port to the host port,

Only one container service can be bound to a host port.

For example, change the host into apache

# Turn docker-0001 into apache service
[root@docker-0001 ~]# docker run -itd -p 80:80 myos:httpd
# To turn docker-0001 into nginx service, you must first stop apache
[root@docker-0001 ~]# docker stop $(docker ps -q)
[root@docker-0001 ~]# docker run -itd -p 80:80 myos:nginx

Step 2: container shared volume

Docker container is not suitable for saving any data. Data files and configuration files change frequently. It is very difficult to modify data in multiple containers. There are data sharing and synchronization requirements between multiple containers. Important data is inconvenient to manage and easy to lose in containers. To solve these problems, please use the host volume mapping function.

Docker can map host files or directories to containers:

  • The target object is created automatically if it does not exist
  • If the target object exists, it will be directly overwritten
  • Multiple containers can map the same target object to achieve the purpose of data sharing
  • When starting the container, use the - v mapping parameter (there can be multiple)

The syntax format is as follows:

docker run -itd -v host object: in container object myos:latest

apache uses the configuration files and web home directories in the host

[root@docker-0001 ~]# mkdir /var/webconf
[root@docker-0001 ~]# cp /usr/local/nginx/conf/nginx.conf /var/webconf/
[root@docker-0001 ~]# vim /var/webconf/nginx.conf
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi.conf;
        }
[root@docker-0001 ~]# docker run -itd -p 80:80 --name nginx \
      -v /var/webconf/nginx.conf:/usr/local/nginx/conf/nginx.conf myos:nginx
# View validation
[root@docker-0001 ~]# docker exec -it nginx /bin/bash
[root@e440b53a860a html]# cat /usr/local/nginx/conf/nginx.conf
[root@e440b53a860a html]# # Check whether the php related configuration is mapped into the container

5. Microservice case

5.1 problems

In this case, it is required to implement nginx + PHP FPM service as a self image. The main contents are as follows:

  1. Start the container and test

5.2 steps

To implement this case, you need to follow the following steps.

The working principle of FastCGI is shown in figure-1.

Figure-1

Step 1: run the container (in docker-0001)

Prepare all required web page files on the real machine in advance, and then map the real machine files to nginx and PHP FPM containers through shared volumes to realize data sharing.

1) Prepare web files in advance on the real machine

All web files have been shared to the cloud disk in advance and are in the kubernetes / docker images / directory in the fourth stage.

[root@docker-0001 ~]# mkdir -p /var/{webroot,webconf}
[root@docker-0001 ~]# cd kubernetes/docker-images
[root@docker-0001 ~]# cp info.php info.html /var/webroot/
[root@docker-0001 ~]# cp /usr/local/nginx/conf/nginx.conf /var/webconf/
[root@docker-0001 ~]# vim /var/webconf/nginx.conf
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi.conf;
        }
# Start the front-end nginx service and map the shared directory and configuration file
[root@docker-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
# Start the back-end php service and map the shared directory
[root@docker-0001 ~]# docker run -itd --network=container:nginx \
      -v /var/webroot:/usr/local/nginx/html myos:php-fpm
# Authentication service
[root@docker-0001 ~]# curl http://docker-0001/info.html
<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World</font>
  </marquee>
</html>
[root@docker-0001 ~]# curl http://docker-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

6. Build a private image warehouse

6.1 problems

In this case, it is required to build a private image warehouse. The specific requirements are as follows:

  1. Build a private image warehouse on 192.168.1.100
  2. All node nodes are configured with private warehouse addresses

6.2 scheme

To complete the follow-up courses, we need to prepare the virtual machines for the experiment in advance. The list of experimental virtual machines is shown in TABLE-1.

The hostname and IP of all hosts must be the same as the list!!!

Otherwise, all subsequent tests cannot be completed successfully!!!

TABLE-1

6.3 steps

To implement this case, you need to follow the following steps.

The Docker image reference topology is shown in figure-2.

Figure-2

Step 1: build a private warehouse server (operate on 192.168.1.100 server)

1) Install the software and start the service

[root@localhost ~]# yum install docker-distribution
[root@localhost ~]# systemctl start docker-distribution
[root@localhost ~]# systemctl enable docker-distribution

2) View configuration file (no modification required)

[root@localhost ~]# cat /etc/docker-distribution/registry/config.yml
 The path defined in the configuration file to store the image is/var/lib/registry
 The default port number in the configuration file is 5000

3) Using curl test

[root@localhost ~]# curl http: / / warehouse ip:5000/v2/_catalog

Step 2: build a private warehouse server

Note: operations are required on all node hosts. Take 192.168.1.31 as an example!!!

Modify the docker configuration file. Change the default connected warehouse of the configuration file to the warehouse on the foreign official website. We need to modify it to the 192.168.1.100 server defined by ourselves.

[root@localhost ~]# vim  /etc/docker/daemon.json     # This file is not available by default, and needs to be created
{
    "exec-opts": ["native.cgroupdriver=systemd"],
    "registry-mirrors": ["https://hub-mirror.c.163.com"],
    "insecure-registries":["192.168.1.100:5000", "registry:5000"]
}
# Server IP and port of the root private warehouse after secure registers
[root@localhost ~]# docker rm -f $(docker ps -aq)
[root@localhost ~]# systemctl restart docker
# Note: stop all containers before restarting the docker service

7. Upload image to private warehouse

7.1 problems

This case requires uploading the image to the private warehouse. The specific requirements are as follows:

  1. Build a private image warehouse on 192.168.1.100
  2. Upload the image from docker-0001 to the warehouse host
  3. Download the image and start the container using the remote warehouse on docker-0002

7.2 steps

To implement this case, you need to follow the following steps.

Step 1: upload the image (operate on docker-0001 host)

1) Modify the docker configuration file and specify 192.168.1.100 as the private warehouse server

[root@docker-0001 ~]# vim  /etc/docker/daemon.json     # This file is not available by default, and needs to be created
{
    "exec-opts": ["native.cgroupdriver=systemd"],
    "registry-mirrors": ["https://hub-mirror.c.163.com"],
    "insecure-registries":["192.168.1.100:5000", "registry:5000"]
}
[root@docker-0001 ~]# docker rm -f $(docker ps -aq)
[root@docker-0001 ~]# systemctl restart docker
# Note: stop all containers before restarting the docker service

2) Upload image

[root@docker-0001 ~]# docker tag \
docker.io/busybox:latest  192.168.1.100:5000/busybox:latest
# Modify the label of the mirror
[root@ docker-0001 ~]# docker images
# View the effect of mirror label modification
[root@docker-0001 ~]# docker push 192.168.1.100:5000/busybox:latest
# Upload the image to the 192.168.1.100 server (the private warehouse server has been built earlier)
The push refers to a repository [192.168.1.100:5000/busybox]
a6d503001157: Pushed 
latest: digest: sha256:43d5f7 ... ... ccd7a7cec79464 size: 527

3) Use curl to view the image just uploaded

[root@docker-0001 ~]# curl http://192.168.1.100:5000/v2/_catalog
{"repositories":["busybox","myos"]}
[root@docker-0001 ~]# curl http://192.168.1.100:5000/v2/myos/tags/list
{"name":"myos","tags":["httpd","latest","nginx","php-fpm"]}

Step 2: download the image (operate on the docker-0002 host)

1) Modify the docker configuration file and specify 192.168.1.100 as the private warehouse server

[root@docker-0002 ~]# vim  /etc/docker/daemon.json     # This file is not available by default, and needs to be created
{
    "exec-opts": ["native.cgroupdriver=systemd"],
    "registry-mirrors": ["https://hub-mirror.c.163.com"],
    "insecure-registries":["192.168.1.100:5000", "registry:5000"]
}
[root@docker-0002 ~]# docker rm -f $(docker ps -aq)
[root@docker-0002 ~]# systemctl restart docker
# Note: stop all containers before restarting the docker service

2) Download Image from private warehouse

Start the container on a machine that does not have any mirrors

Syntax format:

docker run -it warehouse IP:5000 / image name: image label

[root@docker-0002 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
# View no mirror by default
[root@docker-0002 ~]# docker run -it 192.168.1.100:5000/myos:latest
Unable to find image '192.168.1.100:5000/myos:latest' locally
Trying to pull repository 192.168.1.100:5000/myos ... 
latest: Pulling from 192.168.1.100:5000/myos
b1300879af4c: Pull complete 
[root@09845adc59fb /]# 

In case of infringement, please contact the author to delete

Keywords: Operation & Maintenance CentOS Docker Container cloud computing

Added by bellaso on Tue, 04 Jan 2022 16:01:47 +0200