공부/Docker

도커 이미지

HY13 2021. 2. 15. 18:09

도커 이미지

- 도커 컨테이너를 만들기 위한 템플릿

- 운영 체제로 구성된 파일 시스템, 컨테이너 위에서 실행하기 위한 애플리케이션, 라이브러리, 실행 환경의 설정 정보를 포함하는 아카이브

 

 // nginx 이미지 다시 다운
[root@localhost ~]#
 docker image pull nginx     
       
// 이미지의 필요한 정보만 검색 
[root@localhost ~]# docker image inspect nginx --format="{{.Os}}"          
linux

// 도커 커밋 버전
[root@localhost ~]# docker image inspect nginx --format="{{.DockerVersion}}"     
19.03.12



// create 명령어를 이용해 임의로 컨테이너 생성
// create에서는 d옵션이 붙지 않는다
[root@localhost ~]# docker container create --name webserver -p 8080:80 nginx d6357a75dc347b6e32b8c89f72db0375cc8888823eed1b572a8033296c55f162


[root@localhost ~]# docker container start webserver
webserver

=> docker run = docker create + docker start


 

 

 

 

도커 이미지 생성 

 
// webserver 컨테이너를 web01이라는 이름의 이미지로 생성
[root@localhost ~]# docker container commit webserver web01:v1.0       
sha256:d0970224581282ec34ad6093561ee43a4e31955772c4d97fe35148ff22545d9b

// 생성한 이미지를 도커 허브에 올리기 위해 tag를 달아준다 (hyeyeon9813은 내 도커허브 계정)
[root@localhost ~]# docker image tag web01:v1.0 hyeyeon9813/web01:v1.0


// 이미지 생성 확인 - web01이미지와 hyeyeon9813/web01이라는 별칭의 이미지 생성
[root@localhost ~]# docker image ls
REPOSITORY                TAG                 IMAGE ID                CREATED                SIZE
hyeyeon9813/web01      v1.0                 d09702245812         48 seconds ago        133 MB
web01                        v1.0                 d09702245812         48 seconds ago        133 MB
docker.io/nginx             latest               298ec0e28760          6 days ago              133 MB


// 도커 자격증명을 위한 로그인
[root@localhost ~]# docker login                                              
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: hyeyeon9813
Password:
Login Succeeded

// 도커허브에 업로드 (**태그 꼭 붙여주기)
[root@localhost ~]# docker image push hyeyeon9813/web01:v1.0
The push refers to a repository [docker.io/hyeyeon9813/web01]
58bfe7dd4689: Pushed
d9eb91d66e2a: Mounted from library/nginx
ae1f545e4c08: Mounted from library/nginx
c20672db3628: Mounted from library/nginx
4cbb728cd302: Mounted from library/nginx
9eb82f04c782: Mounted from library/nginx
v1.0: digest: sha256:eead984571de4dc8a2c8f2b26ddb657a2e86aec331eea43dac2bd3d3d3468c0d size: 1570


도커 허브

 

 

개발자 - centOS가 아닌 ubuntu 사용중

개발자의 vm을 하나 생성해준다

개발자환경에 도커 설치

johnlee@ubuntu:~$ sudo apt-get update -y
[sudo] password for johnlee:                               
johnlee@ubuntu:~$ sudo apt install docker.io -y         // 도커 설치
johnlee@ubuntu:~$ systemctl status docker
johnlee@ubuntu:~$ sudo apt install -y apache2          // 아파치 서버 설치
johnlee@ubuntu:~$ ss -ant

-> 80포트가 열려있는 모습을 확인할 수 있다


-> 아이피:80 입력하면 접속 된다

// 관리자가 올린 도커허브에서 이미지 가져오기
johnlee@ubuntu:~$ sudo docker image pull hyeyeon9813/web01:v1.0 
v1.0: Pulling from hyeyeon9813/web01 45b42c59be33: Pull complete d0d9e9ea897e: Pull complete 66e650438339: Pull complete
76a3dfe4406b: Pull complete
410ff9d97480: Pull complete
92bb506ee653: Pull complete Digest: sha256:eead984571de4dc8a2c8f2b26ddb657a2e86aec331eea43dac2bd3d3d3468c0d Status: Downloaded newer image for hyeyeon9813/web01:v1.0

