2018년 2월 17일 토요일

Docker run command simple reference

docker run
Run command create and start container

If you don't have image to make container, Docker automatically search proper image and pull image.

1. you don't have image
# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

# docker run ubuntu
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
1be7f2b886e8: Pull complete 
6fbc4a21b806: Pull complete 
c71a6f8e1378: Pull complete 
4be3072e5a37: Pull complete 
06c6d2f59700: Pull complete 
Digest: sha256:e27e9d7f7f28d67aa9e2d7540bdc2b33254b452ee8e60f388875e5b7d9b2b696
Status: Downloaded newer image for ubuntu:latest

# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
d23a16d47ae7        ubuntu              "/bin/bash"         18 seconds ago      Exited (0) 17 seconds ago                       stoic_johnson

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              0458a4468cbc        3 weeks ago         112MB

2. you have image
# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              0458a4468cbc        3 weeks ago         112MB

# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

# docker run ubuntu

# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
6fe3a86ac19a        ubuntu              "/bin/bash"         9 seconds ago       Exited (0) 8 seconds ago                       peaceful_kare

[options]

-it
Using for interactive processes
# docker run -it ubuntu:latest

root@b9f52826da9c:/# ll
total 72
drwxr-xr-x   1 root root 4096 Feb 17 03:58 ./
drwxr-xr-x   1 root root 4096 Feb 17 03:58 ../
-rwxr-xr-x   1 root root    0 Feb 17 03:58 .dockerenv*
drwxr-xr-x   2 root root 4096 Jan 23 22:49 bin/
drwxr-xr-x   2 root root 4096 Apr 12  2016 boot/
drwxr-xr-x   5 root root  360 Feb 17 03:58 dev/
drwxr-xr-x   1 root root 4096 Feb 17 03:58 etc/
drwxr-xr-x   2 root root 4096 Apr 12  2016 home/
drwxr-xr-x   8 root root 4096 Sep 13  2015 lib/
drwxr-xr-x   2 root root 4096 Jan 23 22:49 lib64/
drwxr-xr-x   2 root root 4096 Jan 23 22:49 media/
drwxr-xr-x   2 root root 4096 Jan 23 22:49 mnt/
drwxr-xr-x   2 root root 4096 Jan 23 22:49 opt/
dr-xr-xr-x 118 root root    0 Feb 17 03:58 proc/
drwx------   2 root root 4096 Jan 23 22:49 root/
drwxr-xr-x   1 root root 4096 Jan 23 22:49 run/
drwxr-xr-x   1 root root 4096 Jan 25 18:23 sbin/
drwxr-xr-x   2 root root 4096 Jan 23 22:49 srv/
dr-xr-xr-x  13 root root    0 Feb 17 03:58 sys/
drwxrwxrwt   2 root root 4096 Jan 23 22:49 tmp/
drwxr-xr-x   1 root root 4096 Jan 23 22:49 usr/
drwxr-xr-x   1 root root 4096 Jan 23 22:49 var/

-d
To start a container in detached mode (Running in background)
* Caution : Container will stop when any foreground process don't work in container
I use option '-it' to show running container detached mode on below code
# docker run -it -d ubuntu:latest 
8bb0beb95f0f9f85cbf781650baa2194693c6eb5a425a30bcd5c03b61b1539f3

# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
8bb0beb95f0f        ubuntu:latest       "/bin/bash"         4 seconds ago       Up 4 seconds                            quizzical_goodall

-p
Publish a container᾿s port or a range of ports to the host
docker run -p hostport:containerport
# docker run -d -p 12345:6379 redis
efb01ae0a176e72a1e0c7c1e0b418e1e8d338bf2366c2991af12ad8712c32933

$ telnet localhost 12345
Trying ::1...
Connected to localhost.
Escape character is '^]'.
set test abcde
+OK
get test
$5
abcde
^]
telnet> quit
Connection closed.

--rm
automatically clean up the container and remove the file system when the container exits
seunghyun@ubuntu:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
seunghyun@ubuntu:~$ docker run -it ubuntu
root@19c1e015546a:/# exit
exit
seunghyun@ubuntu:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
19c1e015546a        ubuntu              "/bin/bash"         6 seconds ago       Exited (0) 3 seconds ago                       quirky_booth
seunghyun@ubuntu:~$ docker run -it --rm ubuntu
root@28e77f58194b:/# exit
exit
seunghyun@ubuntu:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
19c1e015546a        ubuntu              "/bin/bash"         27 seconds ago      Exited (0) 24 seconds ago                       quirky_booth

--name
Defining a name can be a handy way to add meaning to a container
seunghyun@ubuntu:~$ docker run --name seunghyun ubuntu
seunghyun@ubuntu:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
seunghyun@ubuntu:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
ee741713119e        ubuntu              "/bin/bash"         5 seconds ago       Exited (0) 4 seconds ago                       seunghyun

reference : https://docs.docker.com/engine/reference/run/

댓글 없음:

댓글 쓰기