Docker Super Basics

# How to docker

### Get a images from repositry(Docker-Hub)

- docker search [keyword ex:pkg-name]
- dcoker pull [keyword ex:pkg-name]

```
 [root@Ogiman ~]# docker search httpd
NAME                                DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
httpd                               The Apache HTTP Server Project                  1301      [OK]
hypriot/rpi-busybox-httpd           Raspberry Pi compatible Docker Image with ...   38
centos/httpd                                                                        15                   [OK]
armhf/httpd                         The Apache HTTP Server Project                  8
macadmins/netboot-httpd             use in combination with bruienne/bsdpy          4                    [OK]
centos/httpd-24-centos7             Platform for running Apache httpd 2.4 or b...   3
lolhens/httpd                       Apache httpd 2 Server                           2                    [OK]
salim1983hoop/httpd24               Dockerfile running apache config                2                    [OK]
lead4good/httpd-fpm                 httpd server which connects via fcgi proxy...   1                    [OK]
rgielen/httpd-image-php5            Docker image for Apache httpd with PHP 5 b...   1                    [OK]
rgielen/httpd-image-simple          Docker image for simple Apache httpd based...   1                    [OK]
sbutler/pie-httpd                   PIE httpd server                                0
openbucks/httpd                                                                     0
memeglobal/v2v-httpd                                                                0
dockerpinata/httpd                                                                  0
tplatform/aws-linux-httpd24-php70   aws-linux-httpd24-php70                         0                    [OK]
manageiq/httpd                      Container with httpd, built on CentOS for ...   0                    [OK]
publici/httpd                       httpd:latest                                    0                    [OK]
manasip/httpd                                                                       0
trollin/httpd                                                                       0
memeglobal/adserver-httpd-events7                                                   0
jbpt/httpd                                                                          0                    [OK]
ppc64le/httpd                       The Apache HTTP Server Project                  0
antoineco/httpd                     Extra OS variants for the official HTTPd i...   0                    [OK]
efrecon/httpd                       A micro-sized httpd. Start serving files i...   0                    [OK]

[root@Ogiman ~]# docker pull httpd
latest: Pulling from httpd
632d62e9ff45: Pull complete
4719c3e8a982: Pull complete
f306af4c77c9: Pull complete
27ed30efb15b: Pull complete
4dd8da937227: Pull complete
cf080c8183cd: Pull complete
67969a5b8436: Pull complete
c7bff257d73b: Pull complete
f72468576c1a: Pull complete
c8ad486ac41f: Pull complete
0295aac114eb: Pull complete
59e0a06397fe: Pull complete
214bfb482d97: Pull complete
5aa4b041cff0: Pull complete
b3ef6c400fd6: Pull complete
c78d0fb49952: Pull complete
07077b5b9c31: Pull complete
717460ec98ba: Pull complete
4ee51b7afc56: Pull complete
Digest: sha256:97d025bec2e511820feb0377ad9ade1c4cfd6bc35b884974d09aa70a5f31b1a7
Status: Downloaded newer image for httpd:latest

```

### show list of images

- docker images

```
[root@Ogiman ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
mysql_ogiman        ogiman              61093fd72673        5 minutes ago       412.4 MB
nginx               latest              71326c378a50        3 months ago        107.5 MB
mysql               latest              a4ce13a9e85b        3 months ago        412.3 MB
jenkins             latest              53fa7c60bf25        5 months ago        704.2 MB
```


### Delete the images 

- docker rmi [images or images:tag(if you tagged)]

```
[root@Ogiman ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
mysql_ogiman        ogiman              61093fd72673        5 minutes ago       412.4 MB
nginx               latest              71326c378a50        3 months ago        107.5 MB
mysql               latest              a4ce13a9e85b        3 months ago        412.3 MB
jenkins             latest              53fa7c60bf25        5 months ago        704.2 MB

[root@Ogiman ~]# docker rmi mysql_ogiman:ogiman
Untagged: mysql_ogiman:ogiman
Deleted: 61093fd726737257d5344acb9b64a30079922aaf74ee180132a9775594f71b31


root@Ogiman ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
nginx               latest              71326c378a50        3 months ago        107.5 MB
mysql               latest              a4ce13a9e85b        3 months ago        412.3 MB
jenkins             latest              53fa7c60bf25        5 months ago        704.2 MB
```

