Rate This Document
Findability
Accuracy
Completeness
Readability

Creating an Nginx Image

Create a Docker image that is based on Nginx for deploying and running the Nginx service in the Docker container.

  1. Start Docker.
    dockerd &
    ps -ef | grep docker
  2. Search for and download the image source.
    • CentOS:
      Select the version whose OFFICIAL is OK. This version is the official CentOS image.
      1
      2
      3
      docker search centos
      docker pull centos:7
      docker images
      
    • openEuler 20.03:
      1
      2
      3
      docker search openeuler 
      docker pull openeuler20/origin-pod:v3.11
      docker images
      

    In the actual environment, if the Internet is not available unless a proxy is configured, configure the environment variables of the proxy server and then configure Docker.

    To configure the environment variables of the proxy server, perform the following operations:
    1. Create a directory.
      1
      mkdir /etc/systemd/system/docker.service.d/
      
    2. Create and edit the http-proxy.conf file.
      1
      vim /etc/systemd/system/docker.service.d/http-proxy.conf
      
    3. Press i to enter the insert mode and add the following content to the file. Replace the following parameters with the actual ones: youraccount indicates the user name and password indicates the password.
      IP Address indicates the proxy IP address and PORT indicates the proxy port.
      [Service]
      Environment="http_proxy=http://youraccount:password@IP Address:PORT"
    4. Press Esc, type :wq!, and press Enter to save the file and exit.
    5. Restart the server for the settings to take effect.

    If the Internet is always not available, download the image to a server that can access the Internet and back up the image to the target server for deployment. The procedure is as follows:

    1. Visit the mirror site: https://mirrors.tools.huawei.com/openeuler/openEuler-22.03-LTS-SP1/docker_img/

    2. Download the Docker image based on the server architecture. The Docker image for the AArch64 architecture is used as an example.

    3. Upload the downloaded Docker image to the /home directory on the server.
    4. Load the image in Docker.
      docker load -i /home/openEuler-docker.aarch64.tar.xz

      or

      docker load < /home/openEuler-docker.aarch64.tar.xz

    5. Check the loaded image.
      docker images

  3. Create an image using the dockerfile.
    1. Create a dockerfile directory.
      1
      mkdir /home/dockerfile
      
    2. Copy the compiled and configured Nginx and its dependencies to the dockerfile directory.
      • CentOS:
        1
        2
        3
        4
        5
        6
        7
        \cp -r /usr/local/nginx /home/dockerfile
        \cp -r /usr/lib64/perl5 /home/dockerfile
        \cp -r /usr/local/lib64/perl5 /home/dockerfile/perl5_1
        \cp -r /usr/share/perl5 /home/dockerfile/perl5_2
        \cp  -r  /usr/sbin/ifconfig  /home/dockerfile
        \cp  -r  /usr/sbin/ethtool  /home/dockerfile
        \cp  -r  /usr/bin/netstat   /home/dockerfile
        
      • openEuler:
        1
        2
        3
        4
        5
        6
        7
        8
        \cp -r /usr/local/nginx /home/dockerfile
        \cp -r /usr/lib64/perl5 /home/dockerfile
        \cp -r /usr/lib64/perl5 /usr/local/lib64/
        \cp -r /usr/local/lib64/perl5 /home/dockerfile/perl5_1
        \cp -r /usr/share/perl5 /home/dockerfile/perl5_2
        \cp  -r  /usr/sbin/ifconfig  /home/dockerfile
        \cp  -r  /usr/sbin/ethtool  /home/dockerfile
        \cp  -r  /usr/bin/netstat   /home/dockerfile
        
    3. Copy the sysctl.conf file on the physical machine to the dockerfile directory.
      1
      \cp /etc/sysctl.conf /home/dockerfile
      
    4. Create an Nginx startup script.
      1. Create a start.sh script.
        1
        vim /home/dockerfile/start.sh
        
      2. Add the following content to the file:
        sysctl -p
        /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
      3. Press Esc, type :wq!, and press Enter to save the file and exit.
    5. Create a dockerfile file.
      1. Create a file.
        1
        vi /home/dockerfile/dockerfile
        
      2. Add the following content to the file:
        • CentOS:
          FROM centos:7
          USER root
          WORKDIR /home/
          COPY sysctl.conf /etc/
          COPY nginx /usr/local/nginx
          COPY perl5 /usr/lib64/perl5
          COPY perl5_1 /usr/local/lib64/perl5
          COPY perl5_2 /usr/share/perl5
          COPY ifconfig /usr/sbin/
          COPY ethtool /usr/sbin/
          COPY netstat /usr/sbin/
          COPY start.sh /home/
          RUN echo 'ulimit -n 102400' >> /etc/profile
          RUN chmod +x /home/start.sh
          CMD /home/start.sh && /usr/bin/top
        • openEuler:
          FROM openeuler20/origin-pod:v3.11
          USER root
          WORKDIR /home/
          COPY sysctl.conf /etc/
          COPY nginx /usr/local/nginx
          COPY perl5 /usr/lib64/perl5
          COPY perl5_1 /usr/local/lib64/perl5
          COPY perl5_2 /usr/share/perl5
          COPY ifconfig /usr/sbin/
          COPY ethtool /usr/sbin/
          COPY netstat /usr/sbin/
          COPY start.sh /home/
          RUN echo 'ulimit -n 102400' >> /etc/profile
          RUN chmod +x /home/start.sh
          CMD /home/start.sh && /usr/bin/top
      3. Press Esc, type :wq!, and press Enter to save the file and exit.
        For details about the command description, see Table 1.

        The reason for running the /usr/bin/top command is as follows: The Docker containers are running in the background. A suspended command (such as top and tail) must exist. Otherwise, the containers automatically exit.

        Table 1 Command description

        Command

        Description

        FROM

        Creates an image based on the downloaded CentOS image.

        USER

        Runs the image as the root user.

        WORDIR

        The working directory is /home.

        COPY

        Copies the files in the dockerfile directory of the physical machine to the related directories in the container.

        RUN

        Runs related commands.

        CMD

        Runs the related scripts when the container is started.

    6. Create an image.
      • CentOS:
        1
        2
        3
        cd /home/dockerfile
        docker build -t arm/centos7/nginx:http .
        docker images | grep arm/centos7/nginx
        
      • openEuler:
        1
        2
        3
        cd /home/dockerfile
        docker build -t arm/openeuler20/nginx:http .
        docker images | grep arm/openeuler20/nginx
        
      • arm/centos7/nginx indicates the image name, and http indicates the tag.
      • arm/openeuler20/nginx indicates the image name and http indicates the tag.
      • "." indicates that the image is created using the dockerfile file in the current directory.