// 이미지 확인
johnlee@ubuntu:~$ sudo docker image ls
REPOSITORY                  TAG              IMAGE ID              CREATED                        SIZE
hyeyeon9813/web01        v1.0              d09702245812        Less than a second ago     133MB

// 컨테이너 실행
johnlee@ubuntu:~$ sudo docker container run --name web01 -d -p 8080:80 hyeyeon9813/web01:v1.0 b4cfa6c0b543328830298e6805c033eac8087d1c649213e1a0e839acfb87b7d0

 

 

 

도커 컨테이너 및 이미지 삭제

// 사용하지 않는 이미지 삭제
[root@localhost ~]# docker image prune -a
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y

// 사용중인 이미지 삭제
[root@localhost ~]# docker rmi -f docker.io/nginx
Untagged: docker.io/nginx:latest
Untagged: docker.io/nginx@sha256:8e10956422503824ebb599f37c26a90fe70541942687f70bbdb744530fc9eba4
-> 아직 컨테이너가 실행중이라 제대로 삭제되지 않는다

// 컨테이너 삭제
[root@localhost ~]# docker rm -f webserver
webserver

// 이미지 다시 삭제
[root@localhost ~]# docker rmi -f 298ec0e28760
Deleted: sha256:298ec0e28760b8eb1aad79711dc29c19041c61d7cf342dd1f445e91f30500549
Deleted: sha256:82a80a581c3c25eec11651e31d5637b7c6dacae2f51dc22998b3168697727bab
Deleted: sha256:3e3c87cd680b08d7793d9e5c43117d1d6d2da08bfc1b2d1f1a28555a0224c299
Deleted: sha256:b7639a9a7b8f73c99a76d1dc5777716e230e810a2e737b21b9f36147f69b253b
Deleted: sha256:4d7166647e67b04600ec5b94833e79d28b850433136593ea263fa340f3a54b66
Deleted: sha256:9eb82f04c782ef3f5ca25911e60d75e441ce0fe82e49f0dbf02c81a3161d1300
-> 이제 이미지가 제대로 삭제되었다.

 

 

 

도커 컨테이너 생성 및 시작

// 여러개의 컨테이너 생성
[root@localhost ~]# docker run --name web01 -d -p 8080:80 hyeyeon9813/web01:v1.0
[root@localhost ~]# docker run --name web02 -d -p 8282:80 hyeyeon9813/web01:v1.0
[root@localhost ~]# docker run --name web03 -d -p 8383:80 hyeyeon9813/web01:v1.0

// 컨테이너 이름 없이 생성
[root@localhost ~]# docker run -d -p 8181:80 hyeyeon9813/web01:v1.0
[root@localhost ~]# docker run -d -p 8484:80 hyeyeon9813/web01:v1.0

[root@localhost ~]# docker ps
->이름을 지정하지 않고 컨테이너를 생성하면 컨테이너 이름이 임의로 생성된다


// 모든 컨테이너 실행 중지
[root@localhost ~]# docker stop $(docker ps -q)

// 모든 컨테이너 시작 
[root@localhost ~]# docker start $(docker ps -aq)

// 컨테이너 필터링 (f 옵션)
[root@localhost ~]# docker ps -a -f name=web02

// web01이름의 컨테이너 중지
[root@localhost ~]# docker stop web01
web01

// 멈춰있는 컨테이너 확인
[root@localhost ~]# docker ps -a -f exited=0

 

 

일회성 용도의 컨테이너

[root@localhost ~]# docker run -it --name test_cal centos /bin/cal
-> 현재 centos라는 이미지가 없으므로 다운먼저 받아진다.
-> /bin/cal을 실행하고 컨테이너 멈춤
(자신의 기능만 하고 멈춤)

[root@localhost ~]# docker run -it centos /bin/bash
[root@3218272cc904 /]# ping 8.8.8.8
-> ping이 나간다
컨테이너 안에서 ping외에도 몇개의 명령어를 사용할 수 있다.

 

 

 

'공부 > Docker' 카테고리의 다른 글

Docker  (0) 2021.02.15