Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy micro services using docker

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use docker to deploy micro-services, which has a certain reference value. Interested friends can refer to it. I hope you can learn a lot after reading this article.

one。 Introduction to Docker

Docker is an open source container engine that helps deliver applications faster. Docker isolates the application from the infrastructure layer and manages the infrastructure as if it were a program. With Docker, applications can be packaged, tested, and deployed faster, and the cycle from writing to deploying running code can be shortened.

The advantages of Docker are as follows:

1. Simplified program

Docker allows developers to package their applications and dependency packages into a portable container and publish them to any popular Linux machine for virtualization. Docker has changed the way it is virtualized, allowing developers to manage their work directly in Docker. Convenience and rapidity is already the biggest advantage of Docker. In the past, tasks that took days or even weeks could be completed in seconds under the Docker container.

two。 Avoid choice phobia

If you have choice phobia, you are a senior patient. Docker helps you pack up your entanglements! For example, Docker image; Docker image contains the running environment and configuration, so Docker can simplify the deployment of multiple application instances. For example, Web applications, background applications, database applications, big data applications such as Hadoop cluster, message queue and so on can be packaged into a mirror deployment.

3. Save money

On the one hand, with the advent of the era of cloud computing, developers do not have to configure high hardware in order to pursue results, and Docker has changed the mindset of high performance and high price. The combination of Docker and cloud makes the cloud space more fully utilized. It not only solves the problem of hardware management, but also changes the way of virtualization.

two。 Docker architecture

Docker daemon (Docker daemon)

Docker daemon is a background process running on a host (DOCKER-HOST). You can communicate with it through the Docker client.

Client (Docker client)

The Docker client is the user interface of Docker, which accepts user commands and configuration identities, and communicates with Docker daemon. In the figure, docker build and so on are related commands of Docker.

Images (Docker Mirror)

The Docker image is a read-only template that contains instructions for creating a Docker container. It is similar to the system installation CD, where you can install the system using the system installation CD. Similarly, you can use the Docker image to run the programs in the Docker image.

Container (container)

The container is a runnable instance of the mirror. The relationship between mirrors and containers is somewhat similar to that between classes and objects in object-oriented. You can use Docker API or CLI commands to start, stop, move, and delete containers.

Registry

Docker Registry is a service that centrally stores and distributes images. After the Docker image is built, it can be run on the current host. But if you want to run this image on another machine, you need to copy it manually. At this point, you can use Docker Registry to avoid manual replication of the mirror.

A Docker Registry can contain multiple Docker repositories, and each repository can contain multiple image tags, each corresponding to an Docker image. This is similar to the Maven repository. If Docker Registry is compared to the Maven repository, then the Docker repository can be understood as the path of a jar package, while the mirror tag can be understood as the version number of the jar package.

three。 Docker installation

Docker is an open source commercial product with two versions: Community Edition (CE) and Enterprise Edition (EE). The enterprise version includes some paid services that individual developers generally don't need. The following introductions are for the community version.

Please refer to the official documentation for the installation of Docker CE. The installation methods for different operating systems are listed below

Mac

Indows

Ubuntu

Debian

CentOS

Fedora

Other Linux distributions

Let's take CentOS as an example:

1. Docker requires that the kernel version of the CentOS system is higher than 3.10. Check the prerequisites on this page to verify whether your CentOS version supports Docker.

Check your current kernel version with the uname-r command

# uname-r

2. Log in to Centos with root permission. Make sure the yum package is up to date.

# yum-y update

3. Uninstall the old version (if the old version is installed)

# yum remove docker docker-common docker-selinux docker-engine

4. Install the required software packages. Yum-util provides yum-config-manager functions, and the other two are dependent on devicemapper drivers.

# yum install-y yum-utils device-mapper-persistent-data lvm2

5. Set the yum source

# yum-config-manager-- add-repo https://download.docker.com/linux/centos/docker-ce.repo

6. You can view all docker versions in all repositories and select a specific version to install

# yum list docker-ce-- showduplicates | sort-r

