Knowing the various states of the Docker Container is essential for any serious Docker user.

I'll explain the container lifecycle and then show the Docker commands for each stage of the lifecycle.

But before you learn all those things, let's revisit the concept of the container once more.

What are Docker containers, again?

Most traditional definitions of containers go like this:

Containers are a form of operating system virtualization. While traditional hypervisor based virtualization requires separate kernels for separate VMs, containers share the same host kernel, and is therefore, much more lightweight and faster to spin up

The definition will differ from source to source, but the gist is the same.

I find it boring and unnecessarily complicated. I'd like to use something different here to define containers.

Containers are a bunch of processes that are being cheated on.

Why do I say that? Because containers are just a collection of processes, sometimes just a single process. And these processes are being lied to about different resources of its host like networks, process tree, filesystem, hostname etc.

You can run any process, even a shell like bash, and hide your actual process tree from it, give it a different set of networks, hide the actual root filesystem from it, give it a different hostname and essentially, what you'll end up doing is creating a containerized version of the process.

This is achieved by namespaces, separate root filesystem and cgroups.

Docker is just a wrapper around the lower level tools that run and manage the containers, or more specifically saying the container lifecycle.

Apart from that, Docker also does many other things, like making networking for the containers easy, handling storage, pulling & pushing container images etc.

Docker is here to make our life easier.

Now, let's look at the Docker Container Lifecycle

The container lifecycle is basically a series of stages, starting from the creation of the container to its destruction.

The following diagram is going to make this a lot clear.

Container Lifecycle

Let me explain each stage of the container lifecycle.

  • Creation: Many people think that when you run a container, it's one single step. But that's not the case. There is a process which creates the containers first before anything else. More on this later.
  • Started/Running: Once a container is created, it can be started, after which the status changes to Running. This is when the container is actually doing something.
  • Paused: Traditionally to pause a process (which containers basically are), we use the SIGSTOP signal, and to unpause, the SIGCONT signal. But since this signal is observable by the processes, instead of signals, cgroup freezers are used. This way the processes are suspended by freezing the cgroup.
  • Exited/Stopped: The opposite of Running, but not the same as Paused. Stopping a container means sending the SIGTERM signal to the main container process, i.e. PID 1 in the container namespace. It then waits for 10 seconds to let the process exit gracefully. If it doesn't, a SIGKILL signal is sent, and we all know what that means, don't we?
  • Destroyed/Deleted: The container doesn't exist anymore, all the resources that it once allocated, is now gone.

I hope this has made the concept of container lifecycle clearer now. In the following section I'll go through all the specific docker commands that'll help you in managing all these states of containers.

Docker Commands to Manage Container Lifecycle

All the commands (subcommands, if being more specific) that control the container lifecycle belongs to the subcommand container.

In a terminal if you execute docker container --help you'll get a list of subcommands that are associated with multiple operations performable on containers.

But we're not concerned about all of them here. Let's focus only on a specific subset of those.

Container Creation

Container creation is handled by the following commands:

  1. docker container create
  2. docker create

The first one is the long form, while the second one, you guessed it right, is the short form.

I recommend you use the long form as it's more verbose and something I consider a good practice.

What is container creation?

Container creation is creating the R/W layer on top of the specified image's R/O layer[s], and preparing it to run the required program. Creating a container does not start the actual process, you can specify all the configuration parameters like capabilities, CPU limit, memory limit, container image etc right at the creation stage and then start the container anytime you want without having to re-specify those parameters.

Read docker container create --help for a list of configurable options.

Example

First create a container like the following (I used an alpine image here to run the command sleep infinity)

docker container create --name alpine alpine:latest sleep infinity

Once the container is created, you should get the ID of it in the terminal screen. Now you can filter the container list to find the containers that aren't running, but just created.

docker container ls --filter=status=created

Here I'm filtering the list so that I get only the containers that are created. Take a look at the STATUS column, that's what specifies a container's state. You should see something like the following:-

➟ docker container ls --filter=status=created
CONTAINER ID   IMAGE           COMMAND            CREATED          STATUS    PORTS     NAMES
3f8d56fb3f78   alpine:3.13.4   "sleep infinity"   17 seconds ago   Created             alpine

You can also inspect the container and see what state it is in.

➟ docker container inspect -f '{{json .State.Status}}' alpine
"created"

Using docker create will not give you anything different.

Container Startup

To start a container, use one of the following two commands:-

  1. docker container start
  2. docker start

Same thing here, one long and one short version. And I recommend you use the ...container start command.

What is container startup?

Once a container has been created, you can start it. Starting the container means actually running the containerized application.

While creating a container merely prepares all the configuration (how the container will run, runtime environment, runtime constraints, container name etc), it does not allocate any of the resources.

Starting a container allocates the necessary resources and runs the application.

Example

Consider the previous container that we created. Simply start the command with the container name or ID like this:

docker container start alpine

Once the container is started, it's status changes from Created to Running. In the container list, you can filter by the status again like you did previously:-

docker container ls --filter=status=running

On the STATUS column, instead of Running it'll show how long the container has been running since it started.

➟ docker container ls --filter=status=running
CONTAINER ID   IMAGE           COMMAND            CREATED         STATUS          PORTS     NAMES
3f8d56fb3f78   alpine:3.13.4   "sleep infinity"   8 minutes ago   Up 18 seconds             alpine

You can also inspect a container to see it's state. I'll use Go templates to format the output to the only thing that I need, i.e. the state.

docker container inspect \
	-f '{{json .State.Status}}' alpine

