지난 글에서는 도커 설치 방법에 대해서 소개해드렸고 오늘은 도커 허브에서 이미지를 다운로드하여서 컨테이너를 실행하는 방법을 정리해보고자 합니다.
도커 이미지 찾기
먼저, 이미지는 Docker Hub 사이트에서 찾아볼 수 있습니다. 예를 들어, CentOS 이미지를 설치하기 위해서 검색해보면 아래 그림과 같이 결과를 확인할 수 있습니다.
이미지를 검색하는 다른 방법은 도커 클라이언트 화면에서 명령어를 사용하는 것입니다.
docker search 이미지 이름
아래 예는 CentOS 이미지를 찾기 위해서 "docker search centos"를 실행한 결과를 보여줍니다.
Rayner-MacBookPro ~/Documents/Projects/docker$ docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 6420 [OK]
ansible/centos7-ansible Ansible on Centos7 132 [OK]
consol/centos-xfce-vnc Centos container with "headless" VNC session… 125 [OK]
jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 117 [OK]
centos/systemd systemd enabled base container. 96 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 86
imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 58 [OK]
tutum/centos Simple CentOS docker image with SSH access 46
kinogmt/centos-ssh CentOS with SSH 29 [OK]
pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag names… 13
guyton/centos6 From official centos6 container with full up… 10 [OK]
nathonfowlie/centos-jre Latest CentOS image with the JRE pre-install… 8 [OK]
centos/tools Docker image that has systems administration… 7 [OK]
drecom/centos-ruby centos ruby 6 [OK]
pivotaldata/centos Base centos, freshened up a little with a Do… 5
darksheer/centos Base Centos Image -- Updated hourly 3 [OK]
pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated wi… 3
mamohr/centos-java Oracle Java 8 Docker image based on Centos 7 3 [OK]
pivotaldata/centos-mingw Using the mingw toolchain to cross-compile t… 3
blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
indigo/centos-maven Vanilla CentOS 7 with Oracle Java Developmen… 1 [OK]
mcnaughton/centos-base centos base image 1 [OK]
pivotaldata/centos7-dev CentosOS 7 image for GPDB development 0
pivotaldata/centos6.8-dev CentosOS 6.8 image for GPDB development 0
smartentry/centos centos with smartentry 0 [OK]
도커 이미지 내려받기
도커 이미지를 다운로드하기 위해서는 "docker pull" 명령어를 사용할 수 있다. 이때 다운로드한 이미지 이름과 필요한 버전을 명기할 수 있습니다. 버전을 명기하지 않으면 기본적으로 latest 가 선택되어 최신 버전으로 다운로드됩니다.
docker pull 이미지 이름[:버전]
아래는 centos 8 버전의 이미지를 다운로드하는 예시입니다. 직접 해보시면 centos 8 이미지의 다운로드 진행 상태를 보실 수 있습니다.
Rayner-MacBookPro ~/Documents/Projects/docker$ docker pull centos:8
8: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:8
도커 이미지 목록 보기
도커 허브 리포지토리로부터 이미지를 다운로드한 후, 로컬 컴퓨터에 저장하고 있는 이미지 리스트를 보는 명령은 "docker images"입니다.
docker images
docker images 명령을 실행하면 이미지 목록에서 방금 다운로드한 centos 이미지를 확인할 수 있습니다. 아래 예시 목록에 나온 wordpress, mysql 등은 이전에 받아두었던 이미지들입니다.
Rayner-MacBookPro ~/Documents/Projects/docker$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wordpress latest 2f339b49b645 9 days ago 550MB
mysql 5.7 5f47254ca581 10 days ago 449MB
nginx latest 298ec0e28760 10 days ago 133MB
centos 8 300e315adb2f 2 months ago 209MB
도커 컨테이너 생성하기
도커 이미지를 실행하기 위한 다음 순서는 컨테이너를 만드는 커맨드인 "docker create"를 실행하는 것입니다. docker create 명령은, 객체지향 프로그래밍에서 클래스를 객체로 인스턴스 화하는 것과 같이, Image를 이용해서 Container 인스턴스를 만드는 역할을 수행합니다.
docker create [옵션] 이미지 이름[:버전]
docker create 명령에 사용할 수 있는 옵션은 매우 많지만 그중에서 많이 사용하는 3가지만 소개하겠습니다. -i와 -t 옵션은 컨테이너와 상호 입출력을 가능하게 하고, --name 옵션을 사용하면 컨테이너 이름을 지정할 수 있습니다.
아래는 centos 8 버전의 이미지를 이용해서 이름이 webserver인 컨테이너를 생성하는 예시입니다.
Rayner-MacBookPro ~/Documents/Projects/docker$ docker create -i -t --name webserver centos:8
2138c6bdec83d2fb8a13387f9125d0a46e774300e2d7d96e1f93554359090ce5
도커 컨테이너 실행하기
방금 만든 도커 컨테이너를 실행하는 명령은 docker start입니다. 추가로 컨테이너를 실행한 후 컨테이너 내로 진입하는 명령은 docker attach입니다.
docker start 컨테이너 이름
docker attach 컨테이너 이름
webserver라는 이름의 cetos 8 컨테이너를 실행합니다. 하지만 docker start 명령만으론 실행된 컨테이너 내부로 진입할 수가 없습니다.
Rayner-MacBookPro ~/Documents/Projects/docker$ docker start webserver
webserver
그래서, 아래와 같이 centos 8을 실행 중이 내부로 진입하기 위해서 docker attach 명령을 사용합니다. 그러면 centos 안으로 접근하여 리눅스 명령을 실행시키거나 서버 로그 등을 확인할 수 있습니다
Rayner-MacBookPro ~/Documents/Projects/docker$ docker attach webserver
[root@2138c6bdec83 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@2138c6bdec83 /]# uname -a
Linux 2138c6bdec83 4.9.93-linuxkit-aufs #1 SMP Wed Jun 6 16:55:56 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@2138c6bdec83 /]#
여기까지가 도커 이미지를 다운로드한 후 컨테이너로 실행하는 과정입니다. 다시 한번 CentOS 8 이미지를 이용하는 과정을 정리해보면 아래와 같습니다.
1. docker pull centos:8
2. docker create -i -t --name webserver centos:8
3. docker start webserver
4. docker attach webserver
'DevOps > Docker' 카테고리의 다른 글
Docker(도커) - docker run 사용법 (0) | 2021.02.20 |
---|---|
Docker(도커) 설치 - Mac (0) | 2021.02.14 |
Docker(도커) 소개 (0) | 2021.02.14 |
댓글