使用Docker-compose

2020/12/2 Docker

🌙 0.查看文件:

$ compose pwd
/home/compose

$ compose ls
docker-compose.yml

$  compose ls -la
total 12
drwxr-xr-x 2 root root 4096 Aug 15 15:40 .
drwxr-xr-x 7 root root 4096 Aug  8 19:05 ..
-rw-r--r-- 1 root root 1586 Aug 15 15:40 docker-compose.yml
1
2
3
4
5
6
7
8
9
10
11

cat docker-compose.yml内容:


version: '1.0'
services:                                      # 集合
  docker_jenkins:
    user: root                                 # 为了避免一些权限问题 在这我使用了root
    restart: always                            # 重启方式
    image: jenkinsci/blueocean:latest          # 指定服务所使用的镜像
    container_name: jenkins                    # 容器名称
    ports:                                     # 对外暴露的端口定义
      - 6868:8080
      - 50000:50000
    volumes:                                   # 卷挂载路径
      - /home/jenkins/jenkins_home/:/var/jenkins_home  # 这是我们一开始创建的目录挂载到容器内的jenkins_home目录
      - /var/run/docker.sock:/var/run/docker.sock
      - /usr/bin/docker:/usr/bin/docker                # 这是为了我们可以在容器内使用docker命令
      - /usr/local/bin/docker-compose:/usr/local/bin/docker-compose
  docker_nginx:
    restart: always
    image: nginx
    container_name: nginx
    ports:
      - 80:80
      - 443:443
    volumes:
      - /home/nginx/conf.d/:/etc/nginx/conf.d
      - /home/webserver/:/usr/share/nginx/html
  docker_gitlab:
    user: root
    restart: always
    image: gitlab/gitlab-ce:latest
    container_name: gitlab
    ports:
      - 1080:80
      - 10443:443
      - "10222:22"
    volumes:
      - /home/gitlab/conf:/etc/gitlab
      - /home/gitlab/logs:/var/log/gitlab
      - /home/gitlab/data:/var/opt/gitlab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

在当前目录下执行以下命令:

🌙 1.启动up

# -d 后台启动
$ docker-compose up -d

Starting jenkins ... done
Starting gitlab  ... done
Starting nginx   ... done

# 前台启动,Ctrl-C退出
$ docker-compose up
gitlab            |
gitlab            | ==> /var/log/gitlab/postgres-exporter/current <==
gitlab            | 2022-08-15_07:22:47.38074 ts=2022-08-15T07:22:47.380Z caller=log.go:168 level=debug msg="Querying PostgreSQL version" server=/var/opt/gitlab/postgresql:5432
gitlab            | 2022-08-15_07:22:47.38110 ts=2022-08-15T07:22:47.381Z caller=log.go:168 level=debug msg="Querying pg_setting view" server=/var/opt/gitlab/postgresql:5432

... ... ...

gitlab            | 2022-08-15_07:46:32.47538 ts=2022-08-15T07:46:32.475Z caller=log.go:168 level=debug msg="Querying namespace" namespace=pg_oldest_blocked
^CGracefully stopping... (press Ctrl+C again to force)
Stopping gitlab  ... done
Stopping nginx   ... done
Stopping jenkins ... done

$ compose docker ps -a
CONTAINER ID   IMAGE                        COMMAND                  CREATED          STATUS                        PORTS     NAMES
30688cb25f7d   gitlab/gitlab-ce:latest      "/assets/wrapper"        28 minutes ago   Exited (0) 23 seconds ago               gitlab
662ffe369cee   nginx                        "/docker-entrypoint.…"   29 minutes ago   Exited (1) 48 seconds ago               nginx
770ae9cfa53d   jenkinsci/blueocean:latest   "/sbin/tini -- /usr/…"   6 days ago       Exited (143) 32 seconds ago             jenkins

# 强制启动,不重建
$ docker-compose up -d --no-recreate
Starting jenkins ... done
Starting gitlab  ... done
Starting nginx   ... done

# 
$ docker-compose up -d -t 30

Starting gitlab  ... done
Starting jenkins ... done
Starting nginx   ... done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
–no-deps This will not start any linked depended services.
–no-build This will not build the image, even when the image is missing
–abort-on-container-exit This will stop all the containers if any container was stopped. You cannot use this option with -d, you have to use this option by itself.
–no-color In the output, this will not show any color. This will display the monochrome output on screen.
1
2
3
4

2.停止stop

$ docker-compose stop
Stopping gitlab  ...
Stopping nginx   ... done
Stopping jenkins ... done

docker-compose stop [container name]

# 关闭超时时间默认10s,-t指定
docker-compose stop -t 30
1
2
3
4
5
6
7
8
9

3.删除remove

# 删除数据卷
docker-compose rm -v

# 删除指定容器的数据卷
docker-compose rm -f [container name]

1
2
3
4
5
6

4.状态status

# 查看容器状态
docker-compose ps

# 查看容器ID
docker-compose ps -q db
1
2
3
4
5

5.重启restart

# 关闭
docker-compose stop && docker-compose rm -f
# 启动
docker-compose up -d 
1
2
3
4

Howto use docker-compose to Start, Stop, Remove Docker Containers (opens new window)