DEV Community

jaysonnguyen
jaysonnguyen

Posted on • Edited on

[BTY#2] 12/07/2023 Docker

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

Images & Containers: What and Why?

Image description

Build Docker Image Command

Image description

  • Command
docker build -t image_tag .
Enter fullscreen mode Exit fullscreen mode

Example

docker build -t VSA:v1.0 .

List all of Docker Images

docker images
Enter fullscreen mode Exit fullscreen mode

or

docker image ls
Enter fullscreen mode Exit fullscreen mode

Remove an Image

docker rmi IMAGE_ID
Enter fullscreen mode Exit fullscreen mode

Remove all unused images at once

docker image prune
Enter fullscreen mode Exit fullscreen mode

Run a Container based on an Image

  • Command
docker run -p local_port:container_port --name CONTAINER_NAME IMAGE_ID/IMAGE_NAME
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

List all of Containers

  • List all of running containers
docker ps
Enter fullscreen mode Exit fullscreen mode
  • List all of containers
docker ps -a
Enter fullscreen mode Exit fullscreen mode

Run a Container in interactive mode and be able to enter the input

docker run -it CONTAINER_ID 
Enter fullscreen mode Exit fullscreen mode

Example

Image description

Execute a Container with interactive mode and be able to enter the input in bashscript

docker exec -it CONTAINER_ID /bin/bash
Enter fullscreen mode Exit fullscreen mode

Remove a Container

docker rm CONTAINER_ID
Enter fullscreen mode Exit fullscreen mode

Remove all stopped containers at once

docker container prune
Enter fullscreen mode Exit fullscreen mode

Remove a container when it is stopped

docker run --rm IMAGE_ID
Enter fullscreen mode Exit fullscreen mode

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

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

Image description

  • Change a container from detached mode to attached mode with the following command
docker attach CONTAINER_ID
Enter fullscreen mode Exit fullscreen mode
  • (Option 2) Using docker logs to get all of container logs without having to change mode
docker logs CONTAINER_ID 
Enter fullscreen mode Exit fullscreen mode

Example
Image description

or follow the container logs in the real-time like attached mode

docker logs -f CONTAINER_ID
Enter fullscreen mode Exit fullscreen mode

Example
Image description

Start a container with attached mode and interactive mode

docker start -a -i CONTAINER_ID
Enter fullscreen mode Exit fullscreen mode

Run a container with detached mode

docker run -d IMAGE_ID
Enter fullscreen mode Exit fullscreen mode

Copying Files Into & From A Container

  • From local into container
docker cp local_files CONTAINER_NAME:container_directory
Enter fullscreen mode Exit fullscreen mode

Example

docker cp dummy/. boring_vaughan:/test

  • From container to local
docker cp CONTAINER_NAME:container_files local_directory
Enter fullscreen mode Exit fullscreen mode

Example

docker cp boring_vaughan:/test/test.txt dummy

Top comments (0)