[BTY#2] 12/07/2023 Docker
Tags: docker
Published: 2023-07-12 06:24:02

### Section 2. Docker Images & Containers: The Core Building Blocks

#### Images & Containers: What and Why?

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zhcmuix4rp1lqc8afs7h.png)

#### Build Docker Image Command

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0q1ntyqdfwezeyrg9wek.png)

- Command

```
docker build -t image_tag .
```
*Example*
> docker build -t VSA:v1.0 .


#### List all of Docker Images
```
docker images
```
or 
```
docker image ls
```

#### Remove an Image
```
docker rmi IMAGE_ID
```
#### Remove all unused images at once
```
docker image prune
```

#### Run a Container based on an Image
- Command
```
docker run -p local_port:container_port --name CONTAINER_NAME IMAGE_ID/IMAGE_NAME
```
*Example*
> docker run -p 3000:80 --name goalsap 3de


#### Start/Stop a Container
```
docker start CONTAINER_ID/CONTAINER_NAME
docker stop CONTAINER_ID/CONTAINER_NAME
```
#### List all of Containers
- List all of running containers
```
docker ps
```
- List all of containers
```
docker ps -a
```
#### Run a Container in interactive mode and be able to enter the input
```
docker run -it CONTAINER_ID 
```
*Example*

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4rkbtoeb3pximnocah6n.png)


#### Execute a Container with interactive mode and be able to enter the input in bashscript
```
docker exec -it CONTAINER_ID /bin/bash
```

#### Remove a Container
```
docker rm CONTAINER_ID
```

#### Remove all stopped containers at once
```
docker container prune
```

#### Remove a container when it is stopped
```
docker run --rm IMAGE_ID
```

#### Understanding a Attached & Detached Containers
- When using ```docker start CONTAINER_ID``` => the Container is running in the background => It is detached mode => Not showing logs in the terminal

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kf0hmpma1bxrpx38nu47.png)

- When using ```docker run IMAGE_ID``` => the Container is running in the foreground => It is attached mode => Showing logs in the terminal

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zisfwnrn4sdlfxh93mql.png)

- Change a container from detached mode to attached mode with the following command
```
docker attach CONTAINER_ID
```
- (Option 2) Using ``` docker logs``` to get all of container logs without having to change mode
```
docker logs CONTAINER_ID 
```
*Example*
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lxnz70pz50ov105q0n1u.png)

or follow the container logs in the real-time like attached mode
```
docker logs -f CONTAINER_ID
```
*Example*
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b2axbuis6rh6qr2p7t3e.png)

#### Start a container with attached mode and interactive mode
```
docker start -a -i CONTAINER_ID
```

#### Run a container with detached mode
```
docker run -d IMAGE_ID
```

#### Copying Files Into & From A Container
- From local into container
```
docker cp local_files CONTAINER_NAME:container_directory
```
*Example*
> docker cp dummy/. boring_vaughan:/test

- From container to local
```
docker cp CONTAINER_NAME:container_files local_directory
```
*Example*
> docker cp boring_vaughan:/test/test.txt dummy