본문 바로가기
컨테이너/도커

07 docker-composer

by ^..^v 2019. 5. 20.
728x90
반응형

docker-compose를 이용한 컨테이너 실행

docker-compose.yaml 파일 작성을 작성합니다.

version: "3"
services:
    echo:
        build: .
        ports:
             - 8080

docker-compose up 명령으로 echo 이미지를 생성하고 컨테이너를 실행합니다.

root@server:~/docker# docker-compose up 
Creating network "docker_default" with the default driver
Building echo
Step 1/4 : FROM golang:1.9
1.9: Pulling from library/golang
55cbf04beb70: Pull complete
1607093a898c: Pull complete
9a8ea045c926: Pull complete
d4eee24d4dac: Pull complete
9c35c9787a2f: Pull complete
8b376bbb244f: Pull complete
0d4eafcc732a: Pull complete
186b06a99029: Pull complete
Digest: sha256:8b5968585131604a92af02f5690713efadf029cc8dad53f79280b87a80eb1354
Status: Downloaded newer image for golang:1.9
 ---> ef89ef5c42a9
Step 2/4 : RUN mkdir /echo
 ---> Running in 8e0f9ac0ec93
 ---> 7746328fda30
Removing intermediate container 8e0f9ac0ec93
Step 3/4 : COPY main.go /echo
 ---> 48d59ccc55ce
Removing intermediate container 83900ab8b6eb
Step 4/4 : CMD go run /echo/main.go
 ---> Running in a835fa1eb33f
 ---> 90833b1381fd
Removing intermediate container a835fa1eb33f
Successfully built 90833b1381fd
Successfully tagged docker_echo:latest
WARNING: Image for service echo was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating docker_echo_1 ... done
Attaching to docker_echo_1
echo_1  | 2019/05/20 08:03:19 start server

이미지와 컨테이너가 생성된 것을 확인합니다.

root@server:~/docker# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
docker_echo         latest              90833b1381fd        About a minute ago   750MB
golang              1.9                 ef89ef5c42a9        10 months ago        750MB

root@server:~/docker# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                     NAMES
0baa89e30283        docker_echo         "go run /echo/main.go"   About a minute ago   Up About a minute   0.0.0.0:32779->8080/tcp   docker_echo_1

호스트에서 브라우저를 실행하고 컨테이너로 요청을 보냈을 때 Hello Docker !!가 응답되는 것을 확인합니다.

컨테이너를 중지합니다.

root@server:~/docker# docker container stop docker_echo_1
docker_echo_1

root@server:~/docker# docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

여러 컨테이너 생성

scale 옵션을 이용해서 생성할 컨테이너의 개수를 지정할 수 있습니다.

root@server:~/docker# docker-compose up --help
				:
Usage: up [options] [--scale SERVICE=NUM...] [SERVICE...]

Options:
				:
--scale SERVICE=NUM    Scale SERVICE to NUM instances. Overrides the
                       `scale` setting in the Compose file if present.

echo 서버 10개를 실행합니다. 중지했던 1번 컨테이너는 재시작되며, 2번부터 10번 컨테이너는 새롭게 생성되어 시작되는 것을 확인할 수 있습니다.

root@server:~/docker# docker-compose up --scale echo=10
Starting docker_echo_1 ... done
Creating docker_echo_2  ... done
Creating docker_echo_3  ... done
Creating docker_echo_4  ... done
Creating docker_echo_5  ... done
Creating docker_echo_6  ... done
Creating docker_echo_7  ... done
Creating docker_echo_8  ... done
Creating docker_echo_9  ... done
Creating docker_echo_10 ... done
Attaching to docker_echo_1, docker_echo_6, docker_echo_2, docker_echo_5, docker_echo_4, docker_echo_3, docker_echo_7, docker_echo_10, docker_echo_9, docker_echo_8
echo_1  | 2019/05/20 08:16:23 start server
echo_2  | 2019/05/20 08:16:29 start server
echo_6  | 2019/05/20 08:16:29 start server
echo_4  | 2019/05/20 08:16:32 start server
echo_5  | 2019/05/20 08:16:32 start server
echo_3  | 2019/05/20 08:16:32 start server
echo_7  | 2019/05/20 08:16:33 start server
echo_10 | 2019/05/20 08:16:33 start server
echo_8  | 2019/05/20 08:16:33 start server
echo_9  | 2019/05/20 08:16:33 start server

컨테이너의 실행을 확인합니다.

root@server:~/docker# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
5e710ee49709        docker_echo         "go run /echo/main.go"   2 minutes ago       Up 2 minutes        0.0.0.0:32783->8080/tcp   docker_echo_10
24e190216f6d        docker_echo         "go run /echo/main.go"   2 minutes ago       Up 2 minutes        0.0.0.0:32788->8080/tcp   docker_echo_7
680a7b3cd435        docker_echo         "go run /echo/main.go"   2 minutes ago       Up 2 minutes        0.0.0.0:32787->8080/tcp   docker_echo_4
dc99a76e4c52        docker_echo         "go run /echo/main.go"   2 minutes ago       Up 2 minutes        0.0.0.0:32785->8080/tcp   docker_echo_8
2611e612ff3b        docker_echo         "go run /echo/main.go"   2 minutes ago       Up 2 minutes        0.0.0.0:32784->8080/tcp   docker_echo_5
59733b932734        docker_echo         "go run /echo/main.go"   2 minutes ago       Up 2 minutes        0.0.0.0:32781->8080/tcp   docker_echo_9
298ed5999963        docker_echo         "go run /echo/main.go"   2 minutes ago       Up 2 minutes        0.0.0.0:32786->8080/tcp   docker_echo_2
847f53f5fac7        docker_echo         "go run /echo/main.go"   2 minutes ago       Up 2 minutes        0.0.0.0:32789->8080/tcp   docker_echo_3
99c703af79bd        docker_echo         "go run /echo/main.go"   2 minutes ago       Up 2 minutes        0.0.0.0:32782->8080/tcp   docker_echo_6
0baa89e30283        docker_echo         "go run /echo/main.go"   15 minutes ago      Up 2 minutes        0.0.0.0:32780->8080/tcp   docker_echo_1