You should see something like this -

➟ docker container inspect -f '{{json .State.Status}}' alpine
"running"

Not to sound redundant, but docker start will do just the same thing.

The Special Docker Run Command

This is a special command that ties the container creation and startup together and this is the command that we all use the most.

  1. docker container run
  2. docker run

These commands essentially (i) creates the container and then immediately (ii) starts the same container.

This is useful because creating and leaving a container to start at a later time isn't something we do all too often. When you are at the command line, with the docker CLI, you are here to run a container. And that's exactly what ...container run command does.

Example

Example of this command is very simple. To run a container, instead of docker container create, use docker container run.

You will have to pass all the options you'd pass to docker container create to this docker container run command. This is because docker needs all that information right at that moment since there no more second steps.

➟ docker container run --name echo-me alpine:3.13.4 echo "Subscribe to Linux Handbook"
Subscribe to Linux Handbook

 

 

Container Pausing

Pausing a container means exactly how it sounds. We're not stopping any processes, just pausing it. So if a process inside a container is counting from 1 through 100, and it was paused while it was at count 50 and then unpaused at a later time, the count will resume from 50.

One thing to understand is that while Paused is an actual state, Unpaused is not. When you're unpausing some paused container, you're essentially changing the state from Paused to Running.

The commands that are used to pause a container are as follows:-

  1. docker container pause
  2. docker pause

Similarly you can unpause a container with the unpause counterparts:-

  1. docker container unpause
  2. docker unpause

How a container is paused?

A process is paused by using the SIGSTOP signal, and to unpause, the SIGCONT signal is used. Since these signals can be observed by the processes, instead of signals, cgroup freezers are used. This way the processes are suspended by freezing the cgroup. Freezing a cgroup freezes all of its tasks, and all of its child cgroups.

Example:

For the sake of this demo, I've created a special container. Pull the image by executing the following command:-

docker image pull debdutdeb/pause-demo:v1

You can check the Dockerfile and the code right here.

Once pulled, on one terminal, start a container with this image

docker container run --name pause-demo debdutdeb/pause-demo:v1

It should now show counting starting from 0 and going. Should look something like the following

➟ docker container run --name pause-demo debdutdeb/pause-demo:v1 
Count at 30

On another terminal, pause this container.

docker container pause pause-demo

The moment you're pausing the container, you should see the countdown stop, but the terminal isn't back to you yet. This is because the container isn't yet stopped (I'll talk about this later in this article). Once you unpause this container, countdown should resume, not restart.

docker container unpause pause-demo

To get the terminal back, on a separate terminal run

docker container stop pause-demo

Here is a short video that'll show this in action:-

 

Before stopping the container you can like the previous ones, check the state of the paused container like so:

➟ docker container inspect -f '{{json .State.Status}}' pause-demo 
"paused"

Container Stopping

You can stop a Docker container using one of the following two commands:-

  1. docker container stop
  2. docker stop

Stopping a container means stopping all the processes associated with it.

This is not the same as pausing a container. If you stop a container, you can restart it, but the processes are not going to resume from the state that they were previously in. A process can save its state information in a "file", and restore its state from that file, but that's a different case.

For example, after you stopped the previous pause-demo container, if you try and restart it using the ...container start command, it'll start counting from the beginning, unlike its pause counterpart which resumes the counting.

How the process works?

When you ask Docker to stop a container, it sends a SIGTERM signal to PID 1 of the container. After that, it waits for 10 seconds, which is its graceful time period, the process is given this time to gracefully exit, in other words clean up and finish whatever it was working. After the 10 seconds is over, Docker sends a final SIGKILL signal, I don't have to tell you what happens next.

Docker sends the signal to PID 1 of the container because every other process is a child of PID 1, if this process gets terminated/killed, the chil processes will automatically cease to exist.

This become clearer with the example below.

Example:

You need to pull an image, again.

docker image pull debdutdeb/stop-demo:v1

For the Dockerfile and source code, check out this gist.

In one terminal, start a container from this image.

docker container run --name stop-demo debdutdeb/stop-demo:v1

Once executed, you should see a screen telling you its PID and that it's waiting.

➟ docker container run --name stop-demo debdutdeb/stop-demo:v1 
My PID in this container is 1
Waiting...

Open another terminal, and execute the stop command on this container.

docker container stop stop-demo

Once run, you should see on the terminal that was running the container, telling you what IT is experiencing, i.e. receiving the SIGTERM signal.

Afterwards it starts counting the seconds, and you'll see after 10 seconds it just stops counting. This isn't because I hard-coded it that way, actually I hard-coded it to go on infinitely.

But after 10 seconds docker sends a SIGKILL signal that we can't catch or ignore, the process is bound to be killed.

You can change the time docker waits before sending the SIGKILL signal by using the -t option with docker container stop.

Once everything is done, you should see something like this

➟ docker container run --name stop-demo debdutdeb/stop-demo:v1 
My PID in this container is 1
Waiting...
SIGTERM ignored,
Starting count..
10 seconds

Here is a small video demonstrating this:-

 

You can check the state of this container now using ...container inspect like so:-

➟ docker container inspect -f '{{json .State.Status}}' stop-demo
"exited"

Container Deletion

A container is deleted using one of the following two commands:-

  1. docker container rm
  2. docker rm

Container deletion is the opposite of container creation. When you delete a container, it's gone, you can't bring that specific container back. You can start a new one from the same image using the same configuration, make it identical, but it won't be that exact previous one.

Example:

For an example just remove the previous stop-demo container.

