Top Docker Commands explained - Use Them Everyday
Top Docker Commands explained - Use Them Everyday

In this post I want to share with you my most favorite Docker commands that I'm using every single day.

Up and build

If you are using Docker-compose most often you want to rebuild your images and then start them. In order to do that we are typically writing

docker-compose build
docker-compose up

This is totally fine but we can combine directly these 2 commands.

docker-compose up --build

In this it will check all images and build images which have updates and then start your docker-compose.

Processes

The next command that you need really often is to get a list of running docker processes.

docker ps

Docker ps

Here we can see all processes which are running at this moment. Here are all containers which are started. We can see IDs, names and commands which are being executed.

It is extremely important to check what containers are running and it might help you to debug your problem.

Additionally to that when you need a container ID to execute some command you can always take it from the processes.

Executing commands

Another command that I'm using really often is docker exec. It allows us to execute a command inside a container.

docker exec -it mla-web echo "1"

It will execute an echo command inside this container. But I can do better. For example I can open a rails console inside the container to do some commands.

docker exec it mla-web rails c

One more way how we can use docker exec is to jump inside the container shell to be able to access everything from inside.

docker exec it mla-web sh

It will open a shell of this container and now we can execute any commands inside this shell.

Logs

At some point you need to look on your logs to debug problems. For these we can use either docker logs or docker-compose logs.

docker-compose logs --tail 10 web-dev

It means that we want to see 10 last logs from the container web-dev.

Docker logs

Cleaning your system

One more super important command is docker prune. At some point when you develop with Docker you won't have any free space on your computer. It happens really a lot when you test images and recreate containers again and again. At this moment you need to clean everything.

docker system prune

Prune

By default it will remove all stopped containers, unused networks, images and cache.

But additionally to that you might want to clean all images which are not used now because they can also take quite a lot of space.

docker system prune -a --volumes

Now additionally to everything about it will remove all images which are not associated with container.

Docker zero downtime deployment

The last command that I want to show you is amazing for zero downtime deployment. If you are using Docker with Docker-compose in production you for sure want to restart all your containers and update them without downtime.

docker-compose up -d --build

We use this command when Docker-compose is already running on production. This command doesn't duplicate anything, it simply checks all your containers, pulls newer images if they are needed, builds everything and restarts all containers which are needed to be restarted.

And actually if you want to learn how to setup Docker from empty folder to a fully functional deployed application make sure to check Docker and Docker Compose - Project Deployment From Scratch.