Thursday, August 30, 2018

Introduction to Docker - Day 4

On day 4, we will learn how to manage and clean up containers. Then we will learn how to create common folder to be use in docker container.

Docker Container Management

Listed below are many command that can be used to manage containers. For some function, there are more than one command/method to perform the task.

To list down all container that are active using the command:

docker container ls

To list down all container including non-active containers, use the command

docker container ls -a

To stop an active container

docker container stop <container_name>
docker container kill <container_name>
docker stop <container_name>
docker kill <container_name>

To remove container

docker container rm <container_name>
docker rm <container_name>

To remove all inactive container

docker rm $(docker ps -aq)
docker container prune

Sharing Folder with Containers

We can create a common folder to be used as data dump. After we exit the container, the data folder will still be around. Please follow the following example to create data folder

Use the following command to create a common folder

cd /Users/Shared
mkdir dockerfolder
cd dockerfolder

To run Ubuntu with the data folder, use the command

docker run -it --name myubuntu1 -v $(PWD):/home  ubuntu

The option -v allow us to mount a volume. In this example, we use $PWD which is the current directory (dockerfile) and map the folder to /home folder in Ubuntu.

Write some text as shown below

cd /home
echo "This is a test" >> test.txt

On the Mac OS, navigate to the Shared Folder (/Users/Shared/dockerfile) and check if the file test.txt exist.

Using $PWD is not a very good way as we might be moving in and out of the folder. Use the following command to run and mount the volume:

docker run -it -v /Users/Shared/dockerfolder:/home  ubuntu

We can map any folder in the host system with any existing folder in Ubuntu.

The syntax of folder mapping is as follows:

docker run -it -v <local_folder>:<container_folder> <image_name>


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.