-
참고: MS Learn을 통해 Docker를 자세히 살펴보시는 것을 추천합니다
-
Docker 설치하기 (Ubuntu 20.04 LTS) 공식 docker.com 설치 안내 링크
# root 계정 전환
sudo su -
# 도커 설치를 위한 저장소 설치
apt-get update && apt-get -y install apt-transport-https ca-certificates curl gnupg lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
cat /etc/apt/sources.list.d/docker.list
# 도커 설치
apt-get update && apt-get -y install docker-ce docker-ce-cli containerd.io
# 도커 정보 확인
docker info
root@Ubuntu:~# docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.6.1-docker)
scan: Docker Scan (Docker Inc., v0.8.0)
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 20.10.8
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: false
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runtime.v1.linux runc io.containerd.runc.v2
Default Runtime: runc
Init Binary: docker-init
containerd version: e25210fe30a0a703442421b0f60afac609f950a3
runc version: v1.0.1-0-g4144b63
init version: de40ad0
Security Options:
apparmor
seccomp
Profile: default
Kernel Version: 5.8.0-1039-azure
Operating System: Ubuntu 20.04.2 LTS
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 7.775GiB
Name: Ubuntu
ID: 7BGZ:YPMR:7GOQ:4HXX:SCKH:M2QD:2DBU:FAOY:W4OL:K2LB:FGL2:GD34
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
# 도커 서비스 상태 확인
systemctl status docker
root@Ubuntu:~# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2021-08-17 01:16:25 UTC; 32s ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 14728 (dockerd)
Tasks: 9
Memory: 32.9M
CGroup: /system.slice/docker.service
└─14728 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
...
# 모든 서비스의 상태 표시 - 링크
systemctl list-units --type=service
# 도커 루트 디렉터리 확인 (tree 명령어가 없으면 "apt install tree"로 설치)
tree -L 3 /var/lib/docker
root@Ubuntu:~# tree -L 3 /var/lib/docker
/var/lib/docker
├── buildkit
│ ├── cache.db
│ ├── containerdmeta.db
│ ├── content
│ │ └── ingest
│ ├── executor
│ ├── metadata_v2.db
│ └── snapshots.db
├── containers
├── image
│ └── overlay2
│ ├── distribution
│ ├── imagedb
│ ├── layerdb
│ └── repositories.json
├── network
│ └── files
│ └── local-kv.db
├── overlay2
│ └── l
├── plugins
│ ├── storage
│ │ └── ingest
│ └── tmp
├── runtimes
├── swarm
├── tmp
├── trust
└── volumes
├── backingFsBlockDev
└── metadata.db
23 directories, 8 files
-
컨테이너 실행 및 확인 (docker/whalesay 컨테이너 사용, 참조 링크)
- 실행 명령어:
docker run <IMAGE>:<TAG> [<args>]
- 실행 명령어:
# cowsay 그냥 호출에서 사용해보기
apt install cowsay -y
cowsay <넣고 싶은 메시지!>
# 도커 이미지 정보 확인
docker images
# 도커 컨테이너 리스트 확인
docker ps
docker ps -a
# cowsay 이름의 컨테이너를 실행
# args(전달할 인자)에 cowsay 'Hello, Docker!' 지정했습니다
docker run docker/whalesay cowsay '<넣고 싶은 메시지!>'
root@Ubuntu:~# docker run docker/whalesay cowsay 'Hello, Docker!'
Unable to find image 'docker/whalesay:latest' locally
latest: Pulling from docker/whalesay
Image docker.io/docker/whalesay:latest uses outdated schema1 manifest format. Please upgrade to a schema2 image for better future compatibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/
e190868d63f8: Pull complete
909cd34c6fd7: Pull complete
0b9bfabab7c1: Pull complete
a3ed95caeb02: Pull complete
00bf65475aba: Pull complete
c57b6bcc83e3: Pull complete
8978f6879e2f: Pull complete
8eed3712d2cf: Pull complete
Digest: sha256:178598e51a26abbc958b8a2e48825c90bc22e641de3d31e18aaf55f3258ba93b
Status: Downloaded newer image for docker/whalesay:latest
________________
< Hello, Docker! >
----------------
\
\
\
## .
## ## ## ==
## ## ## ## ===
/""""""""""""""""___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\______/
# 도커 이미지 정보 확인
docker images
# 도커 컨테이너 리스트 확인 : STATUS(상태)가 Exited 코드(0)으로 정상 종료
docker ps
docker ps -a
- 도커 이미지 주소 형식:
<레지스트리 이름>/<이미지 이름>:<TAG>
- 레지스트리 주소: 도메인 주소 형식을 가지며, 생략시 도커 허브(Docker Hub, docker.io) 레지스트리를 사용
- 기본 사용 TAG: latest
docker/whalesay == docker.io/docker/whalesay:latest
# 전체 주소로 이미지 다운로드
docker info |grep Registry
docker images
docker pull docker.io/docker/whalesay:latest
docker pull index.docker.io/docker/whalesay:latest
docker images
- 중지된 컨테이너 삭제
docker ps
docker ps -a
docker ps -a -q
docker rm $(docker ps -a -q)
docker ps -a
- Dockerfile 작성해보기
# 테스트 파일 생성
mkdir dockerfile && cd dockerfile
echo 'Test Web page' > test.html
ls
FROM ubuntu:14.04
LABEL maintainer "test<[email protected]>"
LABEL "purpose"="practice"
RUN sed -i 's/archive.ubuntu.com/ftp.daum.net/g' /etc/apt/sources.list
RUN apt-get update
RUN apt-get install apache2 figlet -y
ADD test.html /var/www/html
WORKDIR /var/www/html
RUN ["/bin/bash", "-c", "echo Hello, Dockerfile! | figlet > /var/www/html/index.html"]
EXPOSE 80
CMD apachectl -DFOREGROUND
cat <<EOF> Dockerfile
FROM ubuntu:14.04
LABEL maintainer "test<[email protected]>"
LABEL "purpose"="practice"
RUN sed -i 's/archive.ubuntu.com/ftp.daum.net/g' /etc/apt/sources.list
RUN apt-get update
RUN apt-get install apache2 figlet -y
ADD test.html /var/www/html
WORKDIR /var/www/html
RUN ["/bin/bash", "-c", "echo Hello, Dockerfile! | figlet > /var/www/html/index.html"]
EXPOSE 80
CMD apachectl -DFOREGROUND
EOF
# -t 생성될 이미지 이름 , 명령어 끝에 Dockerfile 이 저장된 경로(외부 경로 가능)를 입력
docker build -t hello:1 ./
# 생성된 이미지 확인
docker images |egrep '(hello|ubuntu)'
root@Ubuntu:~/dockerfile# docker images |egrep '(hello|ubuntu)'
hello 1 301aba09e870 8 seconds ago 222MB
ubuntu 14.04 13b66b487594 4 months ago 196MB
# 이미지 레이어 비교 (jq가 없을 경우 "apt install jq"로 설치)
root@Ubuntu:~/dockerfile# docker inspect ubuntu:14.04 | jq '.[0] | {RootFS}'
{
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:f2fa9f4cf8fd0a521d40e34492b522cee3f35004047e617c75fadeb8bfd1e6b7",
"sha256:30d3c4334a2379748937816c01f5c972a8291a5ccc958d6b33d735457a16196e",
"sha256:83109fa660b2ed9307948505abd3c1f24c27c64009691067edb765bd3714b98d"
]
}
}
root@ubuntu20:~/dockerfile# docker inspect hello:1 | jq '.[0] | {RootFS}'
{
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:f2fa9f4cf8fd0a521d40e34492b522cee3f35004047e617c75fadeb8bfd1e6b7",
"sha256:30d3c4334a2379748937816c01f5c972a8291a5ccc958d6b33d735457a16196e",
"sha256:83109fa660b2ed9307948505abd3c1f24c27c64009691067edb765bd3714b98d",
"sha256:50d08d9ad8af9cc66733acf3e3811abcae03255e44d924c2ae08ea8556a50134",
"sha256:88f69a005fbc3b11b4f3bde8eeb359dc4e0f19bd1dace4764424fdd43cbee7ab",
"sha256:d05a065cd3f343312bb32520ac7f0c15156f24f4bcd022095d57eefc9d9d579f",
"sha256:c250b0fb5fbe2b8f50b3eb0b04c2985a8d70ce46ac46d382d714d8e113606ebf",
"sha256:5184da536529a0209f3c7c4de91d01f81becca64f0f1ab644d07ba7454432bd1"
]
}
}
root@Ubuntu:~/dockerfile# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
hello 1 301aba09e870 About a minute ago 222MB
<none> <none> 6839aa2a05cb About a minute ago 222MB
<none> <none> c33062f2c717 About a minute ago 222MB
<none> <none> f04344f02c37 About a minute ago 222MB
<none> <none> 56cb53822d83 About a minute ago 222MB
<none> <none> 2a968d73b2e8 2 minutes ago 222MB
<none> <none> fbefb1c84173 2 minutes ago 210MB
<none> <none> b1b696221557 2 minutes ago 196MB
<none> <none> 7a1f5f29cc02 2 minutes ago 196MB
<none> <none> 903799205b66 2 minutes ago 196MB
ubuntu 14.04 13b66b487594 4 months ago 196MB
# hello:1 이미지를 webserver1라는 이름으로 컨테이너 실행
# '-p 외부에_공개할_포트번호:컨테이너내에서_사용하는_포트번호' 를 지정하면 컨테이너 내 포트를 호스트의 IP 주소상의 포트번호와 매핑
docker run -d -p 8080:80 --name webserver1 hello:1
# 확인
root@Ubuntu:~/dockerfile# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
830966e99621 hello:1 "/bin/sh -c 'apachec…" 8 seconds ago Up 7 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp webserver1
# 웹 액세스 확인
root@Ubuntu:~/dockerfile# curl localhost:8080
_ _ _ _ ____ _ __ _ _ _
| | | | ___| | | ___ | _ \ ___ ___| | _____ _ __ / _(_) | ___| |
| |_| |/ _ \ | |/ _ \ | | | |/ _ \ / __| |/ / _ \ '__| |_| | |/ _ \ |
| _ | __/ | | (_) | | |_| | (_) | (__| < __/ | | _| | | __/_|
|_| |_|\___|_|_|\___( ) |____/ \___/ \___|_|\_\___|_| |_| |_|_|\___(_)
|/
root@Ubuntu:~/dockerfile# curl localhost:8080/test.html
Test Web page
- 이미지 태그
# 기존의 이미지에서 새로운 이름을 부여
# docker tag [기존의 이미지 이름] [새롭게 생성될 이미지 이름]
docker tag hello:1 temphello:1
root@Ubuntu:~/dockerfile# docker tag hello:1 temphello:1
# 확인 시 기존의 이미지와 ID가 똑같음!
root@Ubuntu:~/dockerfile# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello 1 301aba09e870 15 minutes ago 222MB
temphello 1 301aba09e870 15 minutes ago 222MB
ubuntu 14.04 13b66b487594 4 months ago 196MB
# 자신의 도커 저장소에 업로드 하기 위해 새로운 이름 부여
# 이미지 이름의 접두어는 이미지가 저장되는 저장소 이름으로 설정해야되어 docker tag 로 이미지의 이름을 추가
docker tag hello:1 <도커 허브 계정>/helloweb:1
root@Ubuntu:~/dockerfile# docker tag hello:1 ianychoi/helloweb:1
# 확인
root@Ubuntu:~/dockerfile# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ianychoi/helloweb 1 301aba09e870 15 minutes ago 222MB
hello 1 301aba09e870 15 minutes ago 222MB
temphello 1 301aba09e870 15 minutes ago 222MB
ubuntu 14.04 13b66b487594 4 months ago 196MB
- 도커 허브 로그인
docker login
root@Ubuntu:~/dockerfile# docker login
Username: <자신의 ID>
Password: <암호>
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
...
Login Succeeded
# 로그인 정보는 /[계정명]/.docker/config.json 에 저장됨. docker logout 시 삭제됨
root@Ubuntu:~/dockerfile# cat /root/.docker/config.json
{
"auths": {
"https://index.docker.io/v1/": {
"auth": "-----(your auth key)-----"
}
}
- 자신 계정의 도커 허브 저장소에 업로드 후 도커 허브 저장소에서 확인
# push 로 이미지를 저장소에 업로드
docker push <도커 허브 계정>/helloweb:1
root@Ubuntu:~/dockerfile# docker push ianychoi/helloweb:1
The push refers to repository [docker.io/ianychoi/helloweb]
5184da536529: Pushed
c250b0fb5fbe: Pushed
d05a065cd3f3: Pushed
88f69a005fbc: Pushed
50d08d9ad8af: Pushed
83109fa660b2: Mounted from library/ubuntu
30d3c4334a23: Mounted from library/ubuntu
f2fa9f4cf8fd: Mounted from library/ubuntu
1: digest: sha256:56022d1e78afc1539c3442ce97db6eb1dcf746145f552c4aefff22392d0e2f31 size: 1989
- 도커 저장소 이미지 사용을 위한 리소스 삭제
# 삭제
docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q) -f && docker rmi $(docker images -q) -f
# 확인
docker ps -a
docker images
- 컨테이너 실행
# 이미지가 로컬에 없으니 도커 저장소에서 다운받아서 컨테이너 실행
docker run -d -p 8080:80 --name webserver <도커 허브 계정>/helloweb:1
root@Ubuntu:~/dockerfile# docker run -d -p 8080:80 --name webserver ianychoi/helloweb:1
Unable to find image 'ianychoi/helloweb:1' locally
1: Pulling from ianychoi/helloweb
2e6e20c8e2e6: Pull complete
0551a797c01d: Pull complete
512123a864da: Pull complete
0c3e3eba706f: Pull complete
6f02ee9cd865: Pull complete
13c64630962a: Pull complete
eea8b9d71ffa: Pull complete
ae327a094398: Pull complete
Digest: sha256:56022d1e78afc1539c3442ce97db6eb1dcf746145f552c4aefff22392d0e2f31
Status: Downloaded newer image for ianychoi/helloweb:1
6bde47bf153186e4a7e0e20565c0ba9c55d0d8fd90ad18144c9cec9f779e449e
# 확인
curl localhost:8080
root@Ubuntu:~/dockerfile# curl localhost:8080
_ _ _ _ ____ _ __ _ _ _
| | | | ___| | | ___ | _ \ ___ ___| | _____ _ __ / _(_) | ___| |
| |_| |/ _ \ | |/ _ \ | | | |/ _ \ / __| |/ / _ \ '__| |_| | |/ _ \ |
| _ | __/ | | (_) | | |_| | (_) | (__| < __/ | | _| | | __/_|
|_| |_|\___|_|_|\___( ) |____/ \___/ \___|_|\_\___|_| |_| |_|_|\___(_)
|/
# 리소스 삭제
docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q) -f && docker rmi $(docker images -q) -f