728x90

실습 가이드


실습 환경 정리

모든 컨테이너 및 이미지 삭제

docker container rm $(docker ps -aq)
docker image prune

 

이미지 다운로드

apline 이미지를 제외한 나머지 이미지 다운로드 시 태그 입력

  • centos:7
  • alpine
  • httpd:latest
  • mysql:5.6

 

설치한 이미지 확인

  • 목록 확인
  • centos:7 이미지의 상세 정보 확인
  • 상세 정보에서 쉘을 실행하는 이미지 확인 (CMD 부분)

 

컨테이너 실행

컨테이너 이름 컨테이너 이미지 사용 명령어
web / web2 httpd create, start / run
os / os2 centos:7 create, start / run
지정하지 않음 hello-world 컨테이너 생성 후 자동 삭제되도록 생성
a1 alpine x
x mysql x

 

리소스 정리

1. 동작 중인 컨테이너들을 하나씩 종료할 것

2. 종료된 컨테이너들 모두 삭제

3. 로컬 이미지들 중 hello-world 이미지 삭제

 


실습


이미지 다운로드

[root@localhost ~]# docker image pull centos:7
[root@localhost ~]# docker image pull alpine
[root@localhost ~]# docker image pull httpd:latest
[root@localhost ~]# docker image pull mysql:5.6

 


 

설치한 이미지 확인

다운로드한 이미지 목록 확인

[root@localhost ~]# docker image ls

 

 

centos:7 이미지의 상세 정보 확인 및 쉘(CMD)을 실행하는 이미지 확인

[root@localhost ~]# docker image inspect centos:7

 

 


 

컨테이너 실행

web 컨테이너 생성 후 시작

[root@localhost ~]# docker container create -it --name web httpd
799485e6b3597d82fdfb225f6deb6eceae4a8b689cc7d84b3365e42b35da61fe

[root@localhost ~]# docker container start web
web
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         PORTS     NAMES
799485e6b359   httpd     "httpd-foreground"   2 minutes ago   Up 3 seconds   80/tcp    web

 

os 컨테이너 생성 후 시작

[root@localhost ~]# docker container create -it --name os centos:7
9296e610d97cc497b7737340db3f72395553cc0d92728311097892f5d28aa3b0

[root@localhost ~]# docker container start os
os
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE      COMMAND              CREATED          STATUS         PORTS     NAMES
9296e610d97c   centos:7   "/bin/bash"          10 seconds ago   Up 2 seconds             os
799485e6b359   httpd      "httpd-foreground"   6 minutes ago    Up 3 minutes   80/tcp    web

[root@localhost ~]# docker container start -ai os
[root@9296e610d97c /]#

 

run 명령어를 사용하여 컨테이너 실행

[root@localhost ~]# docker container run -itd --name web2 httpd
193ab34ff573679a5e8f55333ff09bf43b7e0e83647a68d41d7cab6d77423d4e

[root@localhost ~]# docker container run -itd --name os2 centos:7
99c412c8b7f0632f3acbe163c151194a40deee805909ca8597ec22eaba0dc97b

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE      COMMAND              CREATED              STATUS              PORTS     NAMES
99c412c8b7f0   centos:7   "/bin/bash"          3 seconds ago        Up 2 seconds                  os2
193ab34ff573   httpd      "httpd-foreground"   About a minute ago   Up About a minute   80/tcp    web2

 

hello-world 이미지로 컨테이너 생성, 실행 종료 후 자동 삭제

[root@localhost ~]# docker container run -it --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:80f31da1ac7b312ba29d65080fddf797dd76acfb870e677f390d5acba9741b17
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE      COMMAND              CREATED              STATUS              PORTS     NAMES
99c412c8b7f0   centos:7   "/bin/bash"          About a minute ago   Up About a minute             os2
193ab34ff573   httpd      "httpd-foreground"   3 minutes ago        Up 3 minutes        80/tcp    web2
799485e6b359   httpd      "httpd-foreground"   18 minutes ago       Up 15 minutes       80/tcp    web

 

alpine 이미지로 a1이라는 이름의 컨테이너 실행

[root@localhost ~]# docker container run -itd --name a1 alpine
21036dfa3e9a8addac5f26182f84c5458f03d9c982b0f88f8b948237d30b35c0

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE      COMMAND              CREATED          STATUS          PORTS     NAMES
21036dfa3e9a   alpine     "/bin/sh"            4 seconds ago    Up 2 seconds              a1

 

mysql 이미지 실행

# mysql 이미지를 사용한 mysql-cont 컨테이너 생성 및 실행
[root@localhost ~]# docker container run --name mysql-cont -e MYSQL_ROOT_PASSWORD=123 -d -p 3306:3306 mysql:5.6
296611bf58024dd2002b03f765d71431f2e66049d2b23def092111fc5d2f9c9c

