Top 10 basic docker commands for devops

Spread the love

Here are the docker top 10 basic docker commands that I use everyday. I call it docker cheat sheet. Since listing just the docker commands will not be helpful I have created these docker commands with examples. Hope you like it.

  1. docker search [keyword]
  2. docker run –name [container] -itd [image]
  3. docker start/stop [container]
  4. docker rm [container]
  5. docker build
  6. docker images
  7. docker ps
  8. docker exec -it [container] bash
  9. docker logs [container]
  10. docker inspect [container]

Whenever you want to search for some image then you will use this command. The command will find and list the match if available from docker hub. Remember I have done this in my previous post. For example, if you want to search for Ubuntu image then you will run the following command:

There are few options which you can use with this command eg: --filter, --format, --limit, --no-trunc, --stars

You can find more description here.

2. docker run –name [container] -itd [image]

docker run will create a container from an image. You must provide an image name from which you want to run a container. In the following example I am running container with name ubuntu_container from an Ubuntu image.

Here,
-i : Keep STDIN open
-t : Allocate a pseudo-TTY
-d : Run container in background and print container ID (detach)

3. docker start/stop [container]

If you want to start a stopped container then you can use docker start command and if you want to stop a running container then you can use docker stop command. Here is how

Stop the container

Start the container

4. docker rm [container]

If you try to run number 2 command twice, then you will get the following error

This is because the container with name ubuntu_container already exists. You need to remove this container before you can create the container with same name. Here is how you remove a docker container

Note: Before you remove the container make sure to stop the container first as shown in number 3. Then use the following command to remove it.

5. docker build

When you want to build your own docker image then you can use this command. To run this command you should first have already created a Dockerfile. Go to the folder where you have the Dockerfile and run the following command

Do not forget the “.” at the end.

In above command

-t : Name and optionally a tag in the ‘name:tag’ format

Note: I have used one liner Dockerfile ie FROM ubuntu:14.01, nothing much for demo.

6. docker images

If you want to list the downloaded images in your local machine then you can use this command. Here is how it looks like

7. docker ps

docker ps will list the running container in your terminal. Just enter the command and you will see it in action

I have one container alpine-container running.

8. docker exec -it [container] bash

What will you do if you want to get inside the running container and execute some command? Easy! you will use this command. Let’s enter inside the alpine-container container. One thing to keep in mind while using alpine image is that we do not have bash shell by default, therefore we use sh instead as in the following command to use sh shell on the container.

Bonus

You can pass environmental variable as follows

9. docker logs [container]

In case of troubleshooting you might want to check the logs of the container. For this docker logs command come in handy.

What is your output?

10. docker inspect [container]

To view the detail about the container such as IP address, mounted volumes, etc, you can use this command

Others

docker pull [keyword]

If you just want to download an docker image in your machine then you can simply use this command. You can only fetch images which are publicly available. If you have your own repository or private repository in docker hub then you need to login first using docker login command.

I have this image already so not much action here.

docker ps -a

By default, docker ps will only show running container. If you want to show stopped containers as well then you can use -a option.

docker rmi [image]

I have talked about removing docker container but not image. So here is how do we remove docker image. The process is same so I have included it in bonus

docker ps -aq

Here, -q option means quiet which will only display the container IDs. You can also use this option when listing docker images IDs. This is helpful for me if I want to delete a list of containers and/or images as following. But take precaution when you do so…

Removing docker containers

Removing docker images

You may see some errors. Check if you have running containers or not, if yes then remove and try again. You can also use -f option to remove them forcefully.

Limit CPU and memory in containers

In docker we can create a cgroup and assign the predefine amount of CPU and memory that it can use.

Here, container can use memory upto 300M and may use unlimited swap memory.

Similarly,

You can set CPU constraint, but for this post the topic is out of scope, I encourage you to checkout the docker documentation for it here.


Spread the love

Leave a Reply

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