Docker커맨드 설명(3)

[Docker]Docker커맨드 설명(3) - run


주의 사항
  1. 도커 커맨드는 기본적으로 root권한으로 실행하여야 한다.
  2. 지난 포스팅을 진행하였다는 것을 전제로 함

docker run
  • 도커 이미지로 부터 컨테이너를 생성하여 실행하는 명령어
help로 run 명령어에 대해서 확인
$ sudo docker run —help
Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

  -a, --attach=[]                 Attach to STDIN, STDOUT or STDERR
  --add-host=[]                   Add a custom host-to-IP mapping (host:ip)
  --blkio-weight=0                Block IO (relative weight), between 10 and 1000
  --cpu-shares=0                  CPU shares (relative weight)
  --cap-add=[]                    Add Linux capabilities
  --cap-drop=[]                   Drop Linux capabilities
  --cgroup-parent=                Optional parent cgroup for the container
  --cidfile=                      Write the container ID to the file
  --cpu-period=0                  Limit CPU CFS (Completely Fair Scheduler) period
  --cpu-quota=0                   Limit CPU CFS (Completely Fair Scheduler) quota
  --cpuset-cpus=                  CPUs in which to allow execution (0-3, 0,1)
  --cpuset-mems=                  MEMs in which to allow execution (0-3, 0,1)
* -d, --detach=false              Run container in background and print container ID
  --device=[]                     Add a host device to the container
  --disable-content-trust=true    Skip image verification
  --dns=[]                        Set custom DNS servers
  --dns-opt=[]                    Set DNS options
  --dns-search=[]                 Set custom DNS search domains
  -e, --env=[]                    Set environment variables
  --entrypoint=                   Overwrite the default ENTRYPOINT of the image
  --env-file=[]                   Read in a file of environment variables
  --expose=[]                     Expose a port or a range of ports
  --group-add=[]                  Add additional groups to join
  -h, --hostname=                 Container host name
  --help=false                    Print usage
* -i, --interactive=false         Keep STDIN open even if not attached
  --ipc=                          IPC namespace to use
  --kernel-memory=                Kernel memory limit
  -l, --label=[]                  Set meta data on a container
  --label-file=[]                 Read in a line delimited file of labels
  --link=[]                       Add link to another container
  --log-driver=                   Logging driver for container
  --log-opt=[]                    Log driver options
  --lxc-conf=[]                   Add custom lxc options
  -m, --memory=                   Memory limit
  --mac-address=                  Container MAC address (e.g. 92:d0:c6:0a:29:33)
  --memory-reservation=           Memory soft limit
  --memory-swap=                  Total memory (memory + swap), '-1' to disable swap
  --memory-swappiness=-1          Tuning container memory swappiness (0 to 100)
* --name=                         Assign a name to the container
  --net=default                   Set the Network for the container
  --oom-kill-disable=false        Disable OOM Killer
* -P, --publish-all=false         Publish all exposed ports to random ports
  -p, --publish=[]                Publish a container's port(s) to the host
  --pid=                          PID namespace to use
  --privileged=false              Give extended privileges to this container
  --read-only=false               Mount the container's root filesystem as read only
  --restart=no                    Restart policy to apply when a container exits
  --rm=false                      Automatically remove the container when it exits
  --security-opt=[]               Security Options
  --sig-proxy=true                Proxy received signals to the process
  --stop-signal=SIGTERM           Signal to stop a container, SIGTERM by default
* -t, --tty=false                 Allocate a pseudo-TTY
  -u, --user=                     Username or UID (format: <name|uid>[:<group|gid>])
  --ulimit=[]                     Ulimit options
  --uts=                          UTS namespace to use
  -v, --volume=[]                 Bind mount a volume
  --volume-driver=                Optional volume driver for the container
  --volumes-from=[]               Mount volumes from the specified container(s)
  -w, --workdir=                  Working directory inside the container
우선 커맨드를 모를 때는 help를 먼저 확인하여 보자.
  • 지난 포스팅에서 입력하였던 커맨드에서 사용하였던 옵션들이 눈에 들어올 것이다.
    (사용한 옵션은 *로 표기하였다.)
sudo docker run -i -t —name=test ubuntu /bin/bash
  • help에도 있지만 분해해서 살펴보자.
    Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG…]
    사용법: docker run 뒤에 [옵션]을 넣고 “이미지”를 넣고 이미지에서 실행할 [커맨드]를 넣는다.
  • 사용한 옵션
    • -i : 표준 입출력 환경
    • -t : 텍스트 입출력 터미널 환경
    • —name=[컨테이너 이름]
    • /bin/bash : bash shell을 실행
  • 해석
    • /bin/bash를 표준 입출력 터미널 환경으로 컨테이너에 연결한다.
    • 컨테이너 이름은 [컨테이너 이름]으로 생성한다.
—name옵션을 생략한 경우 랜덤으로 컨테이너 명이 생성된다.
  • 지난 포스팅에서는 의미를 모르고 실행하였던 커맨드의 의미를 알아보았다.
    그럼 테스트로 새로운 컨테이너를 생성하여 어떤 상황인지 확인해보자.
    (—name옵션 생략하여 생성된 컨테이너의 이름을 확인)
$ sudo docker run -i -t ubuntu /bin/bash
  • 컨테이너에 접속하였다면 커맨드라인의 호스트 명이 변경되어 있을 것이다.
    예시 : root@659a7bd6768d:/#
  • 컨테이너를 종료하지 않고 OS로 빠져나와 실행되어있는 컨테이너를 확인해보자.
    root@659a7bd6768d:/# [Ctrl + P] + [Ctrl + Q]
    $ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
d27fd34782bd        ubuntu              "/bin/bash"         6 seconds ago       Up 6 seconds                                 tender_jennings
ps의 커맨드로 현재 실행되어 있는 컨테이너의 정보 확인이 가능하다.
(그리고 —name 옵션을 생략하여 컨테이너 이름이 랜덤으로 생성되어 tender_jennings인 것도 확인)
  • 지난 포스팅에서 도커 이미지를 이용하여 다시 한번 run 커맨드를 실행하였다.
$ sudo docker run -d -p 8080:80 ubuntu:nginx-test
  • 사용한 옵션
    • -d : 데몬으로 실행
    • -p : 포트 설정(OS쪽 포트:컨테이너 쪽 포트)
ubuntu:nginx-test이미지로 컨테이너를 생성하는데 실행은 데몬 형태로 포트는 8080 -> 80으로 포워딩한다.
지난 포스팅에서 입력 하였던 docker run 명령어에 대해 알아보았다.
요약
1. 지난 포스팅에서 사용하였던 옵션은 docker run --help에서 확인 가능
2. docker run에는 많은 옵션이 있으며 이번 포스팅에서 봐야할 옵션은 하기와 같다. 
   [-i, -t, --name, -d, -p]

이 포스팅을 읽고 의문이 생길 것이다.
그럼 웹페이지 소스는???
docker의 응용편에서 docker의 특성과 로그 및 소스 관리에 대해서 포스팅 할 예정

댓글