### create container from images

- docker run -it --name "container-name" "image" /bin/bash
    - any type of option
   
| option        | detail                                     | 
|---------------|--------------------------------------------|
| -it           | interactive mode                           |
| --name        | name of containers author               　 |
| /bin/bash     | boot with bash                             |
| --rm          | destroy the container when stop it         |
| -w:--workdir  | set the current dir                        |
| -e:--env      | set Environment variable                   |
| --env-file    | include Environment variable with file     |
| -h:--hostname | set hostname                               |
| --dns         | set name-server which the container lookup |
| --dns-search  | set domain-name                            |


```
[root@Ogiman ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
httpd               latest              4ee51b7afc56        42 hours ago        177.3 MB
nginx               latest              71326c378a50        3 months ago        107.5 MB
mysql               latest              a4ce13a9e85b        3 months ago        412.3 MB
jenkins             latest              53fa7c60bf25        5 months ago        704.2 MB

[root@Ogiman ~]# docker run -it --name ogiman.httpd httpd /bin/bash
root@4526b10e0867:/usr/local/apache2#
```

### check container which running or not

- dcoker ps
    - you can check only container running
- docker ps -a
    - you can check all container running and stop

```
[root@Ogiman ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4526b10e0867        httpd               "/bin/bash"         9 minutes ago       Up 9 minutes        80/tcp              ogiman.httpd

[root@Ogiman ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                     PORTS               NAMES
4526b10e0867        httpd               "/bin/bash"            9 minutes ago       Up 9 minutes               80/tcp              ogiman.httpd
4d4f3e43e5e3        mysql               "docker-entrypoint.s   10 weeks ago        Exited (0) 7 seconds ago                       naughty_stallman
```

### exit from container

1 ` ctrl + p q (keep ctrl and press p , q)` 