docker-compose logs 명령으로 컨테이너의 로그를 확인할 수 있습니다.

root@server:~/docker# docker-compose logs
Attaching to docker_echo_10, docker_echo_7, docker_echo_4, docker_echo_8, docker_echo_5, docker_echo_9, docker_echo_2, docker_echo_3, docker_echo_6, docker_echo_1
echo_10 | 2019/05/20 08:16:33 start server
echo_4  | 2019/05/20 08:16:32 start server
echo_7  | 2019/05/20 08:16:33 start server
echo_8  | 2019/05/20 08:16:33 start server
echo_5  | 2019/05/20 08:16:32 start server
echo_9  | 2019/05/20 08:16:33 start server
echo_2  | 2019/05/20 08:16:29 start server
echo_3  | 2019/05/20 08:16:32 start server
echo_6  | 2019/05/20 08:16:29 start server
echo_1  | 2019/05/20 08:03:19 start server
echo_1  | 2019/05/20 08:06:49 received request
echo_1  | 2019/05/20 08:06:50 received request
echo_1  | 2019/05/20 08:06:50 received request
echo_1  | 2019/05/20 08:16:23 start server

여러 컨테이너 시작/정지/재시작/일시정지/재개

docker-compose를 사용하면 여러 개의 서비스를 일괄적으로 시작/일시정지/정지/재시작할 수 있습니다.

root@server:~/docker# docker-compose stop
Stopping docker_echo_10 ... done
Stopping docker_echo_7  ... done
Stopping docker_echo_4  ... done
Stopping docker_echo_8  ... done
Stopping docker_echo_5  ... done
Stopping docker_echo_9  ... done
Stopping docker_echo_2  ... done
Stopping docker_echo_3  ... done
Stopping docker_echo_6  ... done
Stopping docker_echo_1  ... done

root@server:~/docker# docker-compose restart
Restarting docker_echo_10 ... done
Restarting docker_echo_7  ... done
Restarting docker_echo_4  ... done
Restarting docker_echo_8  ... done
Restarting docker_echo_5  ... done
Restarting docker_echo_9  ... done
Restarting docker_echo_2  ... done
Restarting docker_echo_3  ... done
Restarting docker_echo_6  ... done
Restarting docker_echo_1  ... done

root@server:~/docker# docker-compose pause
Pausing docker_echo_1  ... done
Pausing docker_echo_6  ... done
Pausing docker_echo_3  ... done
Pausing docker_echo_2  ... done
Pausing docker_echo_9  ... done
Pausing docker_echo_5  ... done
Pausing docker_echo_8  ... done
Pausing docker_echo_4  ... done
Pausing docker_echo_7  ... done
Pausing docker_echo_10 ... done

root@server:~/docker# docker-compose unpause
Unpausing docker_echo_10 ... done
Unpausing docker_echo_7  ... done
Unpausing docker_echo_4  ... done
Unpausing docker_echo_8  ... done
Unpausing docker_echo_5  ... done
Unpausing docker_echo_9  ... done
Unpausing docker_echo_2  ... done
Unpausing docker_echo_3  ... done
Unpausing docker_echo_6  ... done
Unpausing docker_echo_1  ... done

docker-compose down 명령을 사용하면 docker-compose.yaml 파일에 정의된 이미지와 컨테이너를 일괄적으로 삭제할 수 있습니다. docker-compose down 명령은 실행 중인 컨테이너를 정지하고, docker 이미지, 네트워크, 데이터 볼륨을 일괄적으로 삭제합니다.

root@server:~/docker# docker-compose down
Stopping docker_echo_10 ... done
Stopping docker_echo_7  ... done
Stopping docker_echo_4  ... done
Stopping docker_echo_8  ... done
Stopping docker_echo_5  ... done
Stopping docker_echo_9  ... done
Stopping docker_echo_2  ... done
Stopping docker_echo_3  ... done
Stopping docker_echo_6  ... done
Stopping docker_echo_1  ... done
Removing docker_echo_10 ... done
Removing docker_echo_7  ... done
Removing docker_echo_4  ... done
Removing docker_echo_8  ... done
Removing docker_echo_5  ... done
Removing docker_echo_9  ... done
Removing docker_echo_2  ... done
Removing docker_echo_3  ... done
Removing docker_echo_6  ... done
Removing docker_echo_1  ... done
Removing network docker_default
root@server:~/docker# docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
root@server:~/docker# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker_echo         latest              90833b1381fd        31 minutes ago      750MB
golang              1.9                 ef89ef5c42a9        10 months ago       750MB
728x90
반응형

댓글