Docker Compose common commands

Common commands

1.1 restart, start, stop-- Start and stop services

The command must be executed in the directory where the docker-compose.yml file is located.

# 前台启动, 启动项目中的所有服务。
$. docker-compose up

# 后台启动, 启动所有服务并在后台运行。 
$. docker-compose up -d

# 停止所有服务。
$. docker-compose stop 

restart
docker-compose restart重启服务容器。
docker-compose restart # 重启工程中所有服务的容器
docker-compose restart nginx # 重启工程中指定服务的容器

start
docker-compose start启动服务容器。
docker-compose start # 启动工程中所有服务的容器
docker-compose start nginx # 启动工程中指定服务的容器

stop
docker-compose stop停止服务容器。
docker-compose stop # 停止工程中所有服务的容器
docker-compose stop nginx # 停止工程中指定服务的容器

1.2 build -- Build and refactor service

# 构建服务的镜像
docker-compose build

# 如果服务镜像不存在,则构建镜像并启动服务。
docker-compose up –build

# 重构服务。
docker-compose up --force-recreate 

1.3 ps, logs -- Check service information

# 查看项目中所有服务的信息。
docker-compose ps

# 查看容器的日志。
docker-compose logs

# 在服务镜像的容器中执行命令。
docker-compose exec service_name command

1.4 down -- empty owned container

#  删除服务容器(容器)
docker-compose down 

1.5 logs command

# docker-compose logs 查看服务容器的输出日志。
# 默认情况下,docker-compose将对不同的服务输出使用不同的颜色来区分。
# 可以通过--no-color来关闭颜色。
# 输出日志,不同的服务输出使用不同的颜色来区分
docker-compose logs
# 跟踪日志输出
docker-compose logs -f
# 关闭颜色
docker-compose logs --no-color

# 查看日志
docker-compose logs web  # 参考 1.9 docker-compose.yml 文件内容

1.6 run command -- execute the command on the specified container

run
docker-compose run 在指定服务容器上执行一个命令。
docker-compose run nginx echo "helloworld" # 在工程中指定服务的容器上执行 echo "helloworld"

1.7 exec command -- enter the specified container

exec
docker-compose exec进入服务容器。
docker-compose exec nginx bash # 进入工程中指定服务的容器
docker-compose exec --index=1 nginx bash # 当一个服务拥有多个容器时,可通过 --index 参数进入到该服务下的任何容器

sudo docker-compose exec jobmanager ./bin/sql-client.sh -f sql/flink_kafka1.sql
sudo docker-compose exec jobmanager ./bin/flink list
sudo docker logs -f -t --since="2023-05-08" --tail=200 flink_taskmanager_1
sudo docker-compose logs -f taskmanager

sudo docker-compose exec jobmanager bash
sudo docker-compose exec jobmanager ./bin/flink cancel 8d8cc94d73f7bd0c4cdc557264553a04

1.8 pause, unpause command -- pause and resume service container

pause
docker-compose pause暂停服务容器
docker-compose pause # 暂停工程中所有服务的容器
docker-compose pause nginx # 暂停工程中指定服务的容器

unpause
docker-compose unpause恢复服务容器。
docker-compose unpause # 恢复工程中所有服务的容器
docker-compose unpause nginx # 恢复工程中指定服务的容器

1.9 scale command -- expansion and contraction

docker-compose scale taskmanager=5
docker-compose up --scale web=5 -d

# -------------------------------------------- # 
# docker-compose.yml内容:
version: '3'
services:
  web:
    build: .
    networks:
      - app-net

  redis:
    image: "redis:alpine"
    networks:
      - app-net

networks:
  app-net:
    driver: bridge

1.20 ps command -- View container list

docker-compose ps

1.21 stop specified container

docker-compose stop jobmanager
docker-compose stop taskmanager

1.22 Delete container

docker-compose rm -f jobmanager
docker-compose rm -f taskmanager

2. Example description

2.1 Example 1

Suppose there is a project that contains an nginx service and a web service, running in different containers. nginx acts as a reverse proxy server to forward traffic to the web server.
In the project root directory, create a docker-compose.yml file and fill in the following content:
version: '3'
services:
  nginx:
    image: nginx
    ports:
      - 80:80
  web:
    build: ./web
    ports:
      - 5000:5000
Among them, the nginx service directly uses the official nginx image and maps it to port 80 of the host. The web service uses the Dockerfile in the web subdirectory under the current directory to build an image and maps it to port 5000 of the host. .
Start the service by executing the docker-compose up command:
$. docker-compose up -d
Creating network "web_default" with the default driver
Creating web_1 ...
Creating nginx_1 ...

At this time, the nginx service and web service are already running in the background.

If you need to stop the service, you can execute the docker-compose stop command:

$. docker-compose stop
Stopping nginx_1 ... done
Stopping web_1   ... done

If you need to reconstruct the service, you need to execute it in the directory where the docker-compose.yml file is locateddocker-compose up –force-recreateCommand:

$ docker-compose up --force-recreate
Recreating web_1 ...
Recreating nginx_1 ...

2.2 Example 2

Suppose you need to run two copies of the same application on one server, but they need to listen to different ports and need to be configured with different environment variables. able to pass Docker Compose to achieve.
First, create a project directory and create docker-compose.yml file and fill in the following content:
version: '3'
services:
  app1:
    image: myapp
    environment:
      PORT: 3000
      MESSAGE: "Hello from app1"
    ports:
      - 8001:3000
  app2:
    image: myapp
    environment:
      PORT: 4000
      MESSAGE: "Hello from app2"
    ports:
      - 8002:4000

Among them, two services app1 and app2 are defined, both of which are built using the myapp image.

  • The app1 service listens to port 3000, maps it to port 8001 on the host, and configures the environment variables PORT and MESSAGE;
  • The app2 service listens to port 4000, maps it to port 8002 on the host, and configures different PORT and MESSAGE.

Execute the docker-compose up -d command in the directory where the project is located to start the service, and then use the browser to access localhost:8001 and localhost:8002 to access different applications.

If you need to reconstruct the service, you need to execute it in the directory where the docker-compose.yml file is locateddocker-compose up –force-recreate Order.

Guess you like

Origin blog.csdn.net/justlpf/article/details/132734556