I’m participating in nuggets Creators Camp # 4, click here to learn more and learn together!

The advantages of the Docker

1. Simplify the deployment process and improve portability

When we want to put a new machine online, we need to configure the corresponding operating environment, its installation process is quite tedious and long. With Docker, you only need to import or pull images from the repository to install and start with one click, greatly reducing the configuration time and complexity of deployment.

2. Software upgrade and version control

When upgrading a project from 1.0 to 2.0, it is usually safe to stop services first, and then start new services after sorting out the different versions of directories. When Docker is used, it is necessary to start a 2.0 container and then stop the 1.0 container. When a fault occurs and the version needs to be rolled back, it can also directly start the stopped container to temporarily replace it and switch back after the repair of the new version is completed.

3. Isolation

Multiple services are deployed on the same machine, and many times other services cannot run properly because some obsolete projects overwhelm the entire server. The Docker to ensure that each container has its own resources and environment in the container, any changes will not affect the other container, at the same time also can ensure that each application using only resources assigned to it, there will be no different services preemption resources, by its in line with the arrival of the era of cloud computing, let cloud space can be more fully utilized.

How to create a Docker image

Once we have taken an existing container and configured it to the desired environment, we can package it into a new image using the COMMIT command.

You can also create an image from a Dockerfile using the build command.

You can also use tools to create images with mouse action.

1. Docker commit command

CONTAINER [REPOSITORY[:TAG]]

The OPTIONS:

  • -a: Mirror author of the submission
  • -cUse:DockerfileCommand to create a mirror
  • -m: Instructions for submission
  • -pIn:commitPhase, the container pauses

Example: Save container 09AE7EDBF678 as a new image.

2. Run the Docker build command

Grammar: docker build [OPTIONS] PATH | | – URL

The OPTIONS:

  • --build-arg=[]: Sets the variables for creating the image
  • --cpu-sharesSet:CPUA weight
  • --cpu-period: limiting theCPU CFScycle
  • --cpu-quota: limiting theCPU CFSThe quota
  • --cpuset-cpus: Specified for useCPU id
  • --cpuset-mems: Specifies the memory to useid
  • --disable-content-trust: Ignores verification and is enabled by default
  • -f: Specifies what to useDockerfileThe path
  • --force-rm: Deletes an intermediate container during mirroring
  • --isolation: Use container isolation technology
  • --label=[]: Sets the metadata used by the mirror
  • -m: Sets the maximum memory size
  • --memory-swapSet:SwapThe maximum value of is memory+swap."1"Said there is no limitswap
  • --no-cache: The image creation process does not use caching
  • --pull: Attempts to update a new version of the image
  • --quiet, -q: In quiet mode, only mirroring is displayed after successID
  • --rm: Delete the intermediate container after the mirror is successfully configured
  • --shm-sizeSet:/dev/shmThe default value is64M
  • --ulimit:UlimitConfiguration.
  • --tag, -t: The name and label of the mirror, usuallyname:tagornameFormat allows you to set multiple labels for a single image in a single build.
  • --network: the defaultdefault. Set during buildRUNNetwork mode of instruction

Example: Create an image using a Dockerfile in the current directory.

3. IDEA Create a Docker image

  1. Docker configuration: If you want to access Docker through IDEA, you need to enable related ports in Docker.

    • Configuration file location: / usr/lib/systemd/system/docker. Service
    • Add -h TCP ://0.0.0.0:2375 after ExecStart
  2. The IDEA of configuration:

    • Install the Docker plugin in file-settings-plugins-marketplace – Docker
    • Connect to Docker at file-settings-build, Execution, deployment-docker

  1. POM configuration:

    • Pom files introduce the docker-maven-plugin
<! Docker-maven-plugin -->
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.0.0</version>
    <! -- Bind the plug-in to a phase execution -->
    <executions>
        <execution>
            <id>build-image</id>
            <! Docker :build-->
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
    </executions>

    <configuration>
        <! -- Specify the name of the generated image, here is our project name -->
        <imageName>${project.artifactId}</imageName>

        <! This is the latest version of the image -->
        <imageTags>
            <imageTag>${project.version}</imageTag>
        </imageTags>

        <! Jdk1.8 -->
        <baseImage>java</baseImage>
        <! -- Image producer information <maintainer>[email protected]</maintainer> -->
        <! -- Switch to ROOT directory -->
        <workdir>/ROOT</workdir>

        <! View our Java version -->
        <cmd>["java", "-version"]</cmd>

        <! ${project.build.finalName}. Jar is the name of the generated JAR package -->
        <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>

        <! Remote docker API address -->
        <dockerHost>http://IP:2375</dockerHost>

        <! Docker jar package to docker container -->
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <! Target directory --> jar package --> jar package --> jar package --> jar package --> jar package --> jar package --> jar package
                <directory>${project.build.directory}</directory>
                <! Dockerfile --> jar --> jar --> jar --> jar --> jar
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>
Copy the code
  1. packaging

    • Packaging via Maven automatically generates docker images

All three of the above methods can be generatedDockerMirror image, see which is convenient to use which is good.

other

1. Create a Dockerfile file

  • The file name must be Dockerfile
  • Parameters:
    • FROM [image:tag] : the customized image is based on this image. In this example, williamyeh/java8 is used
    • VOLUME [path] : indicates the mount point
    • ADD [fileName] [imageName] : Adds a file from the context directory to the specified path in the container
    • ENTRYPOINT [“param1″,”param2”,……] : Boot parameter
    • More parameters can refer to: www.runoob.com/docker/dock…
  • Upload jar packages and dockerfiles to the same folder at the same time
  • Docker build-t [imageName]
  • Docker run -d -p :[container port] –name [containerName] [imageName]

2. Dockerfile FORM low-level image generation method

  • Docker search centos
  • Docker pull centos/systemd
  • Download JDK:www.oracle.com/java/techno…
  • Create a Dockerfile in the directory where you downloaded the jdK.tar. gz file
# System Mirroring
FROM centos/systemd
# Custom path
ENV MYPATH /usr/local
WORKDIR $MYPATH
# Dependent downloads
RUN yum -y install vim
RUN yum -y install net-tools
RUN yum -y install glibc.i686
RUN mkdir /usr/local/java
# quote
ADD JDKJar.tar.gz /usr/local/java
# Environment variables
ENV JAVA_HOME /usr/local/java/jdk
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
ENV PATH $JAVA_HOME/bin:$PATH
EXPOSE 80
CMD echo $MYPATH
CMD echo "success-------------------OK"
CMD /bin/bash
Copy the code
  • Run: docker build -t [image name: version number]
    • Notice the little dot at the end that represents the current directory

3. Docker Harbor

You can save the image in Harbor as follows:

  • Login: docker login IP :port
  • Name changed: docker tag Image name: version IP :port/ repository name/image name: version
  • Push: Docker push IP :port/ repository name/image name: version number
  • Download: Docker pull IP :port/ repository name/image name: version number

4. Docker image private server configuration

Method 1: daemon.json

  • Location: / etc/docker/daemon. Json
  • Add parameter: “insecure-registries”: [“IP:PORT”]

Method 2: docker.service

  • Location: / usr/lib/systemd/system/docker. Service
  • Add parameters: Add the -insecure -registry=IP:PORT parameter to ExecStart

Only one of the preceding configuration modes can be selected

Restart the docker:

  • systemctl daemon-reload
  • systemctl restart docker

To view:

  • docker login IP:PORT

After registry is configured, we can use the login command to login to the private server for viewing.