Translate

Total Pageviews

My YouTube Channel

Sunday 1 September 2019

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



No comments:

Post a Comment