Home >Operation and Maintenance >Docker >Focus on how to use different directories in Docker Service
Docker is a very popular containerization technology that can build, deploy and run applications quickly and efficiently. Docker Service is one of the most commonly used mechanisms for Docker cluster management. Using Docker Service, users can combine multiple Docker containers into one service to achieve high availability, load balancing and other purposes. This article will focus on how to use different directories in Docker Service.
What is Docker Service?
Docker Service is one of the mechanisms for Docker cluster management. It can combine multiple Docker containers into one service. Docker Service implements functions such as high availability, load balancing, and automatic expansion, which can help users optimize services and applications.
Docker Service supports different deployment strategies, such as replica and global. When using the replica policy, users can define the number of containers to launch, and Docker Engine will automatically deploy and manage container instances across the cluster. When using the global strategy, Docker Engine will start a copy of the container and deploy it to each node in the cluster.
Benefits of using Docker Service
Using Docker Service can bring many benefits, here are a few main ones:
How does Docker Service use different directories?
Docker Service uses the directory where the Dockerfile is located as the build context of the container image by default. However, when we need to build different images in different directories, we need to use different build contexts.
The following will introduce how to use different directories to build Docker images and use Docker Service to deploy applications.
First, we create the following directory structure:
. ├── dockerfiles │ ├── web1 │ │ ├── Dockerfile │ │ └── index.html │ └── web2 │ ├── Dockerfile │ └── index.html ├── docker-compose.yml └── services.yml
In the dockerfiles directory, we created web1 and web2 respectively. Table of contents. Each directory contains a Dockerfile file and an index.html file. Among them, the Dockerfile file is used to build the Docker image, and the index.html file is used to test whether the container is running normally.
In the services.yml file, we define a service named web, which is used to deploy two containers, web1 and web2.
In our example, we created a Dockerfile for web1 and web2 respectively. They are as follows:
Dockerfile file in the web1 directory:
FROM nginx COPY index.html /usr/share/nginx/html
Dockerfile file in the web2 directory:
FROM nginx WORKDIR /usr/share/nginx/html COPY index.html .
Here we use different Dockerfile files, and Use a different build context in each file. web1's Dockerfile uses the COPY directive, which copies the index.html file from the specified build context into the container. The web2 Dockerfile uses the WORKDIR instruction and the COPY instruction, which will set the working directory to /usr/share/nginx/html and copy the index.html file from the build context to the container.
We still use the docker-compose.yml file to define our services. The content of the file is as follows:
version: '3.7' services: web1: build: context: ./dockerfiles/web1 image: web1 deploy: replicas: 2 ports: - 8081:80 web2: build: context: ./dockerfiles/web2 image: web2 deploy: replicas: 3 ports: - 8082:80
In the docker-compose.yml file, we define two services: web1 and web2, which use different build contexts respectively. At the same time, we also specify the replicas and ports attributes of each service to control the number of containers and port mapping.
Finally, we use the Docker Stack command to deploy the service:
$ docker stack deploy -c services.yml myapp
After the command execution is completed, we can use the following command to view the service Status:
$ docker stack ps myapp
After the service deployment is completed, we can use the browser to access the following URL to test whether the web1 and web2 services are running normally:
web1 service:
http://localhost:8081
web2 service:
http://localhost:8082
If everything is fine, we will see the index.html file content on each service.
Summary
In this article, we introduced how to use different directories for container building in Docker Service. We created two services with different Dockerfiles and defined the build context of the services in the docker-compose.yml file. Finally, we deployed the service and tested that the service was running properly. This provides a convenient way for users using Docker Service to build container images in different directories.
The above is the detailed content of Focus on how to use different directories in Docker Service. For more information, please follow other related articles on the PHP Chinese website!