# 실행 상태인 mysql-cont 확인
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                                       NAMES
296611bf5802   mysql:5.6   "docker-entrypoint.s…"   5 seconds ago    Up 3 seconds    0.0.0.0:3306->3306/tcp, :::3306->3306/tcp   mysql-cont

# mysql-cont 컨테이너 접속
[root@localhost ~]# docker exec -it mysql-cont bash
root@296611bf5802:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.51 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

 


 

리소스 정리

동작 중인 컨테이너들을 하나씩 종료

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                                       NAMES
296611bf5802   mysql:5.6   "docker-entrypoint.s…"   3 minutes ago    Up 3 minutes    0.0.0.0:3306->3306/tcp, :::3306->3306/tcp   mysql-cont
21036dfa3e9a   alpine      "/bin/sh"                13 minutes ago   Up 13 minutes                                               a1
99c412c8b7f0   centos:7    "/bin/bash"              17 minutes ago   Up 17 minutes                                               os2
193ab34ff573   httpd       "httpd-foreground"       18 minutes ago   Up 18 minutes   80/tcp                                      web2
799485e6b359   httpd       "httpd-foreground"       33 minutes ago   Up 31 minutes   80/tcp                                      web

[root@localhost ~]# docker container stop mysql-cont
mysql-cont

[root@localhost ~]# docker container stop a1
a1

[root@localhost ~]# docker container stop os2 web2 web
os2
web2
web

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

 

종료된 컨테이너들을 모두 삭제

# 실행 중인 컨테이너 목록 확인
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

# 종료 또는 실행 상태의 모든 컨테이너 목록 확인
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE       COMMAND                  CREATED          STATU   S                          PORTS     NAMES
296611bf5802   mysql:5.6   "docker-entrypoint.s…"   6 minutes ago    Exite   d (0) About a minute ago             mysql-cont
4e9e5157f459   mysql:5.6   "docker-entrypoint.s…"   13 minutes ago   Exite   d (1) 9 minutes ago                  mysql-con
d19fc975900a   mysql:5.6   "docker-entrypoint.s…"   15 minutes ago   Exite   d (1) 15 minutes ago                 sweet_greider
21036dfa3e9a   alpine      "/bin/sh"                16 minutes ago   Exite   d (137) 56 seconds ago               a1
99c412c8b7f0   centos:7    "/bin/bash"              20 minutes ago   Exite   d (137) 35 seconds ago               os2
193ab34ff573   httpd       "httpd-foreground"       21 minutes ago   Exite   d (0) 44 seconds ago                 web2
799485e6b359   httpd       "httpd-foreground"       37 minutes ago   Exite   d (0) 44 seconds ago                 web

# 컨테이너 이름으로 하나씩 삭제
[root@localhost ~]# docker container rm mysql-cont
mysql-cont

[root@localhost ~]# docker container rm mysql-con
mysql-con

# 컨테이너 ID를 이용하여 여러 개 한 번에 삭제
[root@localhost ~]# docker container rm d19fc975900a 21036dfa3e9a 99c412c8b7f0
d19fc975900a
21036dfa3e9a
99c412c8b7f0

# 남아있는 컨테이너 목록 확인
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                     PORTS     NAMES
193ab34ff573   httpd     "httpd-foreground"   23 minutes ago   Exited (0) 2 minutes ago             web2
799485e6b359   httpd     "httpd-foreground"   38 minutes ago   Exited (0) 2 minutes ago             web

# 실행하고 있지 않은 모든 컨테이너 삭제
[root@localhost ~]# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
193ab34ff573679a5e8f55333ff09bf43b7e0e83647a68d41d7cab6d77423d4e
799485e6b3597d82fdfb225f6deb6eceae4a8b689cc7d84b3365e42b35da61fe

Total reclaimed space: 0B

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

 

로컬 이미지들 중 hello-world 이미지 삭제

[root@localhost ~]# docker image ls
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
httpd         latest    b260a49eebf9   33 hours ago   145MB
alpine        latest    e66264b98777   3 weeks ago    5.53MB
mysql         5.6       dd3b2a5dcb48   5 months ago   303MB
hello-world   latest    feb5d9fea6a5   8 months ago   13.3kB
centos        7         eeb6ee3f44bd   9 months ago   204MB

[root@localhost ~]# docker image rm hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:80f31da1ac7b312ba29d65080fddf797dd76acfb870e677f390d5acba9741b17
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359

[root@localhost ~]# docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
httpd        latest    b260a49eebf9   33 hours ago   145MB
alpine       latest    e66264b98777   3 weeks ago    5.53MB
mysql        5.6       dd3b2a5dcb48   5 months ago   303MB
centos       7         eeb6ee3f44bd   9 months ago   204MB
728x90

+ Recent posts