Basic Docker Commands
Before this, I explained here, what is Docker, what is a container and why we are using it. I skip to installation because docker installation has differences in each operating system. You can follow the official installation document from here
INFO Commands
When you did to installation successfully, we can start to use docker. Open terminal or PowerShell and write these lines.
1 | docker version |
This command will give you information about the version of your system and docker engine.
1 | Client: Docker Engine - Community |
Another info command is âdocker infoâ shows you informations about configuration values for engine
1 | docker info |
But what about if we write only âdockerâ.When we type to terminal or PowerShell only âdockerâ it prints to us an informative explanation about docker management commands, commands and their options.
Simply we manage docker image and container like that
1 | docker <command> <sub-command> |
For instance, if we want to run a container we need to write
1 | docker container run |
also, we can use this without command, but this is naming as an old way now.
1 | docker run |
Container Management Commands
Container management command for creation, deletion start and stop for container.To explain that i choose to create nginx container.
1 | docker container run --publish 80:80 --detach --name webhost nginx |
** âdetach or -d option runs container in the background.
With this command Docker doing
- Pulling the image of nginx from docker hub if not exist locally
- Creates new container based of that image
- Gives a virtual ip on a private network inside docker engine
- Reserve port 80 from the host machine and forwards to port 80 in container
- Starts container
After container created , you can list all working containers , you should see your container as up and running
1 | docker container ls // or docker ps (old way) |
with -a flag you can list stopped container also.
1 | docker container ls -a |
If you want to learn details of your container configs , inspect command prints it as json
1 | docker container inspect webhost |
If we want to stop our container just need to write a command below with our container name.Remember , our container name was webhost.(When you donât give a name a container , docker gives a random name for it.BTW, you can also use first 3 digits of your container id instead of name)
1 | docker container stop webhost |
To re-run stopped container we change stop as start .
1 | docker container start webhost |
And lastly when we want to remove docker container , we use to ârmâ command.Before remove a container , you need to stop it.Docker gives a warning to prevent deletion of working containers like above.
1 | docker container rm webhost |
Logs and Tasks Management
If we used âdetach and -d , still we can reach logs of the running container
1 | docker container logs webhost |
and for the process list in one container.
1 | docker container top <containername> |
And todays last command coming for to reach performance stats for all containers
1 | docker container stats |
Credits:
Owner of Thumbnail Image : Astrid Westvang