Docker cheat sheet - Get to know the most common docker commands

 



Docker is a very popular piece of technology. It is being heavily used for the past several years in Development/QA teams. In this post, we are going to list the most common docker terminal commands that will make your life easier, and serve as a glossary/cheat sheet when you need a reminder on how to perform a certain operation. 

On this note, if you haven't read my previous articles about docker I would strongly advise you to start from reading those as well.


$docker login <repository>

This command allows you to login to docker hub or any other remote repository of your choice. 


$docker ps

This command will list all currently running containers on the machine it is executed on.


$docker ps -a/--all

This will list all containers both running and stopped.


$docker run <image>:<version> / $docker container run <image>:<version>

$docker run will do the following:


Check if the image exists locally

                                                     => If it does:

                                    it will check if the version matches. 

If not: It will pull the latest version content

If it does: It will create a container from it and start the container.

                                                      => If is doesn't:

                                    it will pull the image from a remote repo - create a container from it and start the container.


$docker pull <image>:<version>

Pull the specified image from a remote repo. If no version is specified, it will pull the latest. Note that the pull is done by image name / tag. This means that the repo you are logged-in to, would be the souse repo from which it will attempt pulling. By default, the pull is made from docker hub since it is the the official default docker repository. 


$docker push <repo/image>:<version>

This command will push a specified image to a repository of your choice. Note that you will need to login prior to pushing.

 

$docker run --name <container-name> <image> -p <external-port>:<container-port>

Now this is an interesting one, this command features several very common and useful [flags]. --name will name a container on creation. Once it is created you can run $docker ps and see that the container received the name you specified. the [-p] flag allows us to map ports. In this example we are  mapping a dedicated external port (for example 3000) to the port exposed by the container. (For example 8080). This basically means you will be able to access the application via port 3000 on the machine in runs. 

For Example:

 $docker run --name my-container my-image:latest -p 3000:8080

Note that the container's docker file needs to in-fact specify a port which is being exposed for this to work.


$docker start <container-id>

Starts an existing container.


$docker stop <container-id>

Stops an existing container.


$docker rm <container-id>

This command will permanently remove a container from the machine. The $docker rm command can also receive several container id's (Meaning stop several containers with a single command line. Note that the containers have to be stopped prior to deletion. Alternatively, you can force remove using the [-f] flag. It is also important to distinguish that it only removes the container. Nothing to do with the image from which it is created from.


$docker images / $docker image ls

This will list all images on the specific machine the command is executed on.


$docker build -t <my-image-tag>:<version>

This command will build and tag a docker image. (You are welcome to read my article about containerizing applications to get a better understanding of this concept).

 

$docker kill <container-id> 

This command will apply a SIGINT event on a running container. In other words it's like executing CTRL + C on it.


$docker container logs --tail <number-of-lines> <container id>

Using this command you can view the logs produces by the container. As you can see you can also apply a flag [--tail] to choose how many line you would like to view. 


$docker network ls

This will list all existing docker networks on the particular machine this command is being executed on.


$docker cp <file-path-locally> <container>:<path-in-container>

Using this, you can copy a file from your machine INTO a container's filesystem. Note that this command can also be reversed in order to copy files FROM a container to your local filesystem.


$docker exec -it <container-id> /bin/bash

Using $docker exec you can "step" into a container. In the above example we are opening an interactive shell inside of the container and allowing the user to run commands in the terminal which are actually being executed inside the container's shell. 




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

In this example of the $docker exec command we can execute a command directly inside the container. As you can see in the screenshot below, we didn't open an interactive shell to the container but instead we received an output of the command we ran.




$docker exec -it -u 0 <container-id> <command>

 This added flag [-u] allows us to run the container's shell as a specific user. In this case we added a [-u] (Meaning user) flag after the [-it] flag and passed 0 as an argument. This means we are running the container's shell as a root user. 


$docker <command> --help

Using this command we can get information about using any docker command and the possible [OPTIONS] available for it.






Comments

  1. Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you Best linux cheat sheet commands service provider.

    ReplyDelete

Post a Comment

Popular posts from this blog

צעדים ראשונים - המדריך לבודק המתחיל חלק א

Understanding XPath