docker container rm stop-demo

Until now, all the containers that you created and then stopped can be removed with a single command:-

docker container prune

This will delete all the containers with the status Exited.

Example output:

✗ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
ffbdd621c01d0eb3f42d348eeb75c731ddd9bd85674bb90dece32bd70357e541
21b9ed6a6198cd6ee7e162aebd936ae3e93b3b0f738161924a825a27794f2b20
f5f5149866a6ced675ad3bfff0b7386f75ee3fdbddca86be8b8ba341dba4b27f

Total reclaimed space: 0B

So, you learned the container life cycle concept. And you also learned the Docker commands to manage each stage of the lifecycle of the containers. If you want some more Docker tips, you may learn about these lesser known but useful Docker commands.

Docker run, Docker start and Docker create 차이

Docker create : 도커 이미지에서 새로운 컨테이너를 생성합니다. 그러나 즉시 실행되지는 않습니다

Docker start : 중지된 컨테이너를 시작합니다. docker create 명령을 사용하여 컨테이너를 만든 경우 이 명령으로 시작할 수 있습니다.

Docker run : create와 start의 조합으로 새 컨테이너를 생성하고 시작합니다. docker run 명령은 로컬 시스템에서 이미지를 찾지 못하는 경우 Docker Hub에서 이미지를 가져와서 컨테이너를 생성하고 실행합니다.

 

<예제로 알아보기>

예제를 실행하기 전에 Docker가 설치되어 있어야 합니다.

설치가 되어있지 않으면 도커 설치 문서를 참조하세요

 

docker pull 을 사용하기 위해서는 docker 에 login 되어 있어야 합니다. docker에 login 했다고 가정합니다.

 

docker pull 명령어로 ubuntu를 다운로드합니다.

$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
ea362f368469: Pull complete
Digest: sha256:b5a61709a9a44284d88fb12e5c48db0409cfad5b69d4ff8224077c57302df9cf
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest


docker images 명령어로 시스템에서 사용 가능한 모든 도커 이미지를 볼 수 있습니다. 우분투 이미지로 설명합니다.

$ docker images
REPOSITORY                   TAG         IMAGE ID       CREATED       SIZE
ubuntu                       latest      d13c942271d6   9 days ago    72.8MB

 

docker create 명령어로 ubuntu 이미지로 container-1 이라는 이름의 새로운 도커 컨테이너를 생성합니다.  

$ docker create -i --name container-1 ubuntu
468813d90e4cd70bcb9f939f9fbdcfa960840a8e4cbc135928fcfb5d3a61a26f

 

docker ps 명령어로 현재 실행되고 있는 컨테이너를 조회하면 방금 생성한 컨테이너가 보이지 않습니다. 왜냐하면, 생성은 되었지만 실행이 되지 않았기 때문입니다.

$ docker ps
CONTAINER ID   IMAGE          COMMAND        CREATED      STATUS     PORTS           NAMES

 

docker ps -a 명령어로 실행중이거나 중지된 모든 컨테이너를 조회해보면 STATUS가 Created이고 이름이 container-1인 컨테이너를 볼 수 있습니다.

$ docker ps -a
CONTAINER ID   IMAGE                             COMMAND                  CREATED         STATUS                    PORTS                              NAMES
71c041429247   ubuntu                            "bash"                   2 minutes ago   Created                                                      container-1

 

docker run 명령어로 container-2라는 이름의 컨테이너를 생성하고 실행해 보겠습니다.

$ docker run -d -it --name container-2 ubuntu
c28cec7d600b3379c0d9440378427dd7343bca3d02777d62bba79d6f08c6f003

container-2 가 실행되고 있고 STATUS가 Up인 상태인 것을 볼 수 있습니다. 즉, docker run 명령어는 컨테이너를 생성하고 시작합니다.

$ docker ps
CONTAINER ID   IMAGE                             COMMAND                  CREATED          STATUS                       PORTS                              NAMES
c28cec7d600b   ubuntu                            "bash"                   18 seconds ago   Up 17 seconds                                                   container-2

 

docker stop 명령어로 실행 중인 container-2 컨테이너를 중지합니다.

$ docker stop container-2
container-2

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

$ docker ps -a
CONTAINER ID   IMAGE                             COMMAND                  CREATED          STATUS                            PORTS                              NAMES
c28cec7d600b   ubuntu                            "bash"                   3 minutes ago    Exited (137) About a minute ago                                      container-2
71c041429247   ubuntu                            "bash"                   25 minutes ago   Created                                                              container-1

 

docker start 명령어로 docker create로 생성한 container-1 컨테이너를 실행해 봅니다. 아래와 같이 컨테이너가 실행된 것을 확인할 수 있습니다.

$ docker start container-1
container-1

$ docker ps
CONTAINER ID   IMAGE                             COMMAND                  CREATED              STATUS                       PORTS                              NAMES
468813d90e4c   ubuntu                            "bash"                   About a minute ago   Up 2 seconds                                                    container-1

 

도커 컨테이너 및 이미지 삭제

docker rm -f 명령어로  container-1 컨테이너를 강제 삭제합니다. 즉, 실행중인 컨테이너를 중지하고 컨테이너를 삭제합니다.

docker rmi 명령어로 ubuntu 이미지를 삭제합니다. 이제 컨테이너도 이미지도 남아 있지 않습니다.

$ docker rm -f container-1
container-1

$  docker rmi ubuntu
Untagged: ubuntu:latest
Deleted: sha256:d13c942271d66cb0954c3ba93e143cd253421fe0772b8bed32c4c0077a546d4d

 

