10 Must-Know Commands in Docker

ยท

6 min read

10 Must-Know Commands in Docker

If you need an introduction or refresh on Docker and Containers and understand them on a foundational level, please consider looking at my other article on what is docker and why you should care before going further in this one:

In this article I want to highlight the most important and common commands you can run with Docker so it becomes a reference for the community.

  1. Create a Docker container from an image: docker create <image name>. This will create a Docker container from the specified image and prints an ID specific to that container. In other words, the dependencies of that image will be grouped together with a segment of your hardware resources inside a container.

    ๐Ÿ’ก
    Reminder: Every Docker image comes with a list of dependencies or file system snapshot that are necessary for the program inside it to run. Along with that, there also is a default command that runs whenever we run that image inside a Docker container.
  2. Run an already created container using the default run command inside the image: docker start <container ID>. Unexpectedly, this will only print out the container's ID. To watch for any output from the container while it's running we use docker start -a <container ID>.

  3. Create a docker container from an image AND run it using the default command: docker run <image name>

E.g. docker run hello-world. This will run the default command inside the image hello-world which happens to print "hello from Docker!" on the terminal.

๐Ÿ’ก
docker run = docker create + docker start
  1. Create a docker container from an image and run it while overriding the default command: docker run <image name> <overriding command>

E.g. docker run hello-world ls. This will run the ls command inside the image hello-world, but unfortunately that will result with an error. The reason is because in Linux, all commands are executables, and inside the hello-world image there is no executable called ls. So let's try another one:

docker run busybox ls. This will list all the folders inside the busybox image. You can run docker run busybox echo hii and this will print the string "hii" on the terminal.

The ls and echo commands are executable programs that exist in the busybox image but do not necessarily exist in the hello-world image and that's why we managed to execute them and get an output.

๐Ÿ’ก
Note that all the commands I used to overrride the default one create a direct output for us to see. Running other commands like mkdir will require extra steps to see its output.
๐Ÿ’ก
VERY IMPORTANT NOTE: when you have a container that has already been created, you cannot override its default command. That can only occur when you create the container with docker run or docker create.
  1. List all running containers: docker ps

In your case, you will not see any running container because when you execute docker run busybox ls the container gives an output and exits immediately. So I have a screenshot from my system that shows different containers running locally because I am working on a microservices project.

  • The CONTAINER_ID is an ID that can be used for tons of other useful commands.

  • The IMAGE is the name of the Docker Image running

  • COMMAND is the default command that runs inside that container

  • CREATED to know when it was created

  • STATUS to see its status, in my case they are all up and running for 19 minutes.

  • PORTS refers to what ports are open inside that container are open for access from the outside world.

  • NAMES are randomly generated names for every container and can be used like the ID to execute some useful commands.

๐Ÿ’ก
To see all the containers that you created, run docker ps --all or docker ps -a for short.
  1. Delete a stopped container: docker system prune. This deletes all the containers with their image cache from your computer and finally shows you how much space you have freed. For the sake of fun, look how much I removed from my system while writing this article:

  2. See all the logs inside a container: docker logs <container ID>. This does not create or run the container. It only prints the logs inside that container.

  3. Stop a running container: docker stop <container ID>.

  4. Run a second command inside a container other than the default.

Context: run docker run redis. This will create a container from the Redis image and run it. By running that image, a Redis server will fire up and it will be ready to listen to Redis commands from the Redis CLI. If you try to write any Redis commands on your terminal, it won't work for the simple reason that the Redis Server wass created inside a container which is totally isolated from the outside world. So the only way to interact with the Redis Server is to run a command inside that container.

To do that, docker exec -it <container ID> <command>

  • The exec flag means we want to run another command inside the container.

  • The -it flag allows us to pass a command as input to the container

Run docker exec -it <container ID> redis-cli to start writing some Redis commands inside your container.

What is -it inside docker exec command?

Every process (program) running inside a container actually runs in a Linux virtual machine. You can read my other article here to know more about the fundamentals of Docker:

In Linux, every process has 3 channels of communication:

  • STDIN: used to communicate information into the process.

  • STDOUT: used to output information from that process to our terminal.

  • STDERR: used to output errors from that process to our terminal.

The -it flag is a combination of -i and -t flags. The -i is the STDIN communication channel and the -t flag is the STDOUT communication channel.

Start a command prompt inside a container

Most of the times you will use the exec -it to run a shell, a command processor, inside your container. To do that, run the following commands:

  1. docker exec -it <container ID> sh. The container has to be running and not exited in order to run this command.
๐Ÿ’ก
You can use docker run -it <image name> to open a shell inside a container but that will override the default command of that container and blocks any program inside it from running.

Isolated Containers

Remember that anything done inside a container is not shared with other containers. Every container has its own hard drive and RAM and networking capabilities. Any files/directories created inside a container using the sh command will not affect other containers. To solidify this concept, look at the diagram below:

That's it! Those are the 10 most important Docker commands you have to know to learn Docker effectively.

If you like my style of writing and explaining concepts, please consider subscribing to my YouTube channel here:

ย