프로그래밍 툴/Docker

Docker (도커)- basics

험자 2020. 7. 20. 13:53

Docker following Docker Deep Dive

Create a non-root user

sudo usermod -aG docker <username>
# confirm
cat /etc/group | grep docker
docker:x:998:dev

Basic commands

docker version

Client:
 Version:           18.09.4
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        d14af54
 Built:             Wed Mar 27 18:34:51 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.4
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.8
  Git commit:       d14af54
  Built:            Wed Mar 27 18:01:48 2019
  OS/Arch:          linux/amd64
  Experimental:     false
docker image

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              f38770f8ed03        2 days ago          2.38GB
debian              buster              1b686a95ddbf        5 weeks ago         114MB
docker image pull ubuntu:latest

latest: Pulling from library/ubuntu
692c352adcf2: Pull complete
97058a342707: Pull complete
2821b8e766f4: Pull complete
4e643cc37772: Pull complete
Digest: sha256:55cd38b70425947db71112eb5dddfa3aa3e3ce307754a3df2269069d2278ce47
Status: Downloaded newer image for ubuntu:latest
docker container run -it ubuntu:latest /bin/bash

root@46ac1a559fa7:/#
# local shell is attached to the shell of the container
# -it flags tell the daemon to make the container interactive and to attach our current terminal to the shell of the container.
# inside container
ps -elf

F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 S root         1     0  0  80   0 -  1027 do_wai 01:53 pts/0    00:00:00 /bin/bash
0 R root         9     1  0  80   0 -  1470 -      01:58 pts/0    00:00:00 ps -elf
# ctrl-pq
# escape without killing it
# confirm by
docker container ls

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
46ac1a559fa7        ubuntu:latest       "/bin/bash"         11 minutes ago      Up 11 minutes                           dazzling_sammet

# connect back
docker container exec -it dazzling_sammet bash

# stop
docker container stop dazzling_sammet

# remove 
docker container rm dazzling_sammet

run an existing App from Git

git clone <app-repository>

# check `Dockerfile` in the root folder of the app
cat Dockerfile

# for example
FROM alpine
LABEL maintainer="nigelpoulton@hotmail.com"
RUN apk add --update nodejs nodejs-npm
COPY . /src
WORKDIR /src
RUN npm install
EXPOSE 8080
ENTRYPOINT ["node", "./app.js"]

# create docker image out of it
# where Dockerfile is
docker image build -t test:latest
docker container run -d --name web1 -p 8080:8080 test:latest

# on browser, connect to `localhost:8080` to find index page loaded