아래와 같이 docker run 명령어를 수행합니다. local 서버에서 이미지를 찾을 수 없다는 메시지와 함께 이미지를 라이브러리에서 Pulling하고 설치하고 실행합니다.

$ docker run -d -it --name container-3 ubuntu
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
ea362f368469: Pull complete
Digest: sha256:b5a61709a9a44284d88fb12e5c48db0409cfad5b69d4ff8224077c57302df9cf
Status: Downloaded newer image for ubuntu:latest
4b3269f98b4dc91fd934bd039d8141dc6a6e6ffb763d2c758238a1d005469220

다운로드된 이미지와 시작된 컨테이너을 확인합니다.

$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED              STATUS              PORTS     NAMES
4b3269f98b4d   ubuntu    "bash"    About a minute ago   Up About a minute             container-3

$ docker images
REPOSITORY                   TAG         IMAGE ID       CREATED       SIZE
ubuntu                       latest      d13c942271d6   9 days ago    72.8MB

위와 같이 이미지가 다운로드되었고 컨테이너가 시작된 것을 확인할 수 있습니다.

 

결론적으로 docker run 명령어는 pull, create, start 를 상황에 따라 수행하는 명령어입니다. 즉, 이미지가 없을 때는 pull, create, start를 이미지가 있을 때는 create, start를 수행하고 컨테이너가 있을 경우에는 start를 수행합니다.

 

 

'Docker' 카테고리의 다른 글

헷갈리는 Docker - 수정  (0) 2021.12.26
Docker Commands for Managing Container Lifecycle  (0) 2021.12.25
window10에 도커 설치하기  (1) 2021.12.25
도커란  (0) 2021.12.25
도커 명령어 - 수정  (0) 2021.12.25

윈도우에 Docker를 설치하기 위해 반드시 WSL2나 Hyper-V backend and Windows containers 가설치되어 있어야 합니다.

본 문서는 윈도우10의 WSL2(Window Subsystem for Linux) 기반으로 Docker를 설치하는 내용을 설명합니다.

설치요구사항

 

도커 다운로드/설치하기

Docker를 설치하기 위해 https://docs.docker.com 이나 https://hub.docker.com 사이트에서 Docker 설치파일을 다운로드 합니다.

 

1) docs.docker.com 사이트에서 다운로드

다운로드 URLhttps://docs.docker.com/desktop/windows/install/

아래 화면을 클릭하면, Docker Desktop Installer.exe 파일이 다운로드됩니다.

2) docker hub 사이트에서 다운로드

다운로드 URL : https://hub.docker.com/editions/community/docker-ce-desktop-windows/

에 접속하여 아래 화면의 이미지를 클릭하여 Docker Desktop Installer.exe 설치파일을 다운로드 합니다.

 

 

Docker Desktop 설치

  1. 다운받은 Docker Desktop Installer.exe 파일을 더블 클릭
  2. Configuration 화면에 Install required Windows components for WSL 2 option 을 선택 후 [OK] 클릭
  3. 설치가 진행되고 설치가 완료되면 [Close] 버튼 클릭

Docker Desktop 설치 완료 화면

Docker Desktop 시작

Docker Desktop 시작하려면:

  1. Window 로고 키를 클릭한 후 Docker를 입력한 다음 Docker Desktop 을 클릭합니다.

Quick Start Guide

초기화가 완료되면 Docker Desktop이 "빠른 시작 가이드"를 시작합니다. 이가이드에는 Docker 이미지를 빌드하고, 컨테이너로 실행하고, Docker Hub에 이미지를 푸시하고 저장하는 간단한 연습이 포함되어 있습니다.

필요 시 빠른 시작 가이드를 실행하려면 알림 영역(또는 시스템 트레이)에서 Docker 아이콘을 마우스 오른쪽 버튼으로 클릭하여 "Quick Start Guide"를 클릭합니다. 아래와 같은 화면이 나타나면 [Start] 버튼을 클릭합니다.

 

 

Start the tutorial

1단계. clone a repository

 

아래 화면에서 빨간색 박스가 표시된 곳을 클릭하여 repository를 복제합니다. gitbhub repository 에서 alpine/git 이미지를 로컬 서버에 다운으로하여 repo라는 컨테이너를 생성하고 getting-started 폴더를 현재 위치에 복사합니다.

 

2단계. build the image

아래 화면에서 빨간색 박스가 표시된 곳을 클릭하여 1 단계에서 복사한 디렉토리에서 Dockerfile을 가지고 docker101tutorial 이름의 image를 생성합니다. 

 

3단계. Run container

아래 화면에서 빨간색 박스가 표시된 곳을 클릭하여 2단계에서 생성한 docker101tutorial 이미지로 docker-tutorial 컨테이너를 생성하고 실행합니다.

 

docker run 에 사용된 옵션

  • -d  :  컨테이너를 detached mode (in the background)로 수행
  • -p 80:80 : 호스트의 포트 80을 컨테이너의 포트 80에 매핑
  • docker-tutorial : 컨테이너 이름

 

 

4단계. 이미지 저장 및 공유

아래 화면에서 빨간색 박스가 표시된 곳을 클릭하여 2단계에서 생성한 docker101tutorial 이미지에 tag를 붙이고 docker hub에 저장합니다.

 

docker hub에 접속해보면 위에서 저장한 도커 image를 확인할 수 있습니다.

 

 

The Docker Dashboard

