Starting with Docker-CLI

Shivam Garg
5 min readMay 8, 2021

Introduction to docker’s command-line interface

You can read the basic concepts of docker written in this amazing Article, Starting with Docker.

Before moving forward to what is Docker CLI, I’ll write something about why I need a docker CLI. I know that Docker daemon is the core process that lets other processes run over it, it manages all the containers, volumes, and other processes required to run a service or a container. Docker provided us a command-line tool that helps us communicate to docker daemon via Rest APIs, that tool is Docker-CLI.

How to Run a process in Docker container?

Well, you can run it by using a Docker Image, a docker image is actually an archive of all the dependencies and files, that are going to be used to run services inside a container.

There are many pre-built images available at DockerHub, a public registry for docker images. Or you can also create an image for your project by using a dokcerfile and docker build command.

# Running a container by using an image from the docker hub

  • Creating a container by using an image from Docker Hub is pretty easy, simply run, docker run docker/whalesay cowsay “I am Mr. Whales” docker CLI will automatically pull docker/whalesay image from Docker Hub and run it.

# Bridging between host and the container, by using port mapping

When I need to access a docker container from my host os, like a web browser, I need to map the port it is running inside the container to a free port outside.

sudo docker run -d --name tomcat-test -p 8080:8080 tomcat
  • -d: A container can be run in two modes, i.e. attach mode and detach mode, Attach mode is when it runs as a foreground process, and
    Detach mode is when it runs as a background process. for detach mode use -d. However, you can attach a background container by using,
    docker attach <container-id>.
  • --name tomcat-test: Assign a name to the container
  • -p 8080:8080: Map a container’s port to the host, After running the command, visit localhost:8080/ in your browser. you’ll find the tomcat server running.

docker run command returns a hash code when ran in detach mode, that is actually the container-id, you can inspect a container using a unique substring from starting of this id like given below, and it will return you all details about your container.

Sudo docker inspect d75d7

Output ~

[
{
"Id": "d75d74e0469872b1da6a66fc6eff056f2266916ca7d9fa18d39016462e7cb047",
"Created": "2021-05-08T06:30:17.184578478Z",
"Path": "catalina.sh",
"Args": [
"run"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 117894,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-05-08T06:30:17.506803127Z",
"FinishedAt": "0001-01-01T00:00:00
...

# Assigning CPU and memory

Sudo docker run --cpus=0.1 --memory=100m tomcat

You can assign CPUs and memory for a docker container by using --cpus and --memory option respectively.

# Listing all the containers or images in Docker

#To list all active containers
docker container ps
docker container ls
#To list all active or inactive containers
docker container ps -a
docker container ls -a
# To list all images
docker image ls

# Stop or Remove a container or image

# To stop a container
docker stop <container-id>
# To remove a container permanently
docker rm <container-id>

Stopping a container will not remove the chanegs that you have made, However removing a Container will remove all the data.

What if you want to save your data before it gets lost, like if you were working with a database or something, You can use volume mapping/mounting.

# What is volume mapping?

You can actually mount an external(host) folder to the docker container’s folder. and this can be done in two ways while creating the container by using -v or--mount option with docker run command.

sudo docker run -v tomcat_data:/usr/local/tomcat/webapps -d -p 8080:8080 tomcatsudo docker run --mount type=bind,source=/var/lib/docker/volumes/tomcat_data/_data,target=/usr/local/tomcat/webapps -d -p 8080:8080 tomcat

-v: it creates a volume named tomcat_data, that you can find by using the command docker volume ls, and then it mounts this volume(folder) inside the tomcat docker container at the location /usr/local/tomcat/webapps.

--mount: takes three key-value pairs type , source and target location and they need to be absolute paths, it does the same things as -v is doing.

Now everything is mapped, if you create anything in this folder inside the docker container it will be available in volume outside the container, and everything from outside, i.e volume will be available inside the docker container.

To test this, create a index.html file inside a folder named test, and move this test folder to volume( /var/lib/docker/volumes/tomcat_data/_data).
then visit localhost:8000/test in your browser. you’ll find your Html file running inside your tomcat server.

# Running a command in a running container

Docker exec command lets you run any command inside the running container.

sudo docker exec -it <container-id> <command>

-i : For taking standard input

-t : Allocate a terminal

docker exec -it 8e4768 /bin/sh

This will take you inside the container running with id 8e4768 and start a shell. You can use it for many testing purposes.

--

--