preface

Docker application and environment can be very convenient in the form of a container to start, but when the application depends on the service is more, or have a large system split service a lot, if still start the container one by one according to the image, that is a bit tiring, that there are a lot of friend will say: get a script will be done. Docker Compose provides a tool called Docker Compose, which launches related services with one click.

For example: For example, to develop a Web project, you need to have a database, Redis, MongoDB, configuration center, etc. If you container it, you can have two options. The first option is to build all the service dependencies and applications as a mirror, and then run in a container. That is, the container contains Web applications, databases, Redis, MongoDB, configuration center, etc. Another way is to start each service separately as a container service, relatively independent, generally can start a container, and then connect through the network on the line; Docker Compose is composed for batch operation. Docker Compose is composed for batch operation. Docker Compose is composed for batch operation.

The body of the

1. An overview of the

Docker Compose is a Docker application tool for defining and running multiple container services. Use the YAML file to configure the application services, and then run the Docker Compose command to start all container services in one click.

2. Install

Docker Compose tool is not included in the default installation environment of Docker, so it needs to be installed separately. Docker Compose tool with Docker is meaningful, so Docker installation before Docker Compose installation. The following demonstration platform for Linux, other platforms, please refer to the document: docs.docker.com/compose/ins…

2.1 Downloading Files

Docker Compose is an executable file for Docker Compose.

Docker Compose file This address download slow sudo curl - L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname - s) - $(uname -m)" -o /usr/local/bin/docker-compose "Https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s) - $(uname -m)" - o /usr/local/bin/docker-composeCopy the code

The diagram below:

2.2 Granting Execution Permission

The downloaded files do not have the execution permission by default. Therefore, you need to grant the execution permission to the downloaded files. Run the following command:

sudo chmod +x /usr/local/bin/docker-compose
Copy the code

The permissions are assigned as follows:

Docker-compose is now installed.

2.3 uninstall

If you need to uninstall it, simply delete it. Run the following command:

sudo rm /usr/local/bin/docker-compose
Copy the code

Use 3.

Docker Compose needs to be used with YAML files, a human-friendly data serialization language for all programming languages with the suffix.yML.

So before the actual operation, you need to have a general understanding of YAML syntax, do not panic, syntax and Json ideas are very similar, have a general understanding, the subsequent use of documentation on the line.

3.1 A brief introduction to grammar

The content of a YAML file is represented by indented Spaces. Common data types are as follows:

  • Object: a collection of key-value pairs;

    {"testKey":"testValue"} # yaml nested objects TestKey: {testKey1: testValue1 testKey2: testValue2} # Json syntax {"testKey":{"testKey1":"testValue1","testKey2":"testValue2"}}Copy the code
  • 1. A group of data arranged in order; It’s prefixed with -.

    ["value1","value2","value3"] # yamL array inline syntax testKey:[value1,value2] # {Json grammar "testKey" : [' value1 ', 'value2']}Copy the code
  • Scalar: non-divisible values, including strings, integers, floating-point numbers, dates, booleans, etc.

    # yaml
    testKey:666
    # Json
    {testKey:666}
    # yaml
    isbool:true
    # Json
    {isbool:true}
    Copy the code

The basic syntax conventions are as follows:

  • Case sensitivity
  • Use space indentation to indicate hierarchy
  • Indent does not allow tabs, only Spaces
  • The number of Spaces indented doesn’t matter, as long as elements of the same rank are left aligned
  • ‘#’ indicates a comment

If you have a general understanding of the above, the files used for Docker Compose are basically enough. If you need to improve, you can check the corresponding syntax. Portal:

yaml.org/spec/1.2.2/

www.runoob.com/w3cnote/yam…

The commands configured in the YAML file content and the Dockerfile commands correspond almost one to one, as I’ll explain shortly.

3.2 Actual operation manual file

Again, take a WebApi as an example that relies on the Redis service.

  • Create projects and write examples

    Here is a Redis cache package, after the constructor injection can be used directly; An API interface, TestCache, was written.

    You need to inject services into the Startup file and specify the Redis connection address as follows:

    Run to test the effect, as follows:

    There are values in Redis, too. Note that the type stored in Redis is Hash.

  • Write a Dockerfile file

    Create a Dockerfile in the project root directory with the following contents:

    I won’t go into details about what’s in the Dockerfile, but there was a previous post about it (click here). The purpose of the Dockerfile here is to build our WebApi project as a mirror, which has nothing to do with Redis, but instead of executing a command, we build it in conjunction with the Compose file.

    Note: Remember to set Dockerfile to always copy by right-clicking -> Properties -> to ensure that the compiled file has the latest file

  • Compose the Compose file

    Create docker-comemess. yml in the project root directory as follows:

    Once you have this project, you can start it with one click. Here we need to change our original code slightly, as follows:

    Note: Remember to set docker-comemess. yml to always copy by right-clicking -> Properties -> to keep the compiled file up-to-date.

3.3 Experience one-click startup
  • Publish the project first and copy it to the corresponding server as follows:

    Here is my Ali cloud server, copy the file is as follows:

  • A key to start

    Execute the following command in the docker-comemess. yml directory: docker-comemess. yml

    docker-compose up
    Copy the code

    Here are the steps to perform the docker-compose up internal execution:

    We first build our application, then pull the dependent Redis service, start it, and finally start our application. (Execution order and dependencies are related); Once started, it can be accessed from the ports mapped in docker-comemage. yml as follows:

  • Take a look at the startup container name

    Docker ps -n 2 Docker inspect composetest_myredis_1: docker inspect composetest_myredis_1: docker inspect composetest_myredis_1:

    It can also be seen that the container corresponding to the API service also uses the network composetest_default. This network is a bridge mode, which can be seen through the Docker network LS, as follows:

  • Docker compose common command

    Docker-compose build: Build or rebuild the service

    Docker-compose up: docker-compose up: build and start the container, plus -d for background operation.

    Docker-compose ps: Lists all containers that are running through compose

    Docker-compose logs: Prints related log information

    Docker-compose stop/start/restartd: can specify service stop, start, and restart

    The docker-compose command is essentially the same as the docker command.

  • Common properties of docker-comemess. yml file content

    Version: specifies the version of the docker-comemage. yml file, usually version 3.

    Services: Define multiple container collections, write as many as there are;

    Build: Build an image, just like docker build.

    Environment: Configure environment variables, like the ENV keyword in Dockerfile.

    Environment: RACK_ENV: development SHOW: 'true'Copy the code

    Expose: Exposes the port, as does the Expose keyword in Dockerfile;

    expose:
      - "80"
      - "9999"
    Copy the code

    Ports: Configures port mapping, which works like docker run -p

    ports:
     - "8080:80"
     - "6379:6379"
    Copy the code

    Volumes: Specifies the path to mount volumes. This is the same as the VOLUME keyword in Dockerifle

    volumes:
      - /var/lib/mysql
      - /opt/data:/var/lib/mysql
    Copy the code

    Command: overrides the default command executed after the container is started, just like the CMD command in Dockerfile.

    command: bundle exec thin -p 3000
    Copy the code

    Image: Specifies the image to be used, which will be pulled during construction.

    Image: redisCopy the code

Listed above are some of the more commonly used, specific can consult website: docs.docker.com/compose/com…

The code address is gitee.com/CodeZoe/mic…

conclusion

Docker Compose: Docker Compose: Docker Compose: Docker Compose: Docker Compose: Docker Compose: Docker Compose: Docker Compose: Docker Compose: Docker Compose

Pay attention to Code Variety Circle and learn with me;