본문 바로가기
요즘공부

[도커&쿠버네티스] 교육 3일차_ Docker Hub Upload

by 게으른 피글렛 2025. 3. 17.
반응형

1. Docker Hub Upload

python으로 로또파일 생성 > 도커이미지 > 도커허브에 생성

 

우선 기존 도커 이미지 중 안쓰는 것 정리

docker rmi -f $(docker images | grep -v -Ev '(mariadb|centos|mysql|redis|httpd|mysql|nginx)' | awk '{print $3}' | grep -v IMAGE)

 

< python으로 로또파일 생성 >

파이썬 파일은 *.py 로 생성해야함

로또파일은 google에 파이썬로또로 검색하면 많은 사람들이 생성해 둔 파일이 나옴

그중 아무거나 마음에 드는 것을 고름

 

vagrant@ubuntu2204:~/work$ vi lotto.py
vagrant@ubuntu2204:~/work$ cat lotto.py
import random

result = []
while len(result) < 6:
    num = random.randint(1, 45)  # 1~45 사이의 숫자중 임의의 숫자 생성
    if num not in result:  # 중복 숫자 뽑기 방지
        result.append(num)

print(result)  # 무작위 생성된 6개의 숫자 출력

 

실행하면 이렇게 나옴

python3는 설치없이 실행됨

vagrant@ubuntu2204:~/work$ python3 lotto.py
[33, 31, 40, 28, 26, 7]

 

< 도커이미지 >

Dockerfile로 이미지를 생성할 예정

참고 : https://hub.docker.com/_/python

 

관련자료 참고하여 도커파일 생성

vagrant@ubuntu2204:~/work$ vi Dockerfile
vagrant@ubuntu2204:~/work$ cat Dockerfile
FROM python:3
# Python 3 기반의 이미지를 사용합니다.

WORKDIR /usr/src/app
# 컨테이너 내 작업 디렉토리를 /usr/src/app으로 설정합니다.

COPY lotto.py .
# 로컬 시스템의 /home/vagrant/work/lotto.py 파일만 컨테이너 내의 현재 디렉토리로 복사합니다.

CMD ["python", "./lotto.py"]
# 컨테이너 실행 시 lotto.py 파일을 python으로 실행합니다.

 

도커 이미지 생성됨

vagrant@ubuntu2204:~/work$ docker images
REPOSITORY              TAG       IMAGE ID       CREATED         SIZE
mylotto                 latest    153962352436   2 minutes ago   1.02GB

 

docker run을 하면 실행이됨 

vagrant@ubuntu2204:~/work$ docker run mylotto
[32, 9, 13, 24, 23, 29]

 

성공

 

 

 

< 도커허브에 upload >

 

docker 로그인 

vagrant@ubuntu2204:~/work$ docker login -u piglet8
Password:
WARNING! Your password will be stored unencrypted in /home/vagrant/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credential-stores

Login Succeeded

 

로그인을 하면 경고메세지가 뜸

WARNING! Your password will be stored unencrypted in /home/vagrant/.docker/config.json

경고! 너의 패스워드가 /home/vagrant/.docker/config.json 안에 암호화되지 않고 저장될겁니다.

 

vagrant@ubuntu2204:~/.docker$ cat config.json
{
        "auths": {
                "https://index.docker.io/v1/": {
                        "auth": "cGlnbGV0ODpmaWtlcGFzc3dvcmQ="
                }
        }

 

이 파일안에 들어가면 이렇게 뜸

 

vagrant@ubuntu2204:~/.docker$ echo 'cGlnbGV0ODpmaWtlcGFzc3dvcmQ=' | base64 -d

이렇게 명령어를 쳐보면 Base64 인코딩된 문자열을 디코딩된 개인의 아이디와 비밀번호를 알 수 있음

반드시 사용후 docker logout을 해줘야함

 

docker 이미지생성 

vagrant@ubuntu2204:~/.docker$ docker tag mylotto:latest piglet8/mylotto:latest
vagrant@ubuntu2204:~/.docker$ docker images
REPOSITORY              TAG       IMAGE ID       CREATED          SIZE
mylotto                 latest    153962352436   17 minutes ago   1.02GB
piglet8/mylotto         latest    153962352436   17 minutes ago   1.02GB

 

docker push

push는 도커 허브에 올리는 명령어

vagrant@ubuntu2204:~/.docker$ docker push piglet8/mylotto
Using default tag: latest
The push refers to repository [docker.io/piglet8/mylotto]
8dc23a7525e1: Pushed
bc319fc295fb: Pushed
fb8d481c6b59: Pushed
93bc0edf8c04: Pushed
462728c2d5e8: Pushed
4b017a36fd9c: Pushed
20a9b386e10e: Pushed
f8217d7865d2: Pushed
01c9a2a5f237: Pushed
latest: digest: sha256:d4d147a225b3b8b11167c73a922d6edad42add4ff2b5c42a4df2e6c19ca3a829 size: 2210

 

 

도커허브에 이렇게 저장이됨

(오류로 다시 함 그래서 mylotto2가됨)

 

 

docker pull 은 내려 받는 명령어

vagrant@ubuntu2204:~/.docker$ docker pull piglet8/mylotto2
Using default tag: latest
latest: Pulling from piglet8/mylotto2
Digest: sha256:d4d147a225b3b8b11167c73a922d6edad42add4ff2b5c42a4df2e6c19ca3a829
Status: Downloaded newer image for piglet8/mylotto2:latest
docker.io/piglet8/mylotto2:latest

 

내려받아짐

vagrant@ubuntu2204:~/.docker$ docker images piglet8/mylotto2
REPOSITORY           TAG       IMAGE ID       CREATED          SIZE
piglet8/mylotto2   latest    153962352436   42 minutes ago   1.02GB

 

테스트도 성공!

vagrant@ubuntu2204:~/.docker$ docker run piglet8/mylotto2
[42, 28, 5, 30, 40, 26]

 

 

도커 로그아웃!

vagrant@ubuntu2204:~$ docker logout
Removing login credentials for https://index.docker.io/v1/

 

반응형