본문 바로가기
Cloud

docker 기본 명령어 정리

by 양눈 2025. 2. 5.
반응형

Docker의 기본적인 명령어는 주로 이미지와 컨테이너 관리에 사용됩니다. 아래는 자주 사용되는 기본 Docker 명령어들입니다:

1. Docker 이미지 관련 명령어

    • docker build: Dockerfile을 기반으로 이미지를 빌드합니다.
       
      docker build -t <이미지 이름>:<태그> <디렉토리 경로>
      docker build -t web:lastest  /home/web/
       
    • docker pull: Docker Hub에서 이미지를 다운로드합니다.
       
      docker pull <이미지 이름>
       
    • docker images: 로컬에 있는 Docker 이미지를 나열합니다.
       
      docker images
       
    • docker rmi: 이미지를 삭제합니다.

       docker rmi <이미지 이름>
    • docker push : 이미지를 업로드 합니다. 
       

      docker push [레지스트리주소/]저장소이름[:태그]

 Docker Hub에 이미지 업로드 하기

    1. Docker Hub에 로그인
       
      docker login
       
    2. 이미지 태깅
       
      docker tag web:lastest  username/my-web:latest
      docker tag [로컬원본] [업로드할 목적지]
       
    3. 이미지 푸시
       
      docker push username/my-web:latest
       

2. Docker 컨테이너 관련 명령어

  • docker run: 이미지를 기반으로 컨테이너를 실행합니다.예:여기서 -d는 백그라운드에서 실행, -p는 포트 매핑을 의미합니다.
     
    docker run -d -p 8080:80 nginx
    docker run <옵션> <이미지 이름>

  • docker ps: 실행 중인 컨테이너를 확인합니다.실행 중인 모든 컨테이너를 표시합니다. docker ps -a는 종료된 컨테이너도 포함하여 모든 컨테이너를 나열합니다.
     
    docker ps
  • docker stop: 실행 중인 컨테이너를 중지합니다.
     
    docker stop <컨테이너 ID or 이름>
  • docker start: 정지된 컨테이너를 시작합니다.
     
    docker start <컨테이너 ID or 이름>
  • docker restart: 컨테이너를 재시작합니다.
     
    docker restart <컨테이너 ID or 이름>
  • docker exec: 실행 중인 컨테이너 안에서 명령어를 실행합니다.예를 들어, bash 쉘을 실행하려면:
     
    docker exec -it <컨테이너 ID or 이름> bash
    docker exec -it <컨테이너 ID or 이름> <명령어>
  • docker rm: 중지된 컨테이너를 삭제합니다.
     
    docker rm <컨테이너 ID or 이름>
  • docker logs: 실행 중인 컨테이너의 로그를 확인합니다.
    docker logs <컨테이너 ID or 이름>

3. 기타 유용한 명령어

  • docker info: Docker의 시스템 정보를 표시합니다.
     
    docker info
  • docker version: Docker의 버전 정보를 표시합니다.
     
    docker version

이 외에도 Docker는 많은 옵션을 제공하며, 사용 시 docker <명령어> --help를 통해 각 명령어에 대한 자세한 옵션을 확인할 수 있습니다.

# docker --help 

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Common Commands:
  run         Create and run a new container from an image
  exec        Execute a command in a running container
  ps          List containers
  build       Build an image from a Dockerfile
  pull        Download an image from a registry
  push        Upload an image to a registry
  images      List images
  login       Log in to a registry
  logout      Log out from a registry
  search      Search Docker Hub for images
  version     Show the Docker version information
  info        Display system-wide information

Management Commands:
  builder     Manage builds
  buildx*     Docker Buildx (Docker Inc., v0.0.0+unknown)
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  plugin      Manage plugins
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Swarm Commands:
  swarm       Manage Swarm

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  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
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  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
  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
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  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
  wait        Block until one or more containers stop, then print their exit codes

Global Options:
      --config string      Location of client config files (default "/root/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket 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

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

For more help on how to use Docker, head to https://docs.docker.com/go/guides/
 

4. 추가 AWS, Azuer, GCP 이미지 레지스트리 사용방법 

(1) Azure Contianer Registry(ACR)로 푸시

1. ACR에 Docker 클라이언트로 로그인해야 합니다.

az acr login --name <ACR_이름>
az acr login --name myacr12345


2. 이미지 태깅 
 
docker tag my-image <ACR_이름>.azurecr.io/my-repo:latest
docker tag my-image myacr12345.azurecr.io/my-repo:latest
 
3. 이미지 푸시 
docker push <ACR_이름>.azurecr.io/my-repo:latest
docker push myacr12345.azurecr.io/my-repo:latest
 

(2) AWS Elastic Container Registry(ECR)로 푸시

  1. AWS ECR 로그인
     
    aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin <AWS_ACCOUNT_ID>.dkr.ecr.ap-northeast-2.amazonaws.com
     
  2. 이미지 태깅
     
    docker tag my-image <AWS_ACCOUNT_ID>.dkr.ecr.ap-northeast-2.amazonaws.com/my-repo:latest
     
  3. 이미지 푸시
     
    docker push <AWS_ACCOUNT_ID>.dkr.ecr.ap-northeast-2.amazonaws.com/my-repo:latest

(3) Google Container Registry(GCR)로 푸시

  1. GCR 로그인
     
    gcloud auth configure-docker
     
  2. 이미지 태깅
     
    docker tag my-image gcr.io/my-project/my-image:latest
     
  3. 이미지 푸시
     
    docker push gcr.io/my-project/my-image:latest

(4) Private Registry로 푸시

  1. Private Registry 로그인
     
    docker login myregistry.com
     
  2. 이미지 태깅
     
    docker tag my-image myregistry.com/myrepo/my-image:latest
     
  3. 이미지 푸시
     
    docker push myregistry.com/myrepo/my-image:latest
반응형

'Cloud' 카테고리의 다른 글

Docker 간단한 이미지 생성 및 서비스 노출 테스트  (1) 2025.02.05

댓글