Study/Docker

도커 기본 명령어

momong'-' 2022. 4. 18. 15:45

도커 명령어 확인

docker --help

도커 명령어를 볼 수 있다.

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  config      Manage Docker configs
  container   Manage containers
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.

 

명령어 설명 및 옵션 확인

 

도커 버전 확인하기

docker version


 

컨테이너 목록 확인하기

docker ps [OPTIONS]

 

옵션 설명
-a, --all 모든 컨테이너(종료된 컨테이너 포함)

 

컨테이너 생성하기

docker create [OPTIONS] IMAGE [COMMAND] [ARG...]

 

컨테이너 실행하기

docker start [OPTIONS] CONTAINER [CONTAINER...]

 

컨테이너 실행하기

docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

컨테이너 생성 실행 들어가기까지 한번에 하는 명령어

옵션 설명
-d detached mode. 백그라운드 모드
-p 호스트와 컨테이너의 포트를 연결 (포워딩)
-v 호스트와 컨테이너의 디렉토리를 연결 (마운트)
-e 컨테이너 내에서 사용할 환경변수 설정
-name 컨티이너 이름 설정
-rm 프로세스 종료시 컨테이너 자동 제거
-it -i와 -t를 동시에 사용한 것으로 터미널을 위한 옵션
-link 컨테이너 연결 [컨테이너명: 별칭]

 

컨테이너 중지하기

docker stop [OPTIONS] CONTAINER [CINTAINER...]

컨테이너 하나 또는 여러개를 중지할 수 있다.

컨테이너 ID 또는 이름을 입력하면 된다.

 

컨테이너 제거하기

docker rm [OPTIONS] CONTAINER [CONTAINER...]

종료된 컨테이너를 하나 또는 여러개를 삭제할 수 있다.

옵션 설명
-f 강제 삭제

 

 

아래 명령어를 입력하면 중지된 컨테이너 ID를 가져와서 한번에 삭제한다.

docker rm -v $(docker ps -a -q -f status=exited)

 

컨테이너 로그보기

docker logs [OPTIONS] CONTAINER

옵션이 없을 경우 전체 로그를 전부 다 출력한다.

옵션 설명
--tail 마지막 n 줄을 출력 (ex) --tail 10)
-f 실시간으로 로그를 보여줌. 중지하려면 ctrl + C

컨테이너의 로그파일은 json방식으로 어딘가에 저장된다.

어느 정도 규모가 커지만 로그 서비스를 이용하는 게 좋다.

(도커는 다양한 플러그인을 지원해 json이 아닌 특정 로그서비스에서 스트림을 전달할 수 있다.)

 

컨테이너 명령어 실행하기

docker exec [OPTIONS] CONTAINER COMMANED [ARG...]

run: 새로 컨테이너를 만들어 실행

exec: 실행중인 컨테이너에 명령어를 내림

 

 

컨테이너 이름변경하기

docker rename CONTAINER NEW_NAME

 


 

이미지 목록 확인하기

docker images [OPTIONS] [REPOSITORY[:TAG]]

도커가 다운로드한 이미지 목록을 보는 명령어

 

이미지 검색하기

docker search [IMAGE NAME]

 

이미지 다운로드하기 

docker pull [OPTIONS] NAME[:TAG|@DIGEST]

run 명령어를 입력할때 이미지가 없으면 자동으로 다운을 받는다.

같은 태그이지만 이미지가 업데이트 된 경우 pull 명령어를 통해 새로 다운받을 수 있다.

 

이미지 생성하기

docker build [OPTIONS] [PATH|URL|-]

 

이미지 삭제하기

docker rmi [OPTIONS] IMAGE [IMAGE...]

컨테이너가 실행중인 이미지는 삭제되지 않는다.