Docker Compose quick start

Posted by richei on Mon, 03 Jan 2022 19:20:33 +0100

Docker Compose

brief introduction

In the docker we learned earlier, we operate a single container, and Dockerfile build is operated manually.
In the case of microservices, 100 microservices! If you use manual operation one by one, it is also very troublesome.
Therefore, Docker Compose can easily and efficiently manage containers and define and run multiple containers!

Introduction to the official website

Compose is a tool for defining and running multi container Docker applications. With compose, you can use YAML files to configure the services of your application. Then, use a single command to create and start all services from the configuration. To learn more about all the features of compose, see the feature list.
Compose applies to all environments: production, staging, development, testing, and CI workflows. You can learn more about each case in Common Use Cases.
Using Compose is basically a three-step process:

  1. Use the Dockerfile to define your application's environment so that it can be replicated anywhere.
  2. Define the services that make up your application, docker - compose YML so that they can run together in an isolated environment.
  3. Run the docker compose up and Docker compose command commands to start and run your entire application. You can also run docker compose up using the docker compose binary.

Understanding of Docker Compose
Docker Compose is to arrange multiple containers into one file.

Docker Compose installation

Under Linux environment

## 1. Run this command to download the current stable version of Docker Compose:
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
##  You can also install Docker Compose at high speed by executing the following command.
sudo curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

## 2. Apply executable permissions to binary files:
sudo chmod +x /usr/local/bin/docker-compose

##Test installation.
$ docker-compose --version
docker-compose version 1.29.2, build 1110ad01 ah

Common commands and configurations of Docker Compose

  • ps: lists all compose run containers
docker-compose ps
  • logs: view the service log output
docker-compose ps
  • Port: print the public port of the binding
docker-compose port eureka 8761
  • Build: build or rebuild services
docker-compose build
  • start; Start a container that already exists for the specified service
docker-compose start	xiaomingapp
  • stop: the container that stops the running service
docker-compose stop redis
  • rm: delete the container that knows the service
docker-compose rm redis
  • up: build and start containers
docker-compose up
  • kill: stop the container that knows the service
  • run: execute a command on a service
docker-compose run web bash
  • pull: download service image
  • scale: sets the number of service running containers, specified in the form of service=num
docker-compose scale user=3 movie=3

docker-compos.yaml attribute

docker-compose.yaml core

Refer to the official document for specific parameters

Official documents: https://docs.docker.com/compose/compose-file/compose-file-v3/

## It is mainly divided into three layers Version -- "service" - > other configurations/
version: ""		## edition
services:		## service
	Service 1:  		## web
		build: 
		...
		image: 
		networks:
		volumes
		...
		
	Service 2:		## redis
		...
## Other configuration networks / volumes, global rules
volumes:
network:
configs:

case

version: "3.9" ## edition
services:		## service
  web:
    build: .
    depends_on:	  ##Dependence. Execute db redis first
      - db
      - redis
  redis:
    image: redis
  db:
    image: postgres

Learning needs to master the law!

Using docker compose network

When arranging containers with docker compose, a default network will be created and defined. All containers use the network, and the containers can be connected.

xiaoming[root@cn-mbp-c02v221ahv2f java]# docker network list
NETWORK ID     NAME           DRIVER    SCOPE
25463fc507cb   bridge         bridge    local
77b421abcbd9   host           host      local
e5f240fc6078   java_default   bridge    local
b7ade70af5bc   mynet          bridge    local
c2509facb374   none           null      local
3f39b14d72a7   redis          bridge    local

Using docker compose to package and deploy jar s

Write and test java web applications and only use redis for testing

@RestController
public class HelloController {

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("hello/{name}")
    public String hello(@PathVariable("name") String name){
        redisTemplate.opsForValue().set("name",name);
        return (String) redisTemplate.opsForValue().get("name");
    }

Here we use the redis service name for network connectivity

server.port=8080
spring.redis.host=redis
[root@cn-mbp-c02v221ahv2f java]# vi docker-compose.yml 
version: '3.3'
services:
  xiaomingapp:
    image: xiaomingapp
    build: .
    ports:
    - 8080:8080
    depends_on:
      - redis
  redis:
    image: "redis:alpine"
[root@cn-mbp-c02v221ahv2f java]# vi Dockerfile 
FROM java:8
COPY *.jar /app.jar
CMD ["--server.port=8080"]
EXPOSE 8080

[root@cn-mbp-c02v221ahv2f java]# ll
total 26424
-rw-r--r--. 1 root root      168 Aug  3 01:49 docker-compose.yml
-rw-r--r--. 1 root root      108 Aug  3 01:48 Dockerfile
-rw-r--r--. 1 root root 27049938 Aug  3 01:48 hello-0.0.1-SNAPSHOT.jar

Use docker compose up -- build to build

[root@cn-mbp-c02v221ahv2f java]# docker-compose up  --build 
[root@cn-mbp-c02v221ahv2f java]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED       STATUS       PORTS                                       NAMES
f697833af140   xiaomingapp    "java -jar /app.jar ..."   4 hours ago   Up 4 hours   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   java_xiaomingapp_1
b1f576accdd1   redis:alpine   "docker-entrypoint.s..."   4 hours ago   Up 4 hours   6379/tcp                                    java_redis_1


Test successful

[root@cn-mbp-c02v221ahv2f java]# curl localhost:8080/hello/xiaoming
xiaoming

Topics: Docker docker compose