
In this article, I’ve listed important Docker commands, aiming to provide developers with a convenient single-source reference for basic commands.
Installing Docker on Mac
To install the Docker community version, run the command:
brew install docker --cask
Once it’s installed successfully, launch the Docker application and verify it with the command:
docker ps
To build a docker image
To create a new Docker image ,user has to create Dockerfile and define the image.
While building a image ,please make sure that the current directory having the docker file named Dockerfile. Because docker engine look for the name “Dockerfile”
docker build -t <Docker image name> .
Ex: docker build -t mysqlImage:1.0 .
Usage:
-t – tagging the image .Ex test_image:1.0
. (dot) – It saying that docker file is in current directory
If the docker file name is different, then we need to specify the docker file name with -f option.
docker build -t <Docker image name> -f <Docker file name > .
Ex: docker build -t mysqlImage:1.0 -f mysqlDockerFile
To run a Docker Image
Once the image build successfully then it’s ready to run as container .To see the list of images by below command .
docker images
<You can see list of images after running the above command>
Now specify the image to run .
docker run <Docker image name>
Once the container created successfully ,you can view the list of container using the command ,
docker ps
<This command list the containers with container id ,image name ,status ,port and etc..,>
To check the container logs .
docker logs <container id >
To go to inside the container for debug .
docker exec -it <container id> bash
Push the image to Artifactory
To make the image for public use , push the image to Docker Aritfactory .
Log into Artifactory and then make sure you have read and write permission for Docker repo.
docker login -u <username> <docker repo name>
Once you are able to login and having the required permission then push the image using the below command .
docker push <Docker repo>/<imagename>:<tag>
To push the image in particular folder then use the below command ,
docker push <Docker repo>/<foldername>/<imagename>:<tag>
I recommend to push the image in particular folder which makes your repo clean and aligned otherwise it will mesh up the repository
Clean up images in local repository
Always make sure that remove the images which is no longer useful otherwise it will slowdown the docker process .
To delete the image use the command
docker rmi <image name >
To delete the associated images then use the command
docker rmi -f <image-id> [recommended]
Also run the below command to clean the dangled images .
docker system prune
Port forwarding
We have to make few changes in docker file and docker command for accessing the Container port to localhost .
Step 1 : Mention the port in Docker file for exposing to outside
EXPOSE 8080,5567
Step 2 :Use the following command to access in localhost
docker run -p <localhost port ref>:<container port >
Ex : docker run -p 28080:8080. You can access the container link via http://localhost:28080.Please note here i mentioned the local port forward is 28080
Mounting container folder [Copy logs ]
If the logs are stored at particular location in container and if you want to view the ongoing result then you can use container mount command .
docker run -v ~/<localhost folder>:/<container-foldername 1>/<container-foldername2> -t <image-name>
~ – This refer to current home dir in linux machine.
Ex : docker run -v ~/test-result:/opt/tmp/logs -t myimage:1
Copy Logs from container to Local machine
Below are the simple commands to copy the logs from container to local machine
The below command will copy the logs to current directory. Note down there is a dot at the end.
docker cp <contaner-name>:/dir1/dir2/file1.txt .
The below command will copy the logs to specified location
docker cp <contaner-name>:/dir1/dir2/file1.txt /localdir1/localdit2/file2.txt
Passing ENV variable
We can pass the ENV variable during docker run .If you have one or two variable you can use the below command .
docker run -e KEY=VALUE -e KEY=VALUE <image name>
If you have more no of ENV variable then add all of them in a file and inject them during docker run.
cat env.list
DB_HOSTANAME=sns
DB_USERNAME=root
DB_PASSWORD=test123
docker run –env-file <env-file-path > <image name>
Verify the newly added ENV variable in container using the below command ,
docker exec -it <container id > env