- it doesn`t stop container , running background

2 `exit`
- it stop the container , 


### container start and stop

- docker start "container id"
- docker stop "container id"
    - these command use only when the container exist
    - it mean that you create container from images
    - so you can check `dcoker ps -a`

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

[root@Ogiman ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                      PORTS               NAMES
4526b10e0867        httpd               "/bin/bash"            17 minutes ago      Exited (0) 12 seconds ago                       ogiman.httpd
4d4f3e43e5e3        mysql               "docker-entrypoint.s   10 weeks ago        Exited (0) 12 seconds ago                       naughty_stallman

[root@Ogiman ~]# docker start 4526b10e0867
4526b10e0867

[root@Ogiman ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4526b10e0867        httpd               "/bin/bash"         17 minutes ago      Up 2 seconds        80/tcp              ogiman.httpd

[root@Ogiman ~]# docker stop 4526b10e0867
4526b10e0867

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


### attach the container running

- docker attach "container id"

```
[root@Ogiman ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4526b10e0867        httpd               "/bin/bash"         19 minutes ago      Up 1 seconds        80/tcp              ogiman.httpd

[root@Ogiman ~]# docker attach 4526b10e0867
root@4526b10e0867:/usr/local/apache2#
```

### snapshot the container

- docker container "container-ID" "ripositry:tag"
    - recommend you that the container should  be stopped when snapshot .There is a possibility that data may be corrupted while the container is running

```
[root@Ogiman ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                     PORTS               NAMES
4526b10e0867        httpd               "/bin/bash"            4 days ago          Exited (0) 7 minutes ago   80/tcp              ogiman.httpd
4d4f3e43e5e3        mysql               "docker-entrypoint.s   11 weeks ago        Exited (0) 4 days ago

[root@Ogiman ~]# docker commit 4526b10e0867 _ogiman/httpd:ogiman
6ff5e793313a05dce01002c4e6f83e9a79b200d4e7fd04ed5d49d7d5546879c9

[root@Ogiman ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
_ogiman/httpd       ogiman              6ff5e793313a        About a minute ago   177.3 MB
httpd               latest              4ee51b7afc56        6 days ago           177.3 MB
nginx               latest              71326c378a50        3 months ago         107.5 MB
mysql               latest              a4ce13a9e85b        3 months ago         412.3 MB
jenkins             latest              53fa7c60bf25        5 months ago         704.2 MB
```

### delete docker container

- docker rm "container-name"

```
[root@Ogiman ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                      PORTS               NAMES
4526b10e0867        httpd               "/bin/bash"            4 days ago          Exited (0) 19 minutes ago   80/tcp              ogiman.httpd
4d4f3e43e5e3        mysql               "docker-entrypoint.s   11 weeks ago        Exited (0) 4 days ago                           naughty_stallman

[root@Ogiman ~]# docker rm 4526b10e0867
4526b10e0867

[root@Ogiman ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                  PORTS               NAMES
4d4f3e43e5e3        mysql               "docker-entrypoint.s   11 weeks ago        Exited (0) 4 days ago                       naughty_stallman
```


### container run with port-forward

- docker run -it -p 8000:80 -name "container-name" "ripository" /bin/bash
    - -p : port-farward ex)from 8000 to 80
    - -name : specify the "container-name"

```
[root@Ogiman ~]# docker run -it -p 8000:80 --name ogiman_nginx nginx /bin/bash
root@d35884fd224f:/#

[root@Ogiman ~]# curl -I http://192.168.247.152:8000
HTTP/1.1 200 OK
Server: nginx/1.13.3
Date: Mon, 30 Oct 2017 14:30:54 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 11 Jul 2017 13:06:07 GMT
Connection: keep-alive
ETag: "5964cd3f-264"
Accept-Ranges: bytes

[root@Ogiman ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
d35884fd224f        nginx               "/bin/bash"         49 seconds ago      Up 48 seconds       0.0.0.0:8000->80/tcp   ogiman_nginx
[root@Ogiman ~]#

```

### check container process

- docker top "container ID"

```
[root@Ogiman ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
d35884fd224f        nginx               "/bin/bash"         23 hours ago        Up About a minute   0.0.0.0:8000->80/tcp   ogiman_nginx

[root@Ogiman ~]# docker top d35884fd224f
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                2562                1035                0                   23:16               pts/1               00:00:00            /bin/bash
[root@Ogiman ~]#
```

### how to make dockerfile

| command         | detail                                  |
|-----------------|-----------------------------------------|
| FROM            | specify the repository:tag of container |
| MAINTAINER      | name of container`s author           　 |
| ENV             | set Environment variable                |
| RUN             | command that you wanna execute          |
| ADD             | put file into container                 |
| EXPOSE          | set port number of application          |
| CMD             | command with executing on boot(with starting container)        |

```
#Dockerfile for Apache HTTP server

FROM nginx
MAINTAINER ogiman

RUN echo "ogiman" >> /var/ogiman
ADD index.html /var/www/html/index.html
CMD service nginx start $$ bash
```

```
[root@Ogiman ~]# ls -l build_nginx/
total 8
-rw-r--r-- 1 root root 172 Oct 31 23:35 Dockerfile
-rw-r--r-- 1 root root  16 Oct 31 23:36 index.html
[root@Ogiman ~]#
[root@Ogiman ~]# cat build_nginx/Dockerfile
#Dockerfile for Apache HTTP server

FROM nginx
MAINTAINER ogiman

RUN echo "ogiman" >> /var/ogiman
ADD index.html /var/www/html/index.html
CMD service nginx start $$ bash

[root@Ogiman ~]# docker build -t ogiman2_nginx ~/build_nginx
Sending build context to Docker daemon 3.072 kB
Sending build context to Docker daemon
Step 0 : FROM nginx
 ---> 71326c378a50
Step 1 : MAINTAINER ogiman
 ---> Running in 5d57c97ae8e7
 ---> 3df3b88cb527
Removing intermediate container 5d57c97ae8e7
Step 2 : RUN echo "ogiman" >> /var/ogiman
 ---> Running in 5a295b7ed3f7
 ---> 181583bac571
Removing intermediate container 5a295b7ed3f7
Step 3 : ADD index.html /var/www/html/index.html
 ---> 33ca9d3f9aa6
Removing intermediate container d1970e617ca2
Step 4 : CMD service nginx start $$ bash
 ---> Running in 659eb7234a0d
 ---> fdc3aea3ab05
Removing intermediate container 659eb7234a0d
Successfully built fdc3aea3ab05

[root@Ogiman ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
ogiman2_nginx       latest              fdc3aea3ab05        About a minute ago   107.5 MB
httpd               latest              4ee51b7afc56        7 days ago           177.3 MB
nginx               latest              71326c378a50        3 months ago         107.5 MB
mysql               latest              a4ce13a9e85b        3 months ago         412.3 MB
jenkins             latest              53fa7c60bf25        5 months ago         704.2 MB

```

### get info from container

- docker inspect　"container-id"
- docker inspect --format="specified info you wanna get"  

```
[root@Ogiman ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
04ba71249af2        reverse_nginx       "/bin/bash"         14 minutes ago      Up 14 minutes       0.0.0.0:8000->80/tcp   reverse_nginx
[root@Ogiman ~]# docker inspect --format="{{.NetworkSettings.IPAddress}}" 04ba71249af2
172.17.0.14
[root@Ogiman ~]# docker inspect 04ba71249af
[
{
    "Id": "04ba71249af2020aaf8d5b5817122c109159458b917d91c9f4033c04275b73cc",
    "Created": "2017-11-04T14:13:45.339722558Z",
    "Path": "/bin/bash",
    "Args": [],
    "State": {
        "Running": true,
        "Paused": false,
        "Restarting": false,
        "OOMKilled": false,
        "Dead": false,
        "Pid": 5541,
        "ExitCode": 0,
        "Error": "",
        "StartedAt": "2017-11-04T14:13:45.74020123Z",
        "FinishedAt": "0001-01-01T00:00:00Z"
    },
　　　　　・
　　　　　・
　　　　　・
　　　　　・
```

### show log taht you executed in container

- docker logs "container-id"

```
[root@Ogiman ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
04ba71249af2        reverse_nginx       "/bin/bash"         26 minutes ago      Up 26 minutes       0.0.0.0:8000->80/tcp   reverse_nginx

[root@Ogiman ~]# docker logs 04ba71249af2
root@04ba71249af2:/#
root@04ba71249af2:/#
root@04ba71249af2:/#
root@04ba71249af2:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@04ba71249af2:/#
root@04ba71249af2:/#
[root@Ogiman ~]#
```


### show resouse in container

- docker top "container-id"

```
[root@Ogiman ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
04ba71249af2        reverse_nginx       "/bin/bash"         26 minutes ago      Up 26 minutes       0.0.0.0:8000->80/tcp   reverse_nginx

[root@Ogiman ~]# docker top 04ba71249af2
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                5541                1178                0                   23:13               pts/1               00:00:00            /bin/bash
[root@Ogiman ~]#
```

### show port used in container

- docker port "container-id"

```
[root@Ogiman ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
04ba71249af2        reverse_nginx       "/bin/bash"         29 minutes ago      Up 29 minutes       0.0.0.0:8000->80/tcp   reverse_nginx

[root@Ogiman ~]# docker port 04ba71249af2
80/tcp -> 0.0.0.0:8000
```

### execute command from host to container

- docker exec "container-id" "command"

```
[root@Ogiman ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
04ba71249af2        reverse_nginx       "/bin/bash"         39 minutes ago      Up 39 minutes       0.0.0.0:8000->80/tcp   reverse_nginx

[root@Ogiman ~]# docker exec 04ba71249af2 df
Filesystem                                                                                        1K-blocks    Used Available Use% Mounted on
/dev/mapper/docker-253:0-8395369-04ba71249af2020aaf8d5b5817122c109159458b917d91c9f4033c04275b73cc  10190100  167308   9482120   2% /
tmpfs                                                                                                504196       0    504196   0% /dev
shm                                                                                                   65536       0     65536   0% /dev/shm
/dev/mapper/cl_ogiman-root                                                                         13092864 4592780   8500084  36% /etc/hosts
```