Translate

Total Pageviews

My YouTube Channel

Sunday, 1 September 2019

Old Way vs New Way to Run Docker Commands - Part 7

You can run the commands in your terminal in old way or new way. Output will remain same. They were changed because as the list of sub-commands grew, the team at docker determined that it would be best to allow users to specify which section of the docker command the user was going to use. So because the sub-command list grew as in docker network create, or docker volume ls, they wanted to specify docker container stop instead of solely docker stop.  The last two do the same thing, but in order to keep uniformity of docker <subcommand> argument  where subcommand is the area in docker you want to control, and argument is what you want to do with it.
To Check other parts of this series:-
Part 1
Part 2
Part 3
Part 4
Part 5
Part 6
Part 7



Command Meaning
Old Way to Run Docker Commands
New Way to Run Docker Commands
To Run a Container
Docker run
Docker container run
To Exit and Stop a Container
Ctrl + c (If running only in windows then docker stop is needed to stop a container)
Ctrl + c (If running only in windows then docker stop is needed to stop a container)
To list running containers
Docker ps
Docker container ls
To stop the container process but doesn’t remove it
Docker stop <container id>
Docker container stop <container id>
To list all containers created till date
Docker ps --all
Docker container ls --all
To Start stopped container
Docker start <container id>
Docker container start <container id>
To get the logs from the container
Docker logs <container id>
Docker container logs <container id>
To check top processes running inside the containers
Docker top <container id>
Docker container top <container id>
To Remove one or more containers
                                                        
Docker rm <container id 1> <container id 2> …..
Docker container rm <container id 1> <container id 2> …..
To forcefully remove one or more running containers
Docker rm -f <container id 1> <container id 2> …..
Docker container rm -f <container id 1> <container id 2> …..

Docker Containers Truly Isolated With Proof!!!!! - Part 6

As in Part 3 i have discussed containers are truly isolated from one another and allows you to run same application different versions inside two different containers even if these containers are sharing same kernel. Now lets prove it.

1. I have started containers on the same machine but in different terminals on same machine

Container 1 in Terminal 1

Container 2 in Terminal 2

Checking running Containers in Terminal 3

Created one "New Folder" in Container 2

Folder created in one container should not be visible in the another container

It's not available in Container 1. Hence Proved Containers are isolated from one another.

To Check other parts of this series:-
Part 1
Part 2
Part 3
Part 4
Part 5
Part 6
Part 7

Docker Commands - Part 5

In this part of this series, i will discuss more variations in docker commands. To access the other parts of this series:
Part 1
Part 2
Part 3
Part 4
Part 5
Part 6
Part 7
Overriding Default Startup Command in Docker Image
As i have discussed in the previous post that docker image consist of File system snapshot and startup command, now how can you override the default startup command
Example 1:-

Example 2 :-

List Running Docker Containers

In my environment no container is running now as the command "docker run" executed by me in the past with default startup command which is in image but this commands starts the container and immediately stops the container.

I am going to run container for long time by replacing the default startup command.



To change the Randonly Generated Names
docker run --detach --name <anynametoreplacerandomlygeneratedname> <imagename>
Example:-
docker run --detach --name test1 redis

If you want to list all the containers created till date, you can use
docker ps --all

How to Create docker and then Run it

As you have seen the command in previous post "docker run", which creates the container and then run it.

How can you create the container first and then run it.







Running the Stopped container with docker start command

Stopping Docker Containers
It stops the container gracefully:-

It stops the container forcefully:-
 
Removing Stopped Docker Containers
Docker system prune
Docker system prune command will remove
  • all stopped containers
  • all networks not used by at least one container
  • all dangling images
  • all dangling build cache



Retrieving Docker Container Logs
If you have started any container and then stopped it. Now if you want to find out what are the commands executed at that time when container was started can be retrieved through this command

As in this container i have executed ping command due to this it has printed this output


Running Additional Commands Inside container


you can see the difference between above command and this command output, as in this command i have not used -t due to this it is unable to show the output in nice format.


Getting Shell Access in a Container
Rather than running "docker exec" command again and again for executing different commands inside the container, you can access shell inside a container and then run any command rather than exiting the container and running "docker exec" again.

docker exec -it <container id> sh
sh is one example although instead of sh it can be:-
  1. powershell
  2. bash
  3. sh
  4. zsh


You can even use "docker run" command with -it and sh but there are the chances in this case that no processes might be running inside the container.


To check the Metadata of the Container
It shows how image is configured like startup, config, networking, volumes etc.
docker inspect <container ID>
                        or
docker container inspect  <container ID>


To Check Performance Statistics of Containers
docker stats <container ID>
                        or
docker container stats  <container ID>
if "container id" is not used it shows the statistics of all the running containers