search
HomeOperation and MaintenanceDockerFocus 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:

  • High availability: Docker Service can easily To achieve high availability of containers, even if a node fails, new container instances can be automatically started on other nodes through the automatic balancing function of the container.
  • Load balancing: Using Docker Service can easily achieve load balancing, so that the user's application is always in the best state without having to pay attention to load balancing issues.
  • Automatic expansion: Docker Service supports automatic expansion. Users only need to adjust the number of containers to achieve application expansion.

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.

  1. Create directory structure

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.

  1. Writing Dockerfile

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.

  1. Writing the docker-compose.yml file

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.

  1. Deploy the service

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
  1. Test service

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Docker on Linux: Containerization for Linux SystemsDocker on Linux: Containerization for Linux SystemsApr 22, 2025 am 12:03 AM

Docker is important on Linux because Linux is its native platform that provides rich tools and community support. 1. Install Docker: Use sudoapt-getupdate and sudoapt-getinstalldocker-cedocker-ce-clicotainerd.io. 2. Create and manage containers: Use dockerrun commands, such as dockerrun-d--namemynginx-p80:80nginx. 3. Write Dockerfile: Optimize the image size and use multi-stage construction. 4. Optimization and debugging: Use dockerlogs and dockerex

Docker: The Containerization Tool, Kubernetes: The OrchestratorDocker: The Containerization Tool, Kubernetes: The OrchestratorApr 21, 2025 am 12:01 AM

Docker is a containerization tool, and Kubernetes is a container orchestration tool. 1. Docker packages applications and their dependencies into containers that can run in any Docker-enabled environment. 2. Kubernetes manages these containers, implementing automated deployment, scaling and management, and making applications run efficiently.

Docker's Purpose: Simplifying Application DeploymentDocker's Purpose: Simplifying Application DeploymentApr 20, 2025 am 12:09 AM

The purpose of Docker is to simplify application deployment and ensure that applications run consistently in different environments through containerization technology. 1) Docker solves the environmental differences problem by packaging applications and dependencies into containers. 2) Create images using Dockerfile to ensure that the application runs consistently anywhere. 3) Docker's working principle is based on images and containers, and uses the namespace and control groups of the Linux kernel to achieve isolation and resource management. 4) The basic usage includes pulling and running images from DockerHub, and the advanced usage involves managing multi-container applications using DockerCompose. 5) Common errors such as image building failure and container failure to start, you can debug through logs and network configuration. 6) Performance optimization construction

Linux and Docker: Docker on Different Linux DistributionsLinux and Docker: Docker on Different Linux DistributionsApr 19, 2025 am 12:10 AM

The methods of installing and using Docker on Ubuntu, CentOS, and Debian are different. 1) Ubuntu: Use the apt package manager, the command is sudoapt-getupdate&&sudoapt-getinstalldocker.io. 2) CentOS: Use the yum package manager and you need to add the Docker repository. The command is sudoyumininstall-yyum-utils&&sudoyum-config-manager--add-repohttps://download.docker.com/lin

Mastering Docker: A Guide for Linux UsersMastering Docker: A Guide for Linux UsersApr 18, 2025 am 12:08 AM

Using Docker on Linux can improve development efficiency and simplify application deployment. 1) Pull Ubuntu image: dockerpullubuntu. 2) Run Ubuntu container: dockerrun-itubuntu/bin/bash. 3) Create Dockerfile containing nginx: FROMubuntu;RUNapt-getupdate&&apt-getinstall-ynginx;EXPOSE80. 4) Build the image: dockerbuild-tmy-nginx. 5) Run container: dockerrun-d-p8080:80

Docker on Linux: Applications and Use CasesDocker on Linux: Applications and Use CasesApr 17, 2025 am 12:10 AM

Docker simplifies application deployment and management on Linux. 1) Docker is a containerized platform that packages applications and their dependencies into lightweight and portable containers. 2) On Linux, Docker uses cgroups and namespaces to implement container isolation and resource management. 3) Basic usages include pulling images and running containers. Advanced usages such as DockerCompose can define multi-container applications. 4) Debug commonly used dockerlogs and dockerexec commands. 5) Performance optimization can reduce the image size through multi-stage construction, and keeping the Dockerfile simple is the best practice.

Docker: Containerizing Applications for Portability and ScalabilityDocker: Containerizing Applications for Portability and ScalabilityApr 16, 2025 am 12:09 AM

Docker is a Linux container technology-based tool used to package, distribute and run applications to improve application portability and scalability. 1) Dockerbuild and dockerrun commands can be used to build and run Docker containers. 2) DockerCompose is used to define and run multi-container Docker applications to simplify microservice management. 3) Using multi-stage construction can optimize the image size and improve the application startup speed. 4) Viewing container logs is an effective way to debug container problems.

How to start containers by dockerHow to start containers by dockerApr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software