Docker 컨테이너 및 이미지 관리 방법

DockerBeginner
지금 연습하기

소개

Docker 는 애플리케이션 개발, 배포 및 관리 방식을 혁신했습니다. 이 포괄적인 튜토리얼에서는 컨테이너 및 이미지 생성 및 배포부터 모니터링 및 문제 해결까지 Docker 컨테이너를 효과적으로 관리하는 방법을 배웁니다. 초보 사용자이든 숙련된 Docker 사용자이든 이 가이드는 Docker 컨테이너 관리의 기술을 마스터하는 데 필요한 필수적인 기술을 제공합니다.

Introduction to Docker Containers

What is Docker?

Docker is an open-source platform that allows developers to build, deploy, and run applications in a containerized environment. Containers are lightweight, portable, and self-contained units that package an application's code, dependencies, and runtime environment, making it easy to move the application between different computing environments.

Benefits of Docker Containers

  1. Consistency: Docker containers ensure that the application runs the same way, regardless of the underlying infrastructure.
  2. Scalability: Containers can be easily scaled up or down to meet changing demands.
  3. Efficiency: Containers are more lightweight and use fewer resources than traditional virtual machines.
  4. Portability: Docker containers can be run on any system that supports the Docker runtime, making it easy to move applications between different environments.

Docker Architecture

Docker uses a client-server architecture, where the Docker client communicates with the Docker daemon (the server) to perform various operations, such as building, running, and managing containers.

graph LD subgraph Docker Architecture client[Docker Client] daemon[Docker Daemon] registry[Docker Registry] client -- communicate --> daemon daemon -- pull/push --> registry end

Docker Components

  1. Docker Images: Docker images are the building blocks of containers. They contain the application code, dependencies, and runtime environment.
  2. Docker Containers: Containers are the running instances of Docker images. They are isolated, lightweight, and portable.
  3. Docker Registry: Docker Registry is a storage and distribution system for Docker images. The most popular registry is Docker Hub, a public registry provided by Docker.

Getting Started with Docker

To get started with Docker, you'll need to install the Docker runtime on your system. You can download and install Docker from the official Docker website (https://www.docker.com/get-started).

Once you have Docker installed, you can start exploring and working with Docker containers. Let's move on to the next section to learn how to manage Docker images.

Docker 이미지 관리

Docker 이미지 가져오기

레지스트리에서 Docker 이미지를 가져오려면 docker pull 명령어를 사용합니다.

docker pull ubuntu:22.04

이 명령어는 Docker Hub 레지스트리에서 ubuntu:22.04 이미지를 가져옵니다.

Docker 이미지 생성

Dockerfile 을 사용하여 직접 Docker 이미지를 생성할 수 있습니다. Dockerfile 은 이미지 생성을 위한 명령어가 포함된 텍스트 파일입니다. 다음은 예시 Dockerfile 입니다.

FROM ubuntu:22.04
RUN apt-get update && apt-get install -y nginx
COPY index.html /var/www/html/
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

이미지를 생성하려면 다음 명령어를 실행합니다.

docker build -t my-nginx-app .

이 명령어는 my-nginx-app이라는 새 Docker 이미지를 생성합니다.

Docker 이미지 목록

시스템에 있는 모든 Docker 이미지를 목록으로 보려면 docker images 명령어를 사용합니다.

docker images

이 명령어는 각 이미지의 이름, 태그, 이미지 ID, 생성 시간 및 크기 등에 대한 정보를 표시하는 테이블을 출력합니다.

Docker 이미지 태깅

Docker 이미지에 태그를 추가하여 이미지를 정리하고 관리할 수 있습니다. 이미지에 태그를 추가하려면 docker tag 명령어를 사용합니다.

docker tag my-nginx-app:latest my-nginx-app:v1.0

이 명령어는 my-nginx-app 이미지에 v1.0이라는 새 태그를 생성합니다.

Docker 이미지 푸시

Docker 이미지를 생성하고 태깅한 후 docker push 명령어를 사용하여 레지스트리에 푸시할 수 있습니다.

docker push my-nginx-app:v1.0

이 명령어는 기본 레지스트리 (Docker Hub) 에 my-nginx-app:v1.0 이미지를 푸시합니다.

Docker 이미지 삭제

Docker 이미지를 삭제하려면 docker rmi 명령어를 사용합니다.

docker rmi my-nginx-app:v1.0

이 명령어는 시스템에서 my-nginx-app:v1.0 이미지를 삭제합니다.

이제 Docker 이미지 관리 방법을 배웠으니, 다음 섹션에서 Docker 컨테이너를 실행하고 배포하는 방법을 배워보겠습니다.

Docker 컨테이너 실행 및 배포

Docker 컨테이너 실행

Docker 컨테이너를 실행하려면 docker run 명령어를 사용합니다.

docker run -d -p 80:80 my-nginx-app

이 명령어는 my-nginx-app 이미지를 기반으로 새로운 컨테이너를 시작하고 호스트의 80 번 포트를 컨테이너의 80 번 포트로 매핑합니다.

-it 플래그를 사용하여 대화형 컨테이너를 실행할 수도 있습니다.

docker run -it ubuntu:22.04 /bin/bash

이 명령어는 대화형 Ubuntu 컨테이너를 시작하고 컨테이너 내부에서 쉘 프롬프트를 제공합니다.

실행 중인 컨테이너 관리

실행 중인 모든 컨테이너를 목록으로 보려면 docker ps 명령어를 사용합니다.

docker ps

실행 중인 컨테이너를 중지하려면 docker stop 명령어를 사용합니다.

docker stop my-nginx-app

중지된 컨테이너를 시작하려면 docker start 명령어를 사용합니다.

docker start my-nginx-app

컨테이너 로그 액세스

실행 중인 컨테이너의 로그를 보려면 docker logs 명령어를 사용합니다.

docker logs my-nginx-app

이 명령어는 my-nginx-app 컨테이너의 로그를 표시합니다.

컨테이너 내 명령어 실행

실행 중인 컨테이너 내에서 명령어를 실행하려면 docker exec 명령어를 사용합니다.

docker exec -it my-nginx-app /bin/bash

이 명령어는 my-nginx-app 컨테이너 내부에서 대화형 쉘 세션을 시작합니다.

Docker 컨테이너 배포

프로덕션 환경에서 Docker 컨테이너를 배포하려면 Docker Compose 또는 Kubernetes 와 같은 도구를 사용할 수 있습니다. 이러한 도구는 여러 컨테이너와 그 종속성을 관리하고 조정하는 데 도움이 됩니다.

다음은 간단한 Docker Compose 파일의 예입니다.

version: "3"
services:
  web:
    image: my-nginx-app
    ports:
      - "80:80"
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: password

docker-compose up 명령어를 사용하여 이 설정을 배포할 수 있습니다.

Docker Compose 또는 Kubernetes 를 사용하면 컨테이너화된 환경에서 애플리케이션을 쉽게 확장, 관리 및 배포할 수 있습니다.

요약

이 튜토리얼에서는 Docker 컨테이너와 이미지 관리의 기본 사항을 배웠습니다. 이제 Docker 기반 애플리케이션을 구축, 배포 및 유지 관리하는 방법을 알게 되었으며, 이를 통해 애플리케이션이 원활하고 효율적으로 실행될 수 있도록 합니다. Docker 의 기능을 활용하여 개발 및 배포 프로세스를 간소화하고 생산성과 확장성을 높일 수 있습니다. Docker 를 계속 탐색하고 실험하여 기술을 더욱 향상시키고 이 변혁적인 기술의 잠재력을 최대한 발휘하십시오.