7. Install docker

# sudo yum install-y docker-ce # since only stable repository is enabled by default in repo, the latest stable version 18.03.1 is installed here

8. Start and join the boot boot

# systemctl start docker# systemctl enable docker

9. Verify whether the installation is successful (both client and service indicate that the docker installation starts successfully)

# docker version

10. Uninstall docker

# yum-y remove docker-engine

four。 Docker common commands

(1). Mirror related commands

1. Search for images

You can use the docker search command to search for images stored in Docker Hub, a place officially provided by docker to store all docker image software, similar to maven's central repository. After executing this command, Docker searches Docker Hub for image repositories that contain the keyword java.

# docker search java

The above list contains five columns with the following meanings:

NAME: name of the image repository.

DESCRIPTION: image repository description.

STARS: the number of collections in the image repository, indicating the popularity of the image repository, similar to GitHub's stars0

OFFICAL: indicates whether it is an official repository. The images marked [0K] in this column are created and maintained by the official project team of each software.

AUTOMATED: indicates whether it is an automatically built image repository.

Note: finding or downloading images using docker may time out, so we need to configure a domestic image accelerator for docker

We can log in to Aliyun (https://cr.console.aliyun.com/#/accelerator) with the help of Aliyun's mirror accelerator.

You can see the image acceleration address as shown below:

# cd / etc/docker

Check for daemon.json. This is the default configuration file for docker.

If it is not new, if so, modify it.

# vim daemon.json {"registry-mirrors": ["https://m9r2r2uj.mirror.aliyuncs.com"]}"

Save exit and restart the docker service

# service docker restart

2. Download the image

Use the command docker pull command to download the image from the Docker Registry. After executing this command, Docker downloads the latest version of the Java image from the java repository in Docker Hub. If you want to download the specified version, add a colon after the java to specify the version, for example: docker pull java:8

# docker pull java:8

3. List the images

Use the docker images command to list the downloaded images

# docker images

The above list means as follows

REPOSITORY: the name of the repository to which the image belongs.

TAG: image tag. The default is latest, which means up to date.

IMAGE ID: image ID, which indicates the unique identity of the image.

CREATED: time when the image was created.

SIZE: image size.

4. Delete the local image

Use the docker rmi command to delete the specified image

# docker rmi java

(2) Container related commands

1. Create and start the container

Use the following docker run command to create and start a container, which is the most commonly used command, which has many options, some of which are listed below.

# docker run-d-p 91:80 nginx

This starts a Nginx container. In this example, two parameters are added to docker run, which mean as follows:

-d running in the background

-p Host Port: container Port # Open Container Port to Host Port

When you visit the http://Docker host IP:91/, you will see the main interface of nginx as follows:

It is important to note that when you use the docker run command to create a container, you will first check whether the specified image exists locally. If no image with that name exists locally, Docker automatically downloads the image from Docker Hub and launches a Docker container.

The command also has a network configuration parameter, as shown below

-- net option: specifies the network mode, which has the following optional parameters:

-- net=bridge: default option, which means to connect to the default bridge.

-- net=host: the container uses the host network.

-- net=container:NAME-or-ID: tell Docker to let the newly created container use the network configuration of the existing container.

-- net=none: the network of the container is not configured. You can customize the network configuration.

2. List containers

Use the docker ps command to list running containers

# docker ps

To list all containers, including those that have been stopped, use the-a parameter. The list contains seven columns, meaning as follows

CONTAINER_ID: represents the container ID.

IMAGE: indicates the image name.

COMMAND: represents the command that runs when the container is started.

CREATED: indicates the creation time of the container.

STATUS: indicates the status of the container running. UP indicates that it is running, and Exited indicates that it has stopped.

PORTS: indicates the external port number of the container.

NAMES: represents the container name. The name is automatically generated by Docker by default, or you can specify it by yourself using the-name option of the docker run command.

3. Stop the container

Use the docker stop command to stop the container

# docker stop f0b1c8ab3633

Where f0b1c8ab3633 is the container ID. Of course, you can also use the docker stop container name to stop specifying the container.

4. Force to stop the container

You can use the docker kill command to send a SIGKILL signal to force the container to stop

# docker kill f0b1c8ab3633

5. Start the stopped container

Using the docker run command, you can create a new container and start it. For stopped containers, use the docker start command to start

# docker start f0b1c8ab3633

6. View all the information of the container

Use the command docker inspect

# docker inspect f0b1c8ab3633

7. View the container log

Use the command docker container logs

# docker container logs f0b1c8ab3633

8. Check the process in the container

Use the command docker top

# docker top f0b1c8ab3633

9. Enter the container

Use the docker container exec-it / bin/bash command to enter a running docker container. If the docker run command runs the container without using the-it parameter, use this command to enter the container. Once you have entered the container, you can execute the command in the container's Shell

# docker container exec-it f0b1c8ab3633 / bin/bash

10. Delete the container

Use the docker rm command to delete the specified container

# docker rm f0b1c8ab3633

This command can only delete stopped containers. To delete running containers, use the-f parameter

(3) Build your own docker image

Use Dockerfile to build your own Docker image

Dockerfile is a text file that contains several instructions that describe the details of building an image

Let's first write the simplest Dockerfile. Take the Nginx image downloaded earlier as an example, to write a Dockerfile to modify the home page of the Nginx image.

1. Create a new folder / app, and create a new file named Dockerfile under the app directory, and add the following content to it:

FROM nginx # pull the docker image of ngxin from the local image repository RUN echo 'This is QingFeng Nginximages created automatically' > / usr/share/nginx/html/index.html # modify the home page of the docker image of ngxin

The Dockerfile is very simple, in which FORM and RUN are Dockerfile instructions. The FROM directive is used to specify the underlying image, and the RUN directive is used to execute the command.

2. Execute the following command in the path where Dockerfile resides to build our own ngxin image. After construction, you can use the docker images command to check whether the image ngxin:tuling has been generated:

# docker build-t nginx:qingfeng.

Where-t specifies the name of the image, and the last dot (.) of the command indicates the path where the Dockerfile file is located.

3. Execute the following command to start a Docker container using this image

# docker run-d-p 92:80 nginx:qingfeng

4. Visit the http://Docker host IP:92/, to see the interface shown below.

Dockerfile file writing also has the following common instructions

Note: the RUN command is executed during the construction phase of the image file, and the results are packaged into the image file; the CMD command is executed after the container starts. In addition, a Dockerfile can contain multiple RUN commands, but only one CMD command.

Note: once the CMD command is specified, the docker container run command cannot be appended (such as the previous / bin/bash), otherwise it will override the CMD command.

(4). Using Dockerfile to build a microservice image

Take the spring boot project ms-eureka-server (source code at the end), which is a spring cloud eureka micro-service project that can be packaged and run as an executable jar package through spring boot's maven plug-in, as shown in the following figure

Construct the executable jar package of the project into a docker image:

1. Upload the jar package to the linux server / app/eureka directory and create a file named Dockerfile in the directory where the jar package is located.

2. Add the following to Dockerfile

# based on which image

From java:8

# copy files to the container

ADD microservice-eureka-server-0.0.1-SNAPSHOT.jar / app.jar

# declare the ports to be exposed

Startup port of EXPOSE 8761 # micro service project

# configure commands to be executed after the container starts

ENTRYPOINT ["java", "- jar", "/ app.jar"]

3. Use the docker build command to build an image

# docker build-t microservice-eureka-server:0.0.1.

# format: docker build-t image name: relative position of label Dockerfile

Here, the label of the image is specified using the-t option. After executing the command, the terminal will output the following

4. Start the image, and add-d to start it in the background.

# docker run-p 8761pur8761 microservice-eureka-server:0.0.1

5. Visit the http://Docker host IP:8761/, to display the home page of the microservice Eureka Server normally.

Thank you for reading this article carefully. I hope the article "how to deploy Micro Services using docker" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report