Docker Command Line with Example – Part 2

Docker Command Line with Example

Docker containers are now one of the most emerging technologies in a day. Docker containers are commonly used on CI / CD (Continuous Integration / Continuous Deployment) platforms. Containers are lightweight VMs (virtual machines) that are used under hypervisor resources (such as RAM, CPU, HDD, and kernel).

When you run the command against the image you basically get the container. After the command is executed at the end of the container, the container will be closed (you will get the container without moving or exiting). If you re-run another command in the same image, then a new container is created.


All the containers created will remain on the host filesystem until you choose to delete them by using the docker rm command

In order to create and run a container, you need to run command into a downloaded image, in this case, Ubuntu, so a basic command would be to display the distribution version file inside the container us Cat Command, as in the following example:

[root@fosnix ~]# Docker run ubuntu cat /etc/issue

The above command is divided as follows:

[root@fosnix ~]# docker run [local image] [command to run into container]

To run one of the containers again with the command that was executed to create it, first you must get the container ID (or the name automatically generated by Docker) by issuing the below command, which displays a list of the running and stopped (non-running) containers:

[root@fosnix ~]# docker ps -l

Once the container ID has been obtained, you can start the container again with the command that was used to create it, by issuing the following command:

[root@fosnix ~]# docker container start c758c33cf44d
[root@fosnix ~]# docker ps -l

Here, the string c758c33cf44d  represents the container ID.

In case the container is running state, you can get it’s ID by issuing docker ps command. To stop the running container issue docker stop command by specifying the container ID or auto-generated name.

[root@fosnix ~]# docker stop adoring_kare

A more elegant alternative so you don’t have to remember the container ID would be to allocate a unique name for every container you create by using the --name option on command line, as in the following example:

[root@fosnix ~]# docker run --name fosnix ubuntu cat /etc/debian_version

Then, using the name that you allocated for the container, you can manipulate container (start, stop, remove, top, stats) further just by addressing its name, as in the below examples:

[root@fosnix ~]# docker start fosnix
[root@fosnix ~]# docker stats fosnix
[root@fosnix ~]# docker top fosnix

Be aware that some of the above commands might display no output if the process of command that was used to create the container finishes. When the process that runs inside the container finishes, the container stops

Run an Interactive Session into a Container

In order to interactively connect into a container shell session, and run commands as you do on any other Linux session, issue the following command:

[root@fosnix ~]# docker run -it ubuntu bash

The above command is divided as follows:

  1. -i is used to start an interactive session.
  2. -t allocates a tty and attaches stdin and stdout.
  3. ubuntu is the image that we used to create the container.
  4. bash (or /bin/bash) is the command that we are running inside the Ubuntu container.

To quit and return to host from the running container session you must type exit command. The exit command terminates all the container processes and stops it.

[root@fosnix ~]# exit

If you’re interactively logged on container terminal prompt and you need to keep the container in running state but exit from the interactive session, you can quit the console and return to host terminal by pressing Ctrl+p and Ctrl+q keys.

To reconnect to the running container you need the container ID or name. Issue docker ps command to get the ID or name and, then, run docker attach command by specifying container ID or name, as illustrated in the image above:

[root@fosnix ~]# docker attach < container id >

To stop a running container from the host session issue the following command:

[root@fosnix ~]# docker kill < container id >

This is for basic container manipulation. In the next tutorial, we will discuss how to save, remove, and run a web server in the docker container.


No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Comments

No comments to show.