Docker 사용해보기

최승훈·2020년 8월 1일
0

Docker

목록 보기
1/1
post-thumbnail

Docker는 무엇을하는 친구?

도커 공식 사이트

https://www.docker.com/

도커헙 공식 사이트

https://hub.docker.com/

도커에 대한 이해도를 높혀주는 영상.

https://www.youtube.com/watch?v=tPjpcsgxgWc

Docker의 명령어

docker의 명령어는 docker <명령> 형식.

1.search

명령으로 Docker Hub 에서 이미지 검색이 가능함.

커맨드

docker search <이미지이름>

> docker search ubuntu
NAME                                                      DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ubuntu                                                    Ubuntu is a Debian-based Linux operating sys…   11171               [OK]
dorowu/ubuntu-desktop-lxde-vnc                            Docker image to provide HTML5 VNC interface …   451                                     [OK]
rastasheep/ubuntu-sshd                                    Dockerized SSH service, built on top of offi…   246                                     [OK]
consol/ubuntu-xfce-vnc                                    Ubuntu container with "headless" VNC session…   222                                     [OK]
ubuntu-upstart                                            Upstart is an event-based replacement for th…   110                 [OK]
ansible/ubuntu14.04-ansible                               Ubuntu 14.04 LTS with ansible                   98                                      [OK]
neurodebian                                               NeuroDebian provides neuroscience research s…   68                  [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5      ubuntu-16-nginx-php-phpmyadmin-mysql-5          50                                      [OK]
ubuntu-debootstrap                                        debootstrap --variant=minbase --components=m…   44                  [OK]
nuagebec/ubuntu                                           Simple always updated Ubuntu docker images w…   24                                      [OK]
i386/ubuntu                                               Ubuntu is a Debian-based Linux operating sys…   22
1and1internet/ubuntu-16-apache-php-5.6                    ubuntu-16-apache-php-5.6                        14                                      [OK]
1and1internet/ubuntu-16-apache-php-7.0                    ubuntu-16-apache-php-7.0                        13                                      [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10   ubuntu-16-nginx-php-phpmyadmin-mariadb-10       11                                      [OK]
1and1internet/ubuntu-16-nginx-php-5.6                     ubuntu-16-nginx-php-5.6                         8                                       [OK]
1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4         ubuntu-16-nginx-php-5.6-wordpress-4             7                                       [OK]
1and1internet/ubuntu-16-apache-php-7.1                    ubuntu-16-apache-php-7.1                        6                                       [OK]
darksheer/ubuntu                                          Base Ubuntu Image -- Updated hourly             5                                       [OK]
1and1internet/ubuntu-16-nginx-php-7.0                     ubuntu-16-nginx-php-7.0                         4                                       [OK]
pivotaldata/ubuntu                                        A quick freshening-up of the base Ubuntu doc…   4
pivotaldata/ubuntu16.04-build                             Ubuntu 16.04 image for GPDB compilation         2
1and1internet/ubuntu-16-php-7.1                           ubuntu-16-php-7.1                               1                                       [OK]
1and1internet/ubuntu-16-sshd                              ubuntu-16-sshd                                  1                                       [OK]
pivotaldata/ubuntu-gpdb-dev                               Ubuntu images for GPDB development              1
smartentry/ubuntu                                         ubuntu with smartentry                          1                                       [OK]

보통 ubuntu, centos, redis 등 OS나 프로그램 이름을 가진 이미지가 공식 이미지.

Tags 탭을 보면 현재 사용할 수있는 이미지의 버전이 확인가능.
https://hub.docker.com/_/ubuntu?tab=tags

2.pull

pull은 이미지를 받는 커맨드(docker hub에서)

커맨드

docker pull <이미지이름>:<태그>

> docker pull ubuntu:latest
latest: Pulling from library/ubuntu
3ff22d22a855: Pull complete
e7cb79d19722: Pull complete
323d0d660b6a: Pull complete
b7f616834fd0: Pull complete
Digest: sha256:5d1d5407f353843ecf8b16524bc5565aa332e9e6a1297c73a92d3e754b8a636d
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

태그 부분에 latest를 입력하면, 최신버전을 내려받음.

3.images

내려받은 이미지 목록을 출력함.

커맨드

docker images

> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              1e4467b07108        7 days ago          73.9MB
hanamam_web         latest              a53a318354f4        9 months ago        393MB
hanamam_tests       latest              a53a318354f4        9 months ago        393MB
<none>              <none>              294976fa8146        9 months ago        393MB
<none>              <none>              54daab785b46        9 months ago        393MB
<none>              <none>              297ac17aa87b        9 months ago        393MB
node                alpine              59be7d262786        9 months ago        105MB

4.run

run 명령으로 컨테이너 생성이 가능함.

커맨드

docker run <옵션> <이미지 이름> <실행할 파일>

ubuntu 이미지를 컨테이너로 생성 뒤 ubuntu 이미지 안의 /bin/bash를 실행.

> docker run -i -t --name hello ubuntu /bin/bash
root@d182a9085750:/# ls -al
total 56
drwxr-xr-x   1 root root 4096 Aug  1 08:39 .
drwxr-xr-x   1 root root 4096 Aug  1 08:39 ..
-rwxr-xr-x   1 root root    0 Aug  1 08:39 .dockerenv
lrwxrwxrwx   1 root root    7 Jul 20 14:43 bin -> usr/bin
drwxr-xr-x   2 root root 4096 Apr 15 11:09 boot
drwxr-xr-x   5 root root  360 Aug  1 08:39 dev
drwxr-xr-x   1 root root 4096 Aug  1 08:39 etc
drwxr-xr-x   2 root root 4096 Apr 15 11:09 home
lrwxrwxrwx   1 root root    7 Jul 20 14:43 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Jul 20 14:43 lib32 -> usr/lib32
lrwxrwxrwx   1 root root    9 Jul 20 14:43 lib64 -> usr/lib64
lrwxrwxrwx   1 root root   10 Jul 20 14:43 libx32 -> usr/libx32
drwxr-xr-x   2 root root 4096 Jul 20 14:43 media
drwxr-xr-x   2 root root 4096 Jul 20 14:43 mnt
drwxr-xr-x   2 root root 4096 Jul 20 14:43 opt
dr-xr-xr-x 173 root root    0 Aug  1 08:39 proc
drwx------   2 root root 4096 Jul 20 14:57 root
drwxr-xr-x   1 root root 4096 Jul 24 14:38 run
lrwxrwxrwx   1 root root    8 Jul 20 14:43 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Jul 20 14:43 srv
dr-xr-xr-x  12 root root    0 Aug  1 08:39 sys
drwxrwxrwt   2 root root 4096 Jul 20 14:57 tmp
drwxr-xr-x   1 root root 4096 Jul 20 14:43 usr
drwxr-xr-x   1 root root 4096 Jul 20 14:57 var
root@d182a9085750:/#

-i(interactive) -t(Pseudo-tty) 옵션을 활용해 Bash 셸에 입력 및 출력을 할 수 있음.
--name 옵션으로 컨터이너의 이름을 지정할 수 있음 (위에서는 hello) 지정하지 않으면 docker가 알아서 이름을 지정해줌.

5.ps

컨테이너의 목록을 출력하는 명령어.

커맨드

docker ps -a

-a 옵션을 사용하면 정지된 컨테이너 리스트 까지 모두 출력.
옵션을 사용하지 않으면 실행되고 있는 컨테이너만 출력.

❯ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

~
❯ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
2e290bada080        ubuntu              "/bin/bash"              3 minutes ago       Exited (0) 3 minutes ago                        hello2
d182a9085750        ubuntu              "/bin/bash"              7 minutes ago       Exited (0) 3 minutes ago                        hello
6a5682ea2c54        hanamam_web         "docker-entrypoint.s…"   9 months ago        Exited (137) 9 months ago                       hanamam_web_1
0e7334c71670        hanamam_tests       "docker-entrypoint.s…"   9 months ago        Exited (137) 9 months ago                       hanamam_tests_1
0e64c06151da        294976fa8146        "docker-entrypoint.s…"   9 months ago        Exited (0) 9 months ago                         stoic_blackburn
564872e8c1c5        294976fa8146        "docker-entrypoint.s…"   9 months ago        Exited (0) 9 months ago                         nervous_edison
130c5c73418d        297ac17aa87b        "docker-entrypoint.s…"   9 months ago        Exited (0) 9 months ago                         suspicious_varahamihira

6.start

컨터이너를 시작(실행)하는 커맨드

커맨드

docker start <컨테이너이름 or 컨테이너ID>

hello 컨테이너 실행
❯ docker start hello
hello

~
// 실행목록
❯ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d182a9085750        ubuntu              "/bin/bash"         15 minutes ago      Up 3 seconds                            hello

~

7.restart

OS의 재부팅 처럼 컨테이너를 재시작.

커맨드

docker restart <컨테이너이름 or 컨테이너ID>

❯ docker restart hello
hello

~
❯ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d182a9085750        ubuntu              "/bin/bash"         21 minutes ago      Up 2 seconds                            hello

~

8.attach

컨테이너에 접속하는 명령어.

커맨드

docker attach <컨테이너 이름 or 컨테이너ID>

❯ docker attach hello
root@d182a9085750:/#

hello컨테이너에서는 /bin/bash를 실행하고있기 때문에 명령을 자유롭게 입력 가능.
DB나 서버 애플리케이션을 실행하면 입력은 할 수 없고 출력만 보게 됨.

Bash셸에서 exit 또는 ctrl+D를 입력하게 되면 컨테이너가 정지됨.
ctrl + P , ctrl + Q 를 차례대로 입력하면 컨테이너를 정지하지 않고, 컨테이너에서 빠져나옴.

8.exec

외부에서 컨테이너 안의 명령을 실행.

커맨드

docker exec <컨테이너이름 or 컨테이너ID> <명령> <매개 변수>

// hello 컨테이너가 기동하지 않은 상태에서 exec를 실행해서 에러..
❯ docker exec hello echo "Hello World"
Error response from daemon: Container d182a9085750785a9df52082c06248156bba9aacb8526d833bdc866bd42b9975 is not running
~
// ps 명령어로 실행중인 컨테이너 리스트 출력.
❯ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

~
// hello 컨테이너 실행
❯ docker start hello
hello

~
❯ docker exec hello echo "Hello World"
Hello World

이미 실행된 컨테이너에 apt-get, yum 명령으로 패키지를 설치하거나, 각종 데몬을 실행할 때 활용 가능.

9.stop

컨테이너를 정지하는 커맨드.

커맨드

docker stop <컨테이너이름 or 컨테이너ID>

~
// 실행중인 컨테이너 목록을 출력
❯ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d182a9085750        ubuntu              "/bin/bash"         37 minutes ago      Up 5 minutes                            hello

~
// storp 컨테이너 정지
❯ docker stop hello
hello

~
// 실행중인 컨테이너 목록을 출력 hello 컨테이너가 목록에서 사라짐.
❯ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

~
❯

10.rm

컨테이너를 삭제하는 커맨드.

커맨드

docker rm <컨테이너이름 or 컨테이너ID>

// 전체 도커 컨테이너 목록 출력 
❯ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
2e290bada080        ubuntu              "/bin/bash"              35 minutes ago      Exited (0) 26 minutes ago                       hello2
d182a9085750        ubuntu              "/bin/bash"              40 minutes ago      Exited (0) 2 minutes ago                        hello
6a5682ea2c54        hanamam_web         "docker-entrypoint.s…"   9 months ago        Exited (137) 9 months ago                       hanamam_web_1
0e7334c71670        hanamam_tests       "docker-entrypoint.s…"   9 months ago        Exited (137) 9 months ago                       hanamam_tests_1
0e64c06151da        294976fa8146        "docker-entrypoint.s…"   9 months ago        Exited (0) 9 months ago                         stoic_blackburn
564872e8c1c5        294976fa8146        "docker-entrypoint.s…"   9 months ago        Exited (0) 9 months ago                         nervous_edison
130c5c73418d        297ac17aa87b        "docker-entrypoint.s…"   9 months ago        Exited (0) 9 months ago                         suspicious_varahamihira

~
// hello2 컨테이너 삭제
❯ docker rm hello2
hello2

~
// 전체 도커 컨테이너 목록 출력 (hello2컨테이너가 사라짐..) 
❯ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
d182a9085750        ubuntu              "/bin/bash"              40 minutes ago      Exited (0) 2 minutes ago                        hello
6a5682ea2c54        hanamam_web         "docker-entrypoint.s…"   9 months ago        Exited (137) 9 months ago                       hanamam_web_1
0e7334c71670        hanamam_tests       "docker-entrypoint.s…"   9 months ago        Exited (137) 9 months ago                       hanamam_tests_1
0e64c06151da        294976fa8146        "docker-entrypoint.s…"   9 months ago        Exited (0) 9 months ago                         stoic_blackburn
564872e8c1c5        294976fa8146        "docker-entrypoint.s…"   9 months ago        Exited (0) 9 months ago                         nervous_edison
130c5c73418d        297ac17aa87b        "docker-entrypoint.s…"   9 months ago        Exited (0) 9 months ago                         suspicious_varahamihira

~

11.rmi

pull 받았던 이미지를 삭제하는 명령어.

커맨드

docker rmi <이미지이름>:<태그>

// ubuntu 이미지를 삭제하려했으나, 아직 쓰고있는 컨테이너가 있다고 안된다고 혼남.
❯ docker rmi ubuntu
Error response from daemon: conflict: unable to remove repository reference "ubuntu" (must force) - container d182a9085750 is using its referenced image 1e4467b07108

// 아직 hello 컨테이너에서 ubuntu이미지를 쓰고 있는 것을 확인.
~
❯ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
d182a9085750        ubuntu              "/bin/bash"              45 minutes ago      Exited (0) 7 minutes ago                        hello
6a5682ea2c54        hanamam_web         "docker-entrypoint.s…"   9 months ago        Exited (137) 9 months ago                       hanamam_web_1
0e7334c71670        hanamam_tests       "docker-entrypoint.s…"   9 months ago        Exited (137) 9 months ago                       hanamam_tests_1
0e64c06151da        294976fa8146        "docker-entrypoint.s…"   9 months ago        Exited (0) 9 months ago                         stoic_blackburn
564872e8c1c5        294976fa8146        "docker-entrypoint.s…"   9 months ago        Exited (0) 9 months ago                         nervous_edison
130c5c73418d        297ac17aa87b        "docker-entrypoint.s…"   9 months ago        Exited (0) 9 months ago                         suspicious_varahamihira

// hello 컨테이너에서 사요나라.
~
❯ docker rm hello
hello

// ubuntu이미지를 쓰고 있는 컨테이너가 없는것을 확인.
~
❯ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
6a5682ea2c54        hanamam_web         "docker-entrypoint.s…"   9 months ago        Exited (137) 9 months ago                       hanamam_web_1
0e7334c71670        hanamam_tests       "docker-entrypoint.s…"   9 months ago        Exited (137) 9 months ago                       hanamam_tests_1
0e64c06151da        294976fa8146        "docker-entrypoint.s…"   9 months ago        Exited (0) 9 months ago                         stoic_blackburn
564872e8c1c5        294976fa8146        "docker-entrypoint.s…"   9 months ago        Exited (0) 9 months ago                         nervous_edison
130c5c73418d        297ac17aa87b        "docker-entrypoint.s…"   9 months ago        Exited (0) 9 months ago                         suspicious_varahamihira

// ubuntu이미지 사요나라
~
❯ docker rmi ubuntu
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:5d1d5407f353843ecf8b16524bc5565aa332e9e6a1297c73a92d3e754b8a636d
Deleted: sha256:1e4467b07108685c38297025797890f0492c4ec509212e2e4b4822d367fe6bc8
Deleted: sha256:7515ee845913c8df9826c988341a09e0240e291c66bdc436a067e070d7910a1f
Deleted: sha256:50ebe6a0675f1ed7ca499a2ec7d8cc993d495dd66ca1035c218ec5efcb6fbb8c
Deleted: sha256:2515e0ecfb82d58c004c4b53fcf9230d9eca8d0f5f823c20172be01eec587ccb
Deleted: sha256:ce30112909569cead47eac188789d0cf95924b166405aa4b71fb500d6e4ae08d

// 이미지 리스트에서 ubuntu가 없어진것을 확인.
~
❯ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hanamam_tests       latest              a53a318354f4        9 months ago        393MB
hanamam_web         latest              a53a318354f4        9 months ago        393MB
<none>              <none>              294976fa8146        9 months ago        393MB
<none>              <none>              54daab785b46        9 months ago        393MB
<none>              <none>              297ac17aa87b        9 months ago        393MB
node                alpine              59be7d262786        9 months ago        105MB

~

docker rmi ubuntu 처럼 이미지 이름만 지정하면 태그는 다르지만 ubuntu라는 이름을 가진 모든 이미지가 삭제됨.

참조

http://pyrasis.com/docker.html

profile
안녕하세요

0개의 댓글