1. Dockerfile 실습
# ubuntu 이미지 다운로드
docker pull ubuntu:20.04
docker images
# 실습을 위한 디렉터리 생성 및 이동
mkdir -p /root/myweb && cd /root/myweb
# Dockerfile 파일 생성
vi Dockerfile
FROM ubuntu:20.04
ENV TZ=Asia/Seoul VERSION=1.0.0 NICK=younggi
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \\
sed -i 's/archive.ubuntu.com/mirror.kakao.com/g' /etc/apt/sources.list && \\
sed -i 's/security.ubuntu.com/mirror.kakao.com/g' /etc/apt/sources.list && \\
apt-get update && apt-get install -y apache2 figlet && \\
echo "$NICK Web Server $VERSION<br>" > /var/www/html/index.html && \\
echo "<pre>" >> /var/www/html/index.html && \\
figlet AEWS Study >> /var/www/html/index.html && \\
echo "</pre>" >> /var/www/html/index.html
EXPOSE 80
CMD ["usr/sbin/apache2ctl", "-DFOREGROUND"]
# 이미지 빌드
ls
cat Dockerfile
# 이미지 빌드
docker build -t myweb:v1.0.0 .
docker images
docker image history myweb:v1.0.0
docker image inspect myweb:v1.0.0 | jq
(younggi@myeks:default) [root@myeks-bastion myweb]# docker image inspect myweb:1.0.0 | jq
[
{
"Id": "sha256:58a0c2d76425b76086082f336f6faed270d1d26f794f5650364122c6d313301f",
"RepoTags": [
"myweb:1.0.0"
],
"RepoDigests": [],
"Parent": "sha256:af3c36dfd1b8c0abe33b80c4706911092009873ea3babeeea2649b301950cb67",
"Comment": "",
"Created": "2024-04-20T06:39:20.835069177Z",
"Container": "ab68bb1d583231edb3fa6f579419f7c391ffbbb9ed66221ee8e6221811e86406",
"ContainerConfig": {
"Hostname": "ab68bb1d5832",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"TZ=Asia/Seoul",
"VERSION=1.0.0",
"NICK=younggi"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\\"usr/sbin/apache2ctl\\" \\"-DFOREGROUND\\"]"
],
"Image": "sha256:af3c36dfd1b8c0abe33b80c4706911092009873ea3babeeea2649b301950cb67",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.opencontainers.image.ref.name": "ubuntu",
"org.opencontainers.image.version": "20.04"
}
},
"DockerVersion": "20.10.25",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"TZ=Asia/Seoul",
"VERSION=1.0.0",
"NICK=younggi"
],
"Cmd": [
"usr/sbin/apache2ctl",
"-DFOREGROUND"
],
"Image": "sha256:af3c36dfd1b8c0abe33b80c4706911092009873ea3babeeea2649b301950cb67",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.opencontainers.image.ref.name": "ubuntu",
"org.opencontainers.image.version": "20.04"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 237768915,
"VirtualSize": 237768915,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/d0169e11c7f108e96788fb3345ef8629832731715d9fe108d5515f0464aab328/diff",
"MergedDir": "/var/lib/docker/overlay2/4d2ab34d1f267981dba44c42b2fcb01cb3acfcd48317fbc18cadb20bf1b4ee38/merged",
"UpperDir": "/var/lib/docker/overlay2/4d2ab34d1f267981dba44c42b2fcb01cb3acfcd48317fbc18cadb20bf1b4ee38/diff",
"WorkDir": "/var/lib/docker/overlay2/4d2ab34d1f267981dba44c42b2fcb01cb3acfcd48317fbc18cadb20bf1b4ee38/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:106e8431b412f51ccd75ea46a2d5cb4343b23273cbcf50188377cb93aa9a6d82",
"sha256:504b4f88eacc84b8f31b46dcd0178c7ab91615c8c3f1a0db188b4b2cafd3214b"
]
},
"Metadata": {
"LastTagTime": "2024-04-20T15:39:20.865523397+09:00"
}
}
]
# 컨테이너 실행
docker run -d -p 80:80 --rm --name myweb myweb:v1.0.0
docker ps
curl localhost
# 웹 접속 확인
curl -s ipinfo.io/ip | awk '{ print "myweb = http://"$1"" }'
2. Docker Hub(image Repository)에 image 올리기
도커 허브 가입 - https://hub.docker.com/
# 위에서 만든 이미지의 tag명 변경하기
DHUB=dudrl0944
docker tag myweb:1.0.0 $DHUB/myweb:1.0.0
docker images
# 도커 허브 로그인
docker login
Username: dudrl0944
Password: **********
## 로그인 정보는 /[계정명]/.docker/config.json 에 저장됨. docker logout 시 삭제됨
## cat /root/.docker/config.json | jq
## 로그인 정보는 /[계정명]/.docker/config.json 에 저장됨. docker logout 시 삭제됨
cat /root/.docker/config.json | jq
# push 로 이미지를 저장소에 업로드
docker push $DHUB/myweb:1.0.0
3. DockerHub에 올린 이미지 사용하기
# 기존 로컬의 도커 컨테이너, 이미지 삭제
docker rm -vf $(docker ps -a -q)
docker rmi -f $(docker images -a -q)
docker ps
docker images
# 도커허브에 올린 이미지 pull & run
docker run -d -p 80:80 --rm --name myweb $DHUB/myweb:1.0.0
docker images
# 확인
docker ps
curl localhost
curl -s ipinfo.io/ip | awk '{ print "myweb = http://"$1"" }'
# 리소스 정리
docker rm -vf $(docker ps -a -q)
docker rmi -f $(docker images -a -q)