Section 2. Docker Images & Containers: The Core Building Blocks
Images & Containers: What and Why?
Build Docker Image Command
- 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
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
- When using
docker run IMAGE_ID
=> the Container is running in the foreground => It is attached mode => Showing logs in the terminal
- 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
or follow the container logs in the real-time like attached mode
docker logs -f CONTAINER_ID
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
Top comments (0)