Docker 대시보드는 Mac 및 Windows에서 사용할 수 있습니다. 컨테이너 로그에 액세스할 수 있고, 컨테이너 내부 쉘에 접근할 수 있으며, 컨테이너 중지, 제거 등을 쉽게 관리할 수 있습니다.

 

Docker Desktop terms

대규모 조직(직원 250명 이상 또는 연간 매출 천만 달러 이상)에서 Docker Desktop을 사용하려면 유료 Docker 구독이 필요합니다. 
발효일은 2021년 8월 31일이지만 유료 구독이 필요한 경우 2022년 1월 31일까지 유예 기간이 있습니다.
자세한 내용은 Docker is Updating and Extending Our Product Subscriptions 참조하십시오.

 

'Docker' 카테고리의 다른 글

헷갈리는 Docker - 수정  (0) 2021.12.26
Docker Commands for Managing Container Lifecycle  (0) 2021.12.25
Docker run, start, create 차이  (0) 2021.12.25
도커란  (0) 2021.12.25
도커 명령어 - 수정  (0) 2021.12.25

Docker 개요

Docker는 애플리케이션을 개발, 적재 및 실행하기 위한 개방형 플랫폼입니다. Docker를 사용하면 애플리케이션을 인프라에서 분리할 수 있으므로 소프트웨어를 빠르게 제공할 수 있습니다. Docker를 사용하면 애플리케이션을 관리하는 것과 동일한 방식으로 인프라를 관리할 수 있습니다. 코드를 신속하게 적재, 테스트 및 배포하기 위한 Docker 방법론을 활용하면 코드 작성과 프로덕션 환경에서 실행 사이의 지연을 크게 줄일 수 있습니다.

The Docker 플랫폼

Docker는 컨테이너라고 하는 느슨하게 격리된 환경에서 애플리케이션을 패키징하고 실행할 수 있는 기능을 제공합니다. 격리 및 보안을 통해 주어진 호스트에서 많은 컨테이너를 동시에 실행할 수 있습니다. 컨테이너는 가볍고 애플리케이션을 실행하는 데 필요한 모든 것을 포함하므로 현재 호스트에 설치된 것에 의존할 필요가 없습니다. 작업하는 동안 컨테이너를 쉽게 공유할 수 있으며 공유하는 모든 사람이 동일한 방식으로 작동하는 동일한 컨테이너를 갖게 됩니다.

Docker는 컨테이너의 수명 주기를 관리하기 위한 도구와 플랫폼을 제공합니다.

  • 컨테이너를 사용하여 애플리케이션 및 구성 요소를 개발
  • 컨테이너는 애플리케이션을 배포하고 테스트하기 위한 단위
  • 애플리케이션을 컨테이너 또는 오케스트레이션된 서비스로 프로덕션 환경에 배포합니다. 이는 프로덕션 환경이 데이터 센터, 클라우드 환경 또는 하이브리드 환경에서 동일하게 작동

도커는 어떻게 사용하나?

빠르고 일관된 애플리케이션 제공
Docker는 개발자가 애플리케이션 및 서비스를 제공하는 로컬 컨테이너를 사용하여 표준화된 환경에서 작업할 수 있도록 하여 개발주기를 간소화합니다. 컨테이너는 지속적 통합 및 지속적 전달(CI/CD) 워크플로우입니다.

<예제 시나리오>

  • 개발자는 로컬에서 코드를 작성하고 Docker 컨테이너를 사용하여 동료와 작업 공유
  • Docker를 사용하여 애플리케이션을 테스트 환경으로 구성하고 자동화 및 수동 테스트 실행
  • 개발자는 버그를 발견하면 개발 환경에서 수정하고 테스트 및 검증을 위해 테스트 환경에 재배포
  • 테스트가 완료되면 업데이트된 이미지를 프로덕션 환경에 배포하는 것으로 간단하게 고객에게 수정 사항을 제공

 

반응형 배포 및 확장
Docker의 컨테이너 기반 플랫폼은 이식성이 높은 워크로드를 허용합니다. Docker 컨테이너는 개발자의 랩톱, 데이터 센터의 물리적 또는 가상 머신, 클라우드 공급자 또는 hybrid 환경에서 실행할 수 있습니다.

Docker의 휴대성과 가벼운 특성 덕분에 거의 실시간으로 비즈니스 요구 사항에 따라 애플리케이션과 서비스를 확장하거나 축소하여 워크로드를 쉽게 동적으로 관리할 수 있습니다.

 

동일한 하드웨어에서 더 많은 워크로드 실행
Docker는 가볍고 빠릅니다. 하이퍼바이저 기반 가상 머신에 실행 가능하고 비용 효율적인 대안을 제공하므로 더 많은 컴퓨팅 용량을 사용하여 비즈니스 목표를 달성할 수 있습니다. Docker는 고밀도 환경과 더 적은 리소스로 더 많은 작업을 수행해야 하는 중소 배포에 적합합니다.

 

Docker는 클라이언트-서버 아키텍처를 사용합니다. Docker 클라이언트는 Docker 컨테이너를 빌드, 실행 및 배포 작업을 수행하는 Docker 데몬과 통신합니다. Docker 클라이언트와 데몬은 동일한 시스템에서 실행되거나 Docker 클라이언트를 원격 Docker 데몬에 연결할 수 있습니다. Docker 클라이언트와 데몬은 UNIX 소켓 또는 네트워크 인터페이스를 통해 REST API를 사용하여 통신합니다. 또 다른 Docker 클라이언트는 컨테이너 세트로 구성된 애플리케이션으로 작업할 수 있는 Docker Compose입니다.

 

