Manish Barnwal

...just another human

Common docker commands

Photo of docker

I recently got to know about dockers. And I love it. For those who don't know what dockers are. Here it is. Dockers help in software development in isolated frameworks.

Say, you are building an application named epsilon-X. epsilon-X relies on packages like numpy, scipy, pandas, other services, and software. Either you install each of these packages on your machine or you create an environment that has all these required packages along with the suitable versions. Docker helps you create that environment so that you don't face the issues like it works in dev environment but fails in the production environment.

Docker ensures that you don't have to worry about the versions of the packages needed for software development. Many times we face issues like it works on my machine, not sure why it is not working on your machine. And reasons for that could be the difference in versions of packages in the machines. Docker helps you abate such issues.

Docker automates the repetitive tasks of setting up and configuring development environments so that developers can focus on their core task of building software.

So basically docker is like an isolated machine (read environment) that has all the required packages and services for your software to run.

Some docker associated terms

  1. Docker file

Docker file is a few lines of code that keeps information about packages and softwares to install. This docker file creates the docker image which sits on top of the virtual machine. There are many containers that run in isolation. Think of each container as a bundle of the packages and softwares required to run the application you want to run.

  1. Containers

Containers are like a group of packages bundled together. Each container has its own space and does not delve into the outside of its space. A docker can have many containers- each container having a different bundle of packages.

I have already talked about the use cases of docker but if you still want to know more about docker, there are a plethora of resources available online. Let us now talk about some of the important docker commands.

Common docker commands

  • docker-compose up

Once you have the docker-file base, you can build the docker and up the containers by typing

docker-compose up -d --build
# Make sure you run the above command from the folder that has the Docker file

If you just want to up the containers(once you have your dockers built),
docker-compose up
docker-compose up -d  # ups the containers in background by passing -d flag
  • docker images

Lists all the docker images running; it also lists all the containers running.

docker images
  • docker ps Lists all the containers running for the docker image.
docker ps
  • docker-compose down

This will shut down the containers

docker-compose down
  • docker restart

This will restart the containers of the docker image

docker restart
  • docker logs

If you want to get the logs of processes running on the worker or master, you can do so by using

docker logs -f docker_worker_1
docker logs docker_master
# This will only give you the tail of the logs.

If you want the complete logs, you can do so by passing additional flag. Something like:

docker logs -f docker_workder_1 --since 60m
# gives you the logs since 60 minutes
  • docker exec If you want to talk to of one of the machines, say you want to go to the bash of docker_master you do so by typing
docker exec -it docker_master bash
# This will take you to the bash of the master

I recently wanted to know what all python packages were installed in my docker. I wanted to know the python packages installed in docker_worker_1 container. You can do so by typing

docker exec docker_worker_1 pip list
  • docker cp

If you want to copy files from docker machines say docker_worker1 to the host, you can do so using docker cp, something like this:

docker cp docker_worker_1:<file-path of source>  <folder-path of destination>

example: docker cp docker_worker_1:/tmp/some-folder/some-file.txt .

Obviously, the above list is not exhaustive. There are many but these are a few that I have used most so far. Let me know in comments what other commands you use.

Advertiser Disclosure: This post contains affiliate links, which means I receive a commission if you make a purchase using this link. Your purchase helps support my work.