[클라우드/Docker 기본(10) - Docker-compose(2) : 기본 실습]

SooYeon Yeon·2022년 8월 30일
0

클라우드 Docker

목록 보기
11/24

yaml 파일 만들기 실습 (1) - wordpress, db version, service 정의

rapa@rapa:~/0819$ touch docker-compose.yml
rapa@rapa:~/0819$ vi docker-compose.yml
  • docker-compose.yml
version: "3.0"
services:
  wordpress:
    image: wordpress
    ports:
      - "8888:80" 
    environment:
      - WORDPRESS_DB_PASSWORD=test123
      - WORDPRESS_DB_NAME=wpdb
      - WORDPRESS_DB_USER=root
    depends_on:
      - db
    links:
      - db:mysql #mysql은 별칭
  
  db: 
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=test123
      - MYSQL_DATABASE=wpdb
  • docker-compose 실행
rapa@rapa:~/0819$ docker-compose up -d
Creating network "0819_default" with the default driver
Creating 0819_db_1 ... done
Creating 0819_wordpress_1 ... done
  • 컨테이너 확인
rapa@rapa:~/0819$ docker container ls
CONTAINER ID   IMAGE                       COMMAND                  CREATED             STATUS             PORTS                                       NAMES
749141e8efc0   wordpress                   "docker-entrypoint.s…"   13 seconds ago      Up 10 seconds      0.0.0.0:8888->80/tcp, :::8888->80/tcp       0819_wordpress_1
fd47eb7ec173   mysql:5.7                   "docker-entrypoint.s…"   14 seconds ago      Up 13 seconds      3306/tcp, 33060/tcp                         0819_db_1
52671517fe1d   centos:7                    "/bin/bash"              About an hour ago   Up About an hour                                               centos11
0dd8f9536071   hyper/docker-registry-web   "start.sh"               3 hours ago         Up 2 hours         0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   registry-web
dc0b26f3dcde   registry                    "/entrypoint.sh /etc…"   3 hours ago         Up 2 hours         0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   registry

NAMES를 보면 앞에 디렉토리 이름이 붙여져 있음(다른 디렉토리에서 같은 이름 만들어도 구분하기 위해)

번호가 1번이 붙어있는 것 (0819_wordpress 1)은 똑같은 컨테이너를 하나 더 만들수 있다는 뜻.

그치만 포트번호때문에 충돌발생가능

yaml파일 만들기 실습(2) - web, db version, services 정의

  • yaml 파일 작성하기
rapa@rapa:~/0822$ touch docker-compose.yml
rapa@rapa:~/0822$ vi docker-compose.yml 
rapa@rapa:~/0822$ cat docker-compose.yml 
version: '3.7'
services: 
  web: 
    image: httpd 
    ports: 
      - "8001:80" 
    command: httpd -D FOREGROUND
    depends_on: 
      - db
    links: 
      - db:mysql
  db: 
    image: mysql:5.7
    environment: 
      - MYSQL_ROOT_PASSWORD=test123
      - MYSQL_DATABASE=testdb
  • 야믈코드 실행 (백그라운드로 -d)
rapa@rapa:~/0822$ docker-compose up -d
Creating network "0822_default" with the default driver
Creating 0822_db_1 ... done
Creating 0822_web_1 ... done

별도의 네트워크를 만들지 않아도, 본인디렉토리_default라는 별도의 네트워크가 만들어진다.

별도의 네트워크를 위한 기본네트워크가 생성되고, 컨테이너도 생성된다.

  • 정상적으로 생성되었는 지 확인
rapa@rapa:~/0822$ docker-compose ps
   Name                Command             State              Ports            
-------------------------------------------------------------------------------
0822_db_1    docker-entrypoint.sh mysqld   Up      3306/tcp, 33060/tcp         
0822_web_1   httpd -D FOREGROUND           Up      0.0.0.0:8001->80/tcp,:::8001
                                                   ->80/tcp
  • web을 2개 만들려했지만 오류 발생 (포트가 겹쳐서)
rapa@rapa:~/0822$ docker-compose scale web=2
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
WARNING: The "web" service specifies a port on the host. If multiple containers for this service are created on a single host, the port will clash.
Creating 0822_web_2 ... 
Creating 0822_web_2 ... error

ERROR: for 0822_web_2  Cannot start service web: driver failed programming external connectivity on endpoint 0822_web_2 (d70df0ed10fc7f3a808d24e19d073835a3ff3fb0411b131635ab5f1919b8c44b): Bind for 0.0.0.0:8001 failed: port is already allocated
ERROR: Cannot start service web: driver failed programming external connectivity on endpoint 0822_web_2 (d70df0ed10fc7f3a808d24e19d073835a3ff3fb0411b131635ab5f1919b8c44b): Bind for 0.0.0.0:8001 failed: port is already allocated
  • 이를 해결하기 위해 yaml파일 수정
rapa@rapa:~/0822$ cat docker-compose.yml 
version: '3.7'
services: 
  web: 
    image: httpd 
    ports: 
      - "8001-8002:80" 
    command: httpd -D FOREGROUND
    depends_on: 
      - db
    links: 
      - db:mysql
  db: 
    image: mysql:5.7
    environment: 
      - MYSQL_ROOT_PASSWORD=test123
      - MYSQL_DATABASE=testdb
  • scale로 2개가 만들어짐
rapa@rapa:~/0822$ docker-compose scale web=2
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
WARNING: The "web" service specifies a port on the host. If multiple containers for this service are created on a single host, the port will clash.
Creating 0822_web_2 ... done
  • 확인
rapa@rapa:~/0822$ docker-compose ps
   Name                Command             State              Ports            
