Tech엠지대표 2023. 8. 2. 16:54

Docker Run Docker Commands

# docker images   // lists images locally
# docker run   // creates a new container
# docker ps    // lists running container
# docker ps -a   // lists all the containers
# docker exec   // executes commands on containers
# docker start/stop/restart/rm
# docker rmi   // remove docker images
# docker inspect   // detail of container & image

참고 : https://docs.docker.com/engine/reference/commandline/cli/

예제 :

$ docker pull nginx    // docker hub에서 nginx 이미지를 당겨온다.

위 command로 하면 자동으로 latest라는 이름의 tag 버전을 끌어오겠지만, https://hub.docker.com/_/nginx/tags 목록을 보고 다른 tag의 것을 가져올 수도 있다.

$ docker run --name myweb -p 7090:80 -d nginx

-d 는 background에서 run 하는 옵션. 이 옵션이 없으면 터미널의 foreground에서 실행되고 command prompt가 안 나온다. Ctrl+C를 누르면 process 자체가 kill 된다.

-p 대신 -P 대문자로 쓰고 숫자를 안 써주면 알아서 랜덤 포트로 지정

Nginx는 default로 port 80에서 실행된다. 

Now you can map a host port.

You cannot connect to this container directly because it’s going to be in a private network in the host machine, like an instance running in a private subnet, you cannot access it. If you want to access it from the outside, you have to map a host port. So you access the host on that port, and then it is gonna route the request to the port you specified (8080).

 host port: 7090, container port: 80

이것을 port mapping 혹은 port forwarding이라고 부른다. → you map a host port with a container port.

$ docker ps

를 치면 정보가 나온다.

브라우저에 IP와 port를 치면 nginx 페이지가 뜬다.

127.0.0.1:7090/

(안 뜨면 ec2의 security group 편집)

stop 하고 싶으면

$ docker stop [container id] or [container name]

docker start 도 마찬가지.

$ ps -ef

→ container는 한 디렉토리에서 실행되는 process의 하나일 뿐이라서 여기 나온다.

root user로 들어가서 docker 디렉토리를 보면

$ sudo -i
# cd /var/lib/docker
# cd containers

라고 하면 container id의 디렉토리가 보인다.

# du -sh [container id 디렉토리 이름]

하여 용량을 보면 44kb밖에 안 된다. because the container doesn’t have any data (설정 파일들), the data is from the image.

Container가 data를 저장하기 위해서는 volume을 attach 시켜야한다.