도커 데몬

Docker 데몬(dockerd)은 Docker API 요청을 수신하고 이미지, 컨테이너, 네트워크 및 볼륨과 같은 Docker 객체를 관리합니다. 

데몬은 Docker 서비스를 관리하기 위해 다른 데몬과 통신할 수도 있습니다.

 

도커 클라이언트

Docker 클라이언트(docker)는 많은 Docker 사용자가 Docker와 상호 작용하는 기본 방법입니다.

docker run과 같은 명령을 사용할 때 클라이언트는 이러한 명령을 dockerd에 보내고 이를 수행합니다. docker 명령은 Docker API를 사용합니다. Docker 클라이언트는 둘 이상의 데몬과 통신할 수 있습니다.

 

도커 데스크탑

Docker Desktop은 컨테이너화된 애플리케이션 및 마이크로서비스를 구축하고 공유할 수 있는 Mac 또는 Windows 환경용으로 설치하기 쉬운 애플리케이션입니다.

Docker Desktop에는 Docker 데몬(dockerd), Docker 클라이언트(docker), Docker Compose, Docker Content Trust, Kubernetes 및 Credential Helper가 포함됩니다.

자세한 내용은 Docker 데스크톱(Docker Desktop)을 참조하세요.

 

도커 저장소

Docker 레지스트리는 Docker 이미지를 저장합니다. Docker Hub는 누구나 사용할 수 있는 공용 레지스트리이며 Docker는 기본적으로 Docker Hub에서 이미지를 찾도록 구성되어 있습니다. 자신의 개인 레지스트리를 실행할 수도 있습니다.
docker pull 또는 docker run 명령을 사용하면 구성된 레지스트리에서 필수 이미지를 가져옵니다. docker push 명령을 사용하면 이미지가 구성된 레지스트리로 저장됩니다

 

도커 오브젝트

Docker를 사용하면 이미지, 컨테이너, 네트워크, 볼륨, 플러그인 및 기타 개체를 만들고 사용합니다. 

Images

이미지는 Docker 컨테이너 생성 지침이 포함된 읽기 전용 템플릿입니다. 이미지는 다른 이미지를 기반으로 추가 사용자 정의를 포함할 수 있습니다. 예를 들어, 우분투 이미지를 기반으로 하는 이미지를 빌드할 수 있고 Apache 웹 서버와 애플리케이션은 물론 애플리케이션을 실행하는 데 필요한 세부 구성정보도 설치합니다.

자신만의 이미지를 만들거나 다른 사람이 만들고 레지스트리에 게시한 이미지를 사용할 수 있습니다. 고유한 이미지를 빌드하려면 이미지를 만들고 실행하는 데 필요한 단계를 정의하기 위해 Dockerfile을 만듭니다. Dockerfile의 각 명령은 이미지에 계층을 생성합니다. Dockerfile을 변경하고 이미지를 다시 빌드하면 변경된 레이어만 다시 빌드됩니다. 이것은 다른 가상화 기술과 비교할 때 이미지를 매우 가볍고 작고 빠르게 만드는 부분입니다.

Containers

컨테이너는 이미지의 실행 가능한 인스턴스입니다. Docker API 또는 CLI를 사용하여 컨테이너를 생성, 시작, 중지, 이동 또는 삭제할 수 있습니다. 컨테이너를 하나 이상의 네트워크에 연결하거나, 컨테이너에 스토리지를 연결하거나, 현재 상태를 기반으로 새 이미지를 생성할 수도 있습니다.
기본적으로 컨테이너는 다른 컨테이너 및 해당 호스트 시스템과 잘 격리되어 있습니다. 컨테이너의 네트워크, 스토리지 또는 기타 하위 시스템이 다른 컨테이너나 호스트 시스템과의 격리를 제어할 수 있습니다.
컨테이너는 이미지와 컨테이너를 만들거나 시작할 때 제공하는 구성 옵션으로 정의됩니다. 컨테이너가 제거되면 영구 저장소에 저장되지 않은 상태 변경 사항은 사라집니다.

<도커 실행 명령의 예>
다음 명령은 우분투 컨테이너를 실행하고 로컬 명령줄 세션에 대화형으로 연결하고 /bin/bash를 실행합니다.

$ docker run -i -t ubuntu /bin/bash

이 명령을 실행하면 다음이 발생합니다.(기본 레지스트리 구성을 사용한다고 가정)

  1. 우분투 이미지가 로컬에 없는 경우 Docker는 수동으로 docker pull ubuntu를 실행한 것처럼 구성된 레지스트리에서 이미지를 가져옵니다.
  2. Docker는 docker container create 명령을 수동으로 실행한 것처럼 새 컨테이너를 생성합니다.
  3. Docker는 컨테이너에 읽기-쓰기 파일 시스템을 최종 계층으로 할당합니다. 이를 통해 실행 중인 컨테이너가 로컬 파일 시스템에서 파일 및 디렉토리를 생성하거나 수정할 수 있습니다.
  4. Docker는 네트워킹 옵션을 지정하지 않았기 때문에 컨테이너를 기본 네트워크에 연결하는 네트워크 인터페이스를 만듭니다. 여기에는 컨테이너에 IP 주소 할당이 포함됩니다. 기본적으로 컨테이너는 호스트 시스템의 네트워크 연결을 사용하여 외부 네트워크에 연결할 수 있습니다.
  5. Docker는 컨테이너를 시작하고 /bin/bash를 실행합니다. 컨테이너가 대화식으로 실행되고 터미널에 연결되기 때문에(-i 및 -t 플래그로 인해) 출력이 터미널에 기록되는 동안 키보드를 사용하여 입력을 제공할 수 있습니다.
    /bin/bash 명령을 종료하기 위해 exit를 입력하면 컨테이너가 중지되지만 제거되지는 않습니다. 다시 시작하거나 제거할 수 있습니다.

