# Introduction to Docker #1

***originally published on July 28th***
# Container Lifecycle
Since the containers are easy to create, programmers do not manage and use one container. They trash old containers and download updated images and use new containers if needed. 

Create -> Run -> Stop -> Remove -> Create

This process is called "Container lifecycle"


![dockercycle.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1658991635376/zJVnR1m8a.png align="left")

# Basic Container Commands
Containers are manipulated with Docker commands. Docker commands always begin with `docker`, and the next follows the manipulation command, possibly its options, objects, and arguments. If you want to know the all docker CLI command, just open the CLI and type `docker` or more specifically `docker [COMMAND] --help`.


```docker
docker COMMAND [OPTION] OBJECT [ARGUMENT]
```

## 1. `docker ps` (List Containers)

```docker
# List all containers up
docker ps [OPTIONS]
# or 
docker container ls [OPTIONS]
```

### Example
Showing all of the containers `docker ps -a`
```docker
docker ps -a #showing all created containers
CONTAINER ID   IMAGE                    COMMAND                  CREATED              STATUS              PORTS                NAMES
9a4cfd832ff3   docker/getting-started   "/docker-entrypoint.…"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp   strange_williams
41d2cea0da4a   httpd                    "httpd-foreground"       9 minutes ago        Up 9 minutes        80/tcp               apa000ex1
```
`docker ps` show the information as below:
<table border="3">
    <tr>
      <th>name</th>
      <th>Description</th>
    </tr>
    <tr>
      <td>CONTAINER ID</td>
      <td>container ID</td>
    </tr>
     <tr>
      <td>IMAGE</td>
      <td>The image that container is based on</td>
    </tr>
     <tr>
      <td>COMMAND</td>
      <td>The name of program that the container run by default</td>
    </tr>
     <tr>
      <td>CREATED</td>
      <td>The elapsed time after the container is created</td>
    </tr>
     <tr>
      <td>STATUS</td>
      <td>The current container status. `Up` for running. `Exited` for not running</td>
    </tr>
     <tr>
      <td>PORTS</td>
      <td>To connect with the browser, the PORTS need to be configured.</td>
    </tr>
     <tr>
      <td>NAMES</td>
      <td>the name of container. When running containers, by using option `--name` 
              (e.g. `docker run --name apa000ex1 httpd`)</td>
    </tr>

  </table>

## 2. `docker run` (Running a container)

`docker run` command takes responsibility for `pull`, `create`, `start`.
If the images exist, it does not pull the images from Dockerhub.

```console
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
# or
docker image pull [OPTIONS] NAME[:TAG|@DIGEST]
docker container create [OPTIONS] IMAGE [COMMAND] [ARG...]
docker container start [OPTIONS] CONTAINER [CONTAINER...]
```
### Example of naming container and running on background
The example below is for creating docker container from Apach image `httpd`, naming apa000ex1 with `--name apa000ex1` and running on background with `-d`
```docker
docker run --name apa000ex1 -d httpd
f93ca67057cdfec6581ba69f9b4327ee3158f22af8f2d65d64b5df666f711db0
```
### Example of the connecting container port `80` and host port `8080`

Since the containers are not connected to the outside. To connect to the outside, the container port must be connected to the host port.
The example below is for connecting container port `80` and host port `8080` with option `-p`. Container port numbers could be duplicated, but host port numbers are not allowed to be duplicated.

``` docker
docker run --name apa000ex2 -d -p 8080:80 httpd
```
In the section of `PORTS`, `0.0.0.0:8082->80/tcp` shows container port connected with the host ports.

``` docker
docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED              STATUS              PORTS                  NAMES
81baa0ea2d90   httpd     "httpd-foreground"   5 seconds ago        Up 4 seconds        0.0.0.0:8082->80/tcp   apa000ex3
df141ea6b943   httpd     "httpd-foreground"   17 seconds ago       Up 15 seconds       0.0.0.0:8081->80/tcp   apa000ex2
7e31dc026aef   httpd     "httpd-foreground"   About a minute ago   Up About a minute   0.0.0.0:8080->80/tcp   apa000ex1
```

