Study/Container

[Docker] Docker 기본 명령어 사용법 정리

Dream Amal 2024. 1. 15.

컨테이너 관리 명령어 Cheat Sheet

https://docs.docker.com/get-started/docker_cheatsheet.pdf

Use the Docker command line

컨테이너 이미지 관리 명령어

docker [OPTIONS] COMMAND
  • 이미지 검색 : search
  • 이미지 다운로드 : pull
  • 이미지 목록보기 : images
  • 이미지 삭제 : rmi
  • 이미지 레이어 목록보기 : history
  • 이미지 태그 수정 : tag

컨테이너 관리 명령어

docker run [OPTIONS] container_image COMMAND
  • 컨테이너 실행 : run
    • option
      • d - detach (background 실행)
      • it - attach (foreground 실행)
  • 컨테이너 목록보기 : ps
  • 컨테이너 삭제 : rm
  • 컨테이너 세부 정보 확인 : inspect
  • 컨테이너 중지 : stop
  • 컨테이너 실행 : start
    • stop된 컨테이너는 startattach 로 다시 재실행 시킬 수 있음
  • 컨테이너 생성 : create

docker로 실행시킨 image의 ip 대역이 실제 instance의 private ip 대역과 다른 이유?!

  • docker 는 install 시 자신의 network 대역을 가지고 동작하게끔 되어 있음

Docker Container Lifecycle

명령어 사용 예시

docker images
docker run -d --name="web-nginx" nginx:latest
docker ps
curl localhost

docker inspect web-nginx
docker inspect web-nginx | grep IP

docker stop elated_chatelet
docker rm elated_chatelet

docker ps -a

docker images -aq
docker rmi -f $(docker images -aq)
docker images

docker ps -aq

docker images -a
docker rm -f $(docker ps -aq)

docker images -a

실전예제 : 아파치 웹 서버 실행하기

# 아파치 이미지 검색
docker search httpd
# 이미지 다운로드
docker pull httpd:latest
# 다운로드된 이미지 확인
docker images
# 컨테이너 실행
docker run --name web-httpd -p 80:80 -d httpd:latest
# 실행중인 컨테이너 확인
docker ps
# 웹 페이지 내용 확인
curl localhost:80
## <html><body><h1>It works!</h1></body></html>
curl -I localhost:80
## HTTP/1.1 200 OK
## Date: Tue, 08 Aug 2023 05:44:33 GMT
## Server: Apache/2.4.57 (Unix)
## Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
## ETag: "2d-432a5e4a73a80"
## Accept-Ranges: bytes
## Content-Length: 45
## Content-Type: text/html
# 컨테이너 강제 종료
docker rm -f web-httpd

이미지 업로드

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

컨테이너 조작 명령어

docker [OPTIONS] COMMAND
  • 컨테이너 프로세스 실행 : exec
    • run과의 차이점
      • run : 새로운 컨테이너를 만들어서 실행
      • exec : 백그라운드에서 실행 중인 컨테이너에 명령어 전달
        • 예시 
          • docker exec -it web-nginx /bin/bash
  • 컨테이너 프로세스 정보보기 : top
  • 컨테이너 표준 출력 결과 보기 : logs
    • option
      • f : 실시간 로그 모니터링
  • 컨테이너 내에서 파일 복사 : cp
    • docker cp [OPTIONS] source_path container:destination_path
  • 컨테이너 내에서 파일 변경 이력 확인 : diff
    • docker diff <container_name>
      • A : add
      • C : change
      • D : delete
728x90

'Study > Container' 카테고리의 다른 글

[Docker] Docker Container의 이해  (0) 2024.01.15
[Docker] Ubuntu22.x EC2에 Docker 설치하기  (0) 2024.01.13

댓글