container lifecycle

Docker container lifecycle

The underlying technology

Docker는 Go 프로그래밍 언어로 작성되었으며 Linux 커널의 여러 기능을 활용하여 기능을 제공합니다. Docker는 네임스페이스라는 기술을 사용하여 컨테이너라는 격리된 작업 공간을 제공합니다. 컨테이너를 실행할 때 Docker는 해당 컨테이너에 대한 네임스페이스 세트를 생성합니다.
이러한 네임스페이스는 격리 계층을 제공합니다. 컨테이너의 각 측면은 별도의 네임스페이스에서 실행되며 액세스는 해당 네임스페이스로 제한됩니다.

 

다음 단계

'Docker' 카테고리의 다른 글

헷갈리는 Docker - 수정  (0) 2021.12.26
Docker Commands for Managing Container Lifecycle  (0) 2021.12.25
Docker run, start, create 차이  (0) 2021.12.25
window10에 도커 설치하기  (1) 2021.12.25
도커 명령어 - 수정  (0) 2021.12.25

Docker CLI 명령어에 대한 설명이다.

 

Child commands

Command Description
docker attach Attach local standard input, output, and error streams to a running container
docker build Build an image from a Dockerfile
docker builder Manage builds
docker checkpoint Manage checkpoints
docker commit Create a new image from a container’s changes
docker config Manage Docker configs
docker container Manage containers
docker context Manage contexts
docker cp Copy files/folders between a container and the local filesystem
docker create Create a new container
docker diff Inspect changes to files or directories on a container’s filesystem
docker events Get real time events from the server
docker exec Run a command in a running container
docker export Export a container’s filesystem as a tar archive
docker history Show the history of an image
docker image Manage images
docker images List images
docker import Import the contents from a tarball to create a filesystem image
docker info Display system-wide information
docker inspect Return low-level information on Docker objects
docker kill Kill one or more running containers
docker load Load an image from a tar archive or STDIN
docker login Log in to a Docker registry
docker logout Log out from a Docker registry
docker logs Fetch the logs of a container
docker manifest Manage Docker image manifests and manifest lists
docker network Manage networks
docker node Manage Swarm nodes
docker pause Pause all processes within one or more containers
docker plugin Manage plugins
docker port List port mappings or a specific mapping for the container
docker ps List containers
docker pull Pull an image or a repository from a registry
docker push Push an image or a repository to a registry
docker rename Rename a container
docker restart Restart one or more containers
docker rm Remove one or more containers
docker rmi Remove one or more images
docker run Run a command in a new container
docker save Save one or more images to a tar archive (streamed to STDOUT by default)
docker search Search the Docker Hub for images
docker secret Manage Docker secrets
docker service Manage services
docker stack Manage Docker stacks
docker start Start one or more stopped containers
docker stats Display a live stream of container(s) resource usage statistics
docker stop Stop one or more running containers
docker swarm Manage Swarm
docker system Manage Docker
docker tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
docker top Display the running processes of a container
docker trust Manage trust on Docker images
docker unpause Unpause all processes within one or more containers
docker update Update configuration of one or more containers
docker version Show the Docker version information
docker volume Manage volumes
docker wait Block until one or more containers stop, then print their exit codes

 

 

docker login

Docker registry(dockerhub)에 login하는 명령어로 docker registry에 등록된 이미지를 pull하거나 push를 위해 login한다. docker login은 패스워드를 직접 입력하는 방식과 STDIN 을 사용해서 로그인하는 방식이 있는데 보안을 위해 STDIN 방식으로 로긴하는 것을 권장한다.

 

1) STDIN을 사용하면 shll history나 로그파일에 남는 것을 방지

아래 예제는 pass.txt 파일에서 패스워드를 읽어서 login하는 방식이다.

$ cat pass.txt | docker login -u <username> --password-stdin

 

2) 패스워드 직업 입력 login

$ docker login -u <username> -p <password>

 

docker serarch

Docker registry(dockerhub)에 등록된 이미지를 검색하는 명령어이다.

아래는 oracle19 라는 이름이 포함된 이미지를 검색하는 명령어이다.

$ docker search oracle19

 

docker pull

docker pull 명령어로 oracle datbase 19c 이미지를 다운로드한다.

 

$ docker pull banglamon/oracle193db:19.3.0-ee

 

docker images

다운로드한 이미지 목록을 조회한다.

docker run

다운로드한 도커 이미지파일을 이용해서 컨테이너를 실행한다.

$ docker run -d --name oracle19db \
-p 1521:1521 \
-e ORACLE_SID=MONGO \
-e ORACLE_PDB=MONGOPDB \
-e ORACLE_PWD=Oracle123 \
-v /u01/app/oracle/oradata:/opt/oracle/oradata \
banglamon/oracle193db:19.3.0-ee

-d : deatach mode를 나타내며, -d=true 는 detached mode(background mode), -d=false는 foreground mode 로 수행

       -d 옵션만 쓸 경우는 -d=true와 동일

--name : 컨테이너 이름을 지정

