Docker Dockerfile 생성부터 Container 실행까지

by 민갤

Server /

1. Dockerfile 생성

# cd /home/dico/docker
# vi dockerfile
FROM mysql:8.0.17
FROM nginx:1.17.4
FROM openjdk:13-jdk

LABEL maintainer=""


2. Docker Build

docker build -t {이미지명}:{태그} {Dockerfile 위치 경로}

# docker build -t dico:v1.0 .
Sending build context to Docker daemon  2.048kB
Step 1/5 : FROM mysql:8.0.17
8.0.17: Pulling from library/mysql
8f91359f1fff: Pull complete 
6bbb1c853362: Pull complete 
e6e554c0af6f: Pull complete 
f391c1a77330: Pull complete 
414a8a88eabc: Pull complete 
fee78658f4dd: Pull complete 
9568f6bff01b: Pull complete 
5a026d8bbe50: Pull complete 
07f193b54ae1: Pull complete 
1e404375a275: Pull complete 
b81b2ef0e430: Pull complete 
2f499f36bd40: Pull complete 
Digest: sha256:6d95fa56e008425121e24d2c01b76ebbf51ca1df0bafb1edbe1a46937f4a149d
Status: Downloaded newer image for mysql:8.0.17
 ---> b8fd9553f1f0
Step 2/5 : FROM nginx:1.17.4
1.17.4: Pulling from library/nginx
b8f262c62ec6: Pull complete 
e9218e8f93b1: Pull complete 
7acba7289aa3: Pull complete 
Digest: sha256:aeded0f2a861747f43a01cf1018cf9efe2bdd02afd57d2b11fcc7fcadc16ccd1
Status: Downloaded newer image for nginx:1.17.4
 ---> f949e7d76d63
Step 3/5 : FROM openjdk:13-jdk
13-jdk: Pulling from library/openjdk
a316717fc6ee: Pull complete 
809137453b07: Pull complete 
b363ad12fddb: Pull complete 
Digest: sha256:59ffb600ede7dff51f294af135a3497bb634589b7e8214ed47eb36a7435879ab
Status: Downloaded newer image for openjdk:13-jdk
 ---> b255bbd4a82d
Step 4/5 : LABEL maintainer=""
 ---> Running in db54728cdc14
Removing intermediate container db54728cdc14
 ---> 6aa4c22f9376
Step 5/5 : WORKDIR /app
 ---> Running in 0ce24c0a5b92
Removing intermediate container 0ce24c0a5b92
 ---> 292bd17939d7
Successfully built 292bd17939d7
Successfully tagged dico:v1.0

3. Docker Image 확인

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
dico                v1.0                292bd17939d7        41 seconds ago      490MB
nginx               1.17.4              f949e7d76d63        11 days ago         126MB
mysql               8.0.17              b8fd9553f1f0        3 weeks ago         445MB
openjdk             13-jdk              b255bbd4a82d        5 weeks ago         490MB

4. Docker Container 생성 및 실행

방법 1) 이 형식은 별다른 조치를 하지 않으면 Container가 바로 종료(Exit)되어버린다.

docker run --name {Container Name} {Image Name:Tag}

# docker run --name dico dico:v1.0
Oct 06, 2019 2:14:24 AM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
|  Welcome to JShell -- Version 13
|  For an introduction type: /help intro

jshell> root@localhost:/home/dico/docker#

- Docker Container 확인

# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMES
d50fed18f7b5        dico:v1.0           "jshell"            About a minute ago   Exited (0) About a minute ago                       dico

방법 2) 옵션 t, d를 주면 종료되지 않는다.

docker run -td --name {Container Name} {Image Name:Tag}

  • -t, --tty : Container에 터미널 할당
  • -d, --detach : 백그라운드에서 Container 실행
  • -p, --pulish list : Container Port를 Host에 연결
# docker run -td --name dico_td dico:v1.0
# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
36d793b38f94        dico:v1.0           "jshell"            5 seconds ago       Up 4 seconds                                   dico_td
d50fed18f7b5        dico:v1.0           "jshell"            19 minutes ago      Exited (0) 6 minutes ago                       dico


docker: Error response from daemon: failed to start shim: exec: "docker-containerd-shim": executable file not found in $PATH: unknown.

# /bin/systemctl restart docker

컨테이너 접속

docker exec -it {Container ID} /bin/bash

# docker exec -it 36d793b38f94 /bin/bash

컨테이너 삭제

docker rm {CONTAINER ID}

이미지 삭제

docker rmi {IMAGE ID}

- 컨테이너도 함께 삭제

docker rmi -f {IMAGE ID}




Back-End Developer

꾸잉꾸잉하고 웁니다.


디코에 오신 것을 환영해요!
전문가들의 수많은 아티클 창고 🤓