-------------------------------------------------------------------------------
0822_db_1    docker-entrypoint.sh mysqld   Up      3306/tcp, 33060/tcp         
0822_web_1   httpd -D FOREGROUND           Up      0.0.0.0:8001->80/tcp,:::8001
                                                   ->80/tcp                    
0822_web_2   httpd -D FOREGROUND           Up      0.0.0.0:8002->80/tcp,:::8002
                                                   ->80/tcp
  • 포트를 80으로 수정 하면 ?
rapa@rapa:~/0822$ cat docker-compose.yml 
version: '3.7'
services: 
  web: 
    image: httpd 
    ports: 
      - "8001-8002:80" 
    command: httpd -D FOREGROUND
    depends_on: 
      - db
    links: 
      - db:mysql
  db: 
    image: mysql:5.7
    environment: 
      - MYSQL_ROOT_PASSWORD=test123
      - MYSQL_DATABASE=testdb
  • compose up
rapa@rapa:~/0822$ docker-compose up -d
0822_db_1 is up-to-date
Stopping and removing 0822_web_2 ... done
Recreating 0822_web_1            ... done

0822web2는 사라짐. 0822_web_1은 변경사항이 있게됨(8001:80이었다가 80으로 되었으니)

rapa@rapa:~/0822$ docker-compose ps
   Name                Command             State              Ports            
-------------------------------------------------------------------------------
0822_db_1    docker-entrypoint.sh mysqld   Up      3306/tcp, 33060/tcp         
0822_web_1   httpd -D FOREGROUND           Up      0.0.0.0:49153->80/tcp,:::491
                                                   53->80/tcp

호스트의 랜덤포트와 컨테이너의 80포트가 연결 된 것을 볼 수 있음

  • scale을 3으로 늘림
rapa@rapa:~/0822$ docker-compose scale web=3
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Creating 0822_web_2 ... done
Creating 0822_web_3 ... done
  • 확인
rapa@rapa:~/0822$ docker-compose ps
   Name                Command             State              Ports            
-------------------------------------------------------------------------------
0822_db_1    docker-entrypoint.sh mysqld   Up      3306/tcp, 33060/tcp         
0822_web_1   httpd -D FOREGROUND           Up      0.0.0.0:49153->80/tcp,:::491
                                                   53->80/tcp                  
0822_web_2   httpd -D FOREGROUND           Up      0.0.0.0:49154->80/tcp,:::491
                                                   54->80/tcp                  
0822_web_3   httpd -D FOREGROUND           Up      0.0.0.0:49155->80/tcp,:::491
                                                   55->80/tcp
  • scale을 1로 바꾸면 web2, 3은 삭제됨
rapa@rapa:~/0822$ docker-compose scale web=1
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Stopping and removing 0822_web_2 ... done
Stopping and removing 0822_web_3 ... done
rapa@rapa:~/0822$ docker-compose ps
   Name                Command             State              Ports            
-------------------------------------------------------------------------------
0822_db_1    docker-entrypoint.sh mysqld   Up      3306/tcp, 33060/tcp         
0822_web_1   httpd -D FOREGROUND           Up      0.0.0.0:49153->80/tcp,:::491
                                                   53->80/tcp
  • 디렉토리 test1을 만들고 이동 후 상위 디렉토리에 만들었던 yml파일을 복사
rapa@rapa:~/0822$ mkdir test1 ; cd test1
rapa@rapa:~/0822/test1$ cd ../docker-compose.yml .
bash: cd: too many arguments
rapa@rapa:~/0822/test1$ cp ../docker-compose.yml .
rapa@rapa:~/0822/test1$ ls
docker-compose.yml
  • 만들고 확인
apa@rapa:~/0822/test1$ dc up -d
Creating network "test1_default" with the default driver
Creating test1_db_1 ... done
Creating test1_web_1 ... done
rapa@rapa:~/0822/test1$ dc ps
   Name                 Command             State              Ports           
-------------------------------------------------------------------------------
test1_db_1    docker-entrypoint.sh mysqld   Up      3306/tcp, 33060/tcp        
test1_web_1   httpd -D FOREGROUND           Up      0.0.0.0:49156->80/tcp,:::49
                                                    156->80/tcp
  • 네트워크 확인 → 각자 다른 것을 알 수 있음
rapa@rapa:~/0822/test1$ docker network ls
NETWORK ID     NAME            DRIVER    SCOPE
10f5116a06f6   0819_default    bridge    local
797e325dfacd   0822_default    bridge    local
56189475fb8a   bridge          bridge    local
3ead376f089f   host            host      local
39430d1f4412   none            null      local
c0238bf90be3   private1        bridge    local
528e7ecee3f1   private2        bridge    local
ea7561a51091   test1_default   bridge    local
  • -p 옵션을 이용해 다른 디렉토리에 있는 컨테이너를 확인할 수 있다.
rapa@rapa:~/0822/test1$ dc -p 0822 ps
   Name                Command             State              Ports            
-------------------------------------------------------------------------------
0822_db_1    docker-entrypoint.sh mysqld   Up      3306/tcp, 33060/tcp         
0822_web_1   httpd -D FOREGROUND           Up      0.0.0.0:49153->80/tcp,:::491
                                                   53->80/tcp
  • down으로 지울 수 있다.(네트워크도)
rapa@rapa:~/0822/test1$ dc -p 0822 down
Stopping 0822_web_1 ... done
Stopping 0822_db_1  ... done
Removing 0822_web_1 ... done
Removing 0822_db_1  ... done
Removing network 0822_default
  • 모든 컨테이너와 네트워크 한번에 삭제하기
rapa@rapa:~/0822/test1$ dc down
Stopping test1_web_1 ... done
Stopping test1_db_1  ... done
Removing test1_web_1 ... done
Removing test1_db_1  ... done
Removing network test1_default

0개의 댓글