Docker Commands

This tutorial acts as a reference to the Docker commands i use everyday and will be good to keep handy if you are a devops/MLOps Engineer. Hoping it helps you all too.

IMPORTANT:
1. We need dockerhub account to install docker

2. If running command on Linux/Unix OS, you need to add your user account to the local docker unix group, else you need to add sudo before every docker command.

Verify Docker Setup/Install

docker version

docker install successful

SUCCESS – docker client and engine are running and talking to each other.

docker is not installed correctly

docker daemon not running

Today morning when i ran the “docker images”, to check all the images present in my system locally, i got the following error:

It clearly means docker daemon is not running in background. The error states it clearly.

FIX: To fix this error, you need to start “Docker Desktop” if on windows or mac.

Docker Commands for Docker Images

Check images present on your local docker host

docker images

When running the command for the first time, after fresh install of docker in your system, you should see the commands output like above.

The screenshot above shows the scenario when you have pulled few images, which are present in your system locally.

Pull Docker Image to your local docker host

docker pull <image name>:<image tag> 
docker pull ubuntu:latest  #Command to pull ubuntu image  

When executing the command above, it connects to dockerhub, default registry in your system (which is why dockerhub account is needed), and pulls the image specified. In this case, it is ubuntu.

Docker Commands for Docker Containers

Launch a container – run command

docker run <image name> : <image tag>
docker run -it <image name>:<Image tag> /bin/bash

Explanation: Docker run tells Docker to start a new container. It tells the Docker engine that we want the container to be based on ubuntu:latest image.
“-it” flag tells Docker to make the container interactive and to attach the current shell to the container’s terminal. To be precise, attach a Bash shell to run inside container.

Get all running containers in your system

docker ps
docker ps -a   # tells docker to list all containers, even those in the stopped state

Attach a shell to the terminal of a running container

docker exec <options> <container-name or container-id> <command/app>

Explanation: As you can see, you can run docker exec command, using both the ID or Name of the container. Option is “-it” which is interactive terminal to Bash shell in the commands above.

Remove Docker Container

docker rm <container Name or ID>

Other useful commands

Get logs of a container

docker logs <container id>

Even the stopped container can emit logs using this command

Removing all stopped container

docker system prune

The screenshot above is self-explanatory. It is very important to run this command time to time, this will clean up resources in your system as well.

Docker Commands List:
1. List running containers: `docker ps`
2. Start a container: `docker start <container_name>`
3. Stop a container: `docker stop <container_name>`
4. Remove a container: `docker rm <container_name>`
5. List all containers: `docker ps -a`
6. Pull an image: `docker pull <image_name>`
7. Run a container: `docker run -d <image_name>`
8. Exec into a container: `docker exec -it <container_name> /bin/bash`
9. Check container logs: `docker logs <container_name>`
10.Build a Docker image: `docker build -t <image_name> .`

References

Ultimate source of truth of docker commands is this page: Docker reference You will find all the variations of the commands explained above in detail in this page.

You can also refer to our own docker resources for deeper understanding of the concepts.

Leave a Comment

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

Scroll to Top