본문 바로가기

Micro Service Architecture/Docker

5. [docker] container run

반응형

1. Image를 이용한 container 실행

## nginx:latest 이미지를 이용하여 포트 호스트:8070 -> 도커:80 으로 포워드 설정으로 컨테이너 생성
[root@docker]# docker run -p 8070:80 nginx:latest
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
 
 
## 백그라운드 모드로 실행
[root@docker]# docker run -d -p 8070:80 nginx:latest
 
 
## 알아보기 편한 이름값을 추가
[root@docker]# docker run -d --name="nginx-test" -p 8070:80 nginx:latest
 
## 컨테이너 확인
[root@docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
17c1c0d1ba84        nginx:latest        "/docker-entrypoint.…"   12 seconds ago      Exited (0) 5 seconds ago                       boring_allen
 
 
## 생성 된 컨테이너의 시작, 재시작, 종료
## docker <start|restart|stop> <container ID>
 
 
## 컨테이너 안의 명령어 실행하기
## docker exec <container ID> <command>
[root@docker]# docker exec 17c1c0d1ba84 ls -al /
total 12
drwxr-xr-x   1 root root   39 Jul 13 05:48 .
drwxr-xr-x   1 root root   39 Jul 13 05:48 ..
-rwxr-xr-x   1 root root    0 Jul 13 05:48 .dockerenv
drwxr-xr-x   2 root root 4096 Jul 20  2020 bin
drwxr-xr-x   2 root root    6 May  2 16:39 boot
drwxr-xr-x   5 root root  340 Jul 13 05:49 dev
drwxr-xr-x   1 root root   41 Jul 22  2020 docker-entrypoint.d
-rwxrwxr-x   1 root root 1202 Jul 22  2020 docker-entrypoint.sh
drwxr-xr-x   1 root root   19 Jul 13 05:48 etc
drwxr-xr-x   2 root root    6 May  2 16:39 home
drwxr-xr-x   1 root root   56 Jul 22  2020 lib
drwxr-xr-x   2 root root   34 Jul 20  2020 lib64
drwxr-xr-x   2 root root    6 Jul 20  2020 media
drwxr-xr-x   2 root root    6 Jul 20  2020 mnt
drwxr-xr-x   2 root root    6 Jul 20  2020 opt
dr-xr-xr-x 148 root root    0 Jul 13 05:49 proc
drwx------   2 root root   37 Jul 20  2020 root
drwxr-xr-x   1 root root   23 Jul 13 05:49 run
drwxr-xr-x   2 root root 4096 Jul 20  2020 sbin
drwxr-xr-x   2 root root    6 Jul 20  2020 srv
dr-xr-xr-x  13 root root    0 Jul 13 02:14 sys
drwxrwxrwt   1 root root    6 Jul 22  2020 tmp
drwxr-xr-x   1 root root   66 Jul 20  2020 usr
drwxr-xr-x   1 root root   19 Jul 20  2020 var

[root@docker]# docker exec 17c1c0d1ba84 cat /etc/nginx/nginx.conf
 
user  nginx;
worker_processes  1;
 
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  /var/log/nginx/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    keepalive_timeout  65;
 
    #gzip  on;
 
    include /etc/nginx/conf.d/*.conf;
}
 
 
## 현재 실행중인 컨테이너 접속
## 이경우 run 했을때의 stdout 상태로 돌아감.
## docker attach <container ID>
 
 
## 실행중인 컨테이너에 쉘을 획득하고 싶으면
[root@docker]# docker exec -it 17c1c0d1ba84 /bin/bash
root@17c1c0d1ba84:/# exit
exit
[root@OPENLDAP-TEST ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
17c1c0d1ba84        nginx:latest        "/docker-entrypoint.…"   46 minutes ago      Up About a minute   0.0.0.0:8070->80/tcp   boring_allen
반응형