Docker commands I use Frequently

Nowadays, docker is an integral part of software development. I use it regularly as part of my job and overtime i have found i use some commands lot more frequently compared to others. If you are new to Docker or revisiting docker after a while i hope you will find this list useful. 

Setup : 

1. Before launching any docker command on your terminal make sure you have downloaded,  installed and started Docker Desktop on your system (Mac/Windows/ Linux).

2. On a Mac you should see the docker icon come up in top right corner of status menu bar. Clicking on it you should confirm "Docker Desktop is running" message is appearing and a green icon next to it. Now you are all set to run the below commands.




3. Confirm docker is running well from command line by running below command in terminal:

         docker 










This should output commonly used docker commands and their brief description. Instead, if you get an error message Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

This means docker daemon is not running or working properly. You likely need to restart Docker Desktop.

Frequently used docker commands:

Now that docker is setup properly. Let's go over some common docker commands: 

1. Listing all running containers:

docker ps

A useful variation of above is when you want to list ALL containers (including the ones that are stopped)

docker  ps -a

2. Sometimes when you run a container it crashes and doesn't launch. Run below command to view container logs to figure out the reason for the crash:

docker logs <container-id>

You can get the container-id by using the command mentioned in Step 1.

3. To change container image name 

docker tag <old-image-name> <new-image-name>

4. To build an Image, you need to provide the path to the dockerfile. It's common practice to have a dockerfile in the root directory of the repo. Assuming this situation run below command to build an image:

docker build -t <docker-hub-username>/<image-name>:<tag> .

5. Once you build an image you may want to push it to a Docker Registry e.g. dockerhub:

docker push <docker-hub-username>/<image-name>:<tag>

6. After you have pushed an image to Dockerhub you may want to pull the image locally: 

docker pull <docker-hub-username>/<image-name>:<tag>

7. But pulling an image won't run it, for that there's below command:

docker run  <docker-hub-username>/<image-name>:<tag>

Docker provides a detailed documentation of it's CLI commands on it's website.  However, knowing the above basic commands and minor variations of these will get you pretty far in your docker journey. 


Comments

Popular posts from this blog

Software Alerting maturity levels

Playwright - The Game Changer Test Automation Tool

New Habits for 2023