-p, --publish : host port를 컨테이너 port로 게시, 앞쪽 port가 host port이고 뒤쪽 port가 컨테이너 port이다.

     ( 예, -p 1621:1521 로 설정하면 호스트에 1621 TCP port로 유입되는 모든 traffic은 컨테이너의 1521 port로 전달)

-e :  컨테이너의 환경변수를 설정. -e 옵션을 사용하면 Dockerfile의 ENV 설정도 덮어쓰게 된다.

    (예, -e ORACLE_SID=MONGO 는 오라클 컨테이너 환경변수 ORACLE_SID를 MONGO로 설정)

-v : 호스트와 컨테이너 간의 볼륨(volumn) 설정을 위해서 사용.

      호스트(host) 컴퓨터의 파일 시스템의 특정 경로를 컨테이너의 파일 시스템의 특정 경로로 마운트(mount)를 해준다.

      (Window : -v d:\temp )

      (Linux : )

 

위 예제는 oracle193db 이미지로 detached mode로 컨테이너를 실행하고

host에 1521 port로 유입되는 traffic을 컨테이너의 1521 port로 전달한다는 명령어이다. (1521은 오라클 리스너 port ) 

docker run 명령어 실행

컨테이너 조회 - docker ps 

docker ps는 현재 실행중인 도커 컨테이너를 조회한다.

docker ps -a는 실행중인 컨테이너와 중지된 컨테이너 모두를 조회한다. 컨테이너가 생성된 것을 확인 할 수 있다.

컨테이너 로그 조회 - docker logs 

컨테이너 실행 명령어 수행 후 바로 컨테이너를 사용할 수 있는 것은 아니다. 컨테이너에 따라 컨테이너를 생성하는데 필요한 설정을 하고 컨테이너를 생성하는데 시간이 소요된다. 오라클 데이터베이스는 오라클 인스턴스와 데이터베이스에 필요한 파일들을 생성하는데 일정 시간이 소요된다. 컨테이너 생성 과정을 모니터링 하기 위해 

docker logs <container name or cotainer id> 명령어를 수행한다. 아래 화면은 DB 설치가 진행 중인 것을 보여주고 있다.

log의 마지막 줄에 "XDB initialized" 가 보이면 정상 설치된 것이다.

컨테이너 접속 - docker exec 

컨테이너 생성이 "XDB initialized" 메시지와 함께 정상적으로 완료되면,

docker exec -i -t <ontainer name or cotainer id> /bin/bash 명령어로 컨테이너에 접속할 수 있다.

$ docker exec -it oracle19db bash
  • -i, --interactive=false : 표준 입력(stdin)을 활성화하며 컨테이너와 연결(attach)되어 있지 않더라도 표준 입력을 유지한다.
  • -t, --tty=false : TTY 모드(pseudo-TTY)를 사용한다. Bash를 사용하려면 이 옵션을 설정해야 한다. 이 옵션을 설정하지 않으면 명령을 입력할 수는 있지만 셸이 표시되지 않는다.
  • ontainer name 을 oracle19db 로 입력한다.
  • bash : 컨테이너 안의 /bin/bash를 실행하여 Bash 셸에 연결한다

 

컨테이너 중지 - docker stop

실행 중인 Docker 컨테이너를 중지하려면 

docker stop <container name or cotainer id> 명령어를 사용한다. 중지하고 싶은 컨테이너 ID나 NAME을 입력한다.

 

컨테이너 시작 - docker start

중지된 Docker 컨테이너를 시작하려면 

docker start <container name or cotainer id> 명령어를 사용한다. 시작하고 싶은 컨테이너 ID나 NAME을 입력한다.

 

컨테이너 재시작 - docker restart

실행 중인 Docker 컨테이너를 재시작하려면 

docker restart <container name or cotainer id> 명령어를 사용한다. 재시작하고 싶은 컨테이너 ID나 NAME을 입력한다.애

 

컨테이너 접속 - docker attach

실행 중인 Docker 컨테이너에 접속하려면 

docker attach <container name or cotainer id> 명령어를 사용한다.  접속하고 싶은 컨테이너 ID나 NAME을 입력한다.

 

컨테이너 삭제 - docker rm

중지된 Docker 컨테이너를 삭제하려면 

docker rm <container name or cotainer id> 명령어를 사용한다. 삭제하고자 하는 컨테이너 ID나 NAME을 입력한다.

실행 중인 Docker 컨테이너를 삭제하려면 

docker rm -f <container name or cotainer id> 명령어를 사용한다. 삭제하고자 하는 컨테이너 ID나 NAME을 입력한다.

 

이미지 삭제 - docker rmi

Docker 이미지를 삭제하려면 

docker rmi <image name or image id> 명령어를 사용한다. 삭제하고자 하는 이미지 ID나 NAME을 입력한다. 

단, Docker 이미지 삭제 시 이미지를 사용하는 컨테이너가 없어야 한다. 이미지를 사용하는 컨테이너가 먼저 모두 삭제되어야 한다.

컨테이너가 존재하는 상태에서 컨테이너와 이미지를 삭제하려먼

docker rmi -f <image name or image id> 명령어를 사용한다. 삭제하고자 하는 이미지 ID나 NAME을 입력한다.

'Docker' 카테고리의 다른 글

헷갈리는 Docker - 수정  (0) 2021.12.26
Docker Commands for Managing Container Lifecycle  (0) 2021.12.25
Docker run, start, create 차이  (0) 2021.12.25
window10에 도커 설치하기  (1) 2021.12.25
도커란  (0) 2021.12.25

+ Recent posts