![ファイル_000 (8).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1658909202000/Xe602VumQ.png align="left")

### Example of Creating MySQL Container 
``` docker
docker run --name mysqlcontainer -dit -e MYSQL_ROOT_PASSWORD=password mysql
```





## 3. `docker  stop` (stopping a container)

`docker stop` takes responsibility of stop in the lifecycle. Every time containers are removed, containers ***must be stopped***. CONTAINER can be chosen as a couple of containers, and they could be name or ID (or short version of ID).
``` docker
docker stop [OPTIONS] CONTAINER [CONTAINER...]
```
### Example of stopping multiple containers by name and ID
``` docker
docker stop 2c0b56b11973 apa000ex3
2c0b56b11973
apa000ex3
```

Type `docker ps -a` and see the `STATUS`, `Exited` show the containers are stopped.
``` docker
docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS                     PORTS                  NAMES
2c0b56b11973   httpd     "httpd-foreground"       About a minute ago   Exited (0) 6 seconds ago                          apa000ex1
8998d8314ae3   httpd     "httpd-foreground"       About a minute ago   Exited (0) 6 seconds ago                          apa000ex3
b22624ce25f1   httpd     "httpd-foreground"       About a minute ago   Up About a minute          0.0.0.0:8081->80/tcp   apa000ex2
ab3bf16fa0c7   mysql     "docker-entrypoint.s…"   4 minutes ago        Up 4 minutes               3306/tcp, 33060/tcp    mysqlcontainer
```

## 4. `docker  rm` (removing a container)

`docker rm` takes responsibility of remove. ***`docker rm`*** can remove only stopped containers.
```docker
docker rm [OPTIONS] CONTAINER [CONTAINER...]
```
### Example of removing two containers by name and ID

Each time removing a container, Make sure containers must be stopped, meaning the section of `STATUS` must be `Exited`.
``` docker
 docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                       PORTS     NAMES
9d716a2e64cc   mysql     "docker-entrypoint.s…"   26 seconds ago   Exited (137) 7 seconds ago             mysqlcontainer
192b2b1c589d   httpd     "httpd-foreground"       33 seconds ago   Exited (0) 16 seconds ago              apa000ex1
```

``` docker
docker rm mysqlcontainer 192b2b1c589d
```

## 5. `docker  image rm` (removing an image)

Although containers are removed, the images for them are not removed and are gradually accumulated, possibly causing run out of storage. You can check which image still exist by typing `docker image ls`

```docker
docker image ls
REPOSITORY               TAG       IMAGE ID       CREATED        SIZE
mysql                    latest    38643ad93215   32 hours ago   446MB
httpd                    latest    444f7df01ce9   2 weeks ago    145MB
docker/getting-started   latest    cb90f98fd791   3 months ago   28.8MB
```

Docker image can be removed by doing below.
```docker
docker image rm [OPTIONS] IMAGE [IMAGE...]
```

### Example of Removing multiple images
Like `docker stop` and `docker rm`, `docker image rm` can choose multiple images, and they can both image name and their ID.

```docker
docker image rm mysql 444f7df01ce9
Untagged: mysql:latest
Untagged: mysql@sha256:657d78ee56e09101902673adcdd7d2bf03012e759c1aa525eeca28cb0fe1aa7d
Deleted: sha256:38643ad93215bedea00fedd3d6f2a1c8e1bff3b9a172aa2547fd8b4bac9cfee3
Deleted: sha256:47e742a69a0555678b9e812fa9ecb57da6f0edeff769f9deaa565d4f1a206eaf
Deleted: sha256:451dabedc195bcb12548ec2097c6c37d79763bb9fb9c5d0ab611988858247c38
(ommited)
```

<hr>
**Archive of Docker**
[Introduction to Docker #0 ->](https://kojiro.hashnode.dev/introduction-to-docker-0)
