Docker containerized deployment practice Docker Compose

In the last issue, we talked about the practice of containerized deployment in Docker-Getting Started talked about the advantages of Docker as a cloud computing and its installation common commands. Today I will introduce a simple orchestration tool docker compose.

Before I talk about it, I continue to use a command mentioned yesterday:

docker run -d -p 5000:5000 --name echo ubuntu:16.04 /bin/bash -c "while true; do echo 1; sleep 1; done"
-d parameter background operation
-p port number mapping
--name custom The container name, the container name is unique, if you want to use it, you need to delete the previous one.

After starting up, we can docker logs -f echocheck the printing of the number 1 in the container.

We can see from the above that the entire operation is relatively cumbersome, not only need to enter various parameters, but also have version numbers, tags, and so on. If we are just a container, we can use a shell to define a shortcut.

But we often have multiple containers, and multiple containers are related to each other. For example, for a web project, we may have a db container, a cache container, a container for offline tasks, a log container, etc. At this time, if we use manual input for control, it is not only very troublesome but also error-prone.

At this time, customizing the command through the shell is a bit more difficult, and it is impossible to start and restart multiple services.

So Docker officially released an orchestration tool called Compose, which defines a group of related containers through a single docker-compose.yml template file. The simple understanding is to assemble the corresponding containers in this way. Start, stop, delete.

Compose is written in the Python language, and the container is managed through the API provided by Docker, and the operation is very convenient.

Let's look at the file format of a docker-compose.yml:

version: "3"  # 注意版本号
services:  # 一个应用的容器,可以包括多个相同镜像容器实例
  dev:
    image: hub.yourdomain.com/test:1.0 # 镜像地址
    command: python app.py  # 容器执行命令
    container_name: test  # 容器名
    restart: always  
    volumes:
      - ./docker:/data  # 挂载地址
    ports:
      - "9527:9527" #端口
    environment:
      - PYTHONPATH=/data
      - XXX_API_SETTINGS=XXX.config.dev
    network_mode: bridge
    extra_hosts: # 配置额外的host名称
      - "test.yourdomain.com:127.0.0.1"
    external_links: # 链接到外部容器
      - redis:redis 
      - mysql:mysql

Through the above simple configuration, we can handle multiple container links:

Through docker-compose up dev, the entire service can be started , and the port number, environment variable address, etc. can be set, which is very convenient.

Let's talk about the docker-compose installation below. The docker-compose installation method is very simple, just one command.

sudo pip install -U docker-compose

Installed. If your Linux or Mac terminal is using ohmyzsh, you can add it to zshrc plugins +=(docker-compose)

d4958970bd8391f51169645b0bcb353c.webp


I will post the auto-completion command line that I often use myself

plugins=(git ruby autojump osx tmux zsh-syntax-highlighting brew-cask brew colored-man rsync sudo node-docs history-substring-search docker docker-compose fabric redis-cli cp zsh-autosuggestions )

Let me briefly introduce several commonly used commands (not much different from docker). For more, please refer to  docker-compose -h:

docker-compose logs dev # dev is to define the service service
docker-compose up -d dev # Start the service, it will automatically pull the image, perform the associated service
docker-compose restart dev # Restart the service
docker-compose rm dev # Delete the service

Through Compose, we can quickly write the startup configuration file of our own docker project, which is convenient for us to quickly start and restart the service. Students who are not using it can use it. Today's content is here first.

Containerized deployment practice, I plan to share it in a series from basic operations to actual applications. Today is the second docker-compose orchestration. Later I will continue to share more related content, from the introductory use of containerization to deployment practice orchestration technology. Welcome everyone to continue to pay attention.


Guess you like

Origin blog.51cto.com/15009257/2552286