Here are list of some useful commands for docker
Docker Images :
Docker Environment Variables :
How to check environment set for container ?
docker inspect containerid
Run a container named blue-app
using image asif1202/simple-webapp
and set the environment variable APP_COLOR
to blue
. Make the application available on port 38282
on the host. The application listens on port 8080
docker run -p 38282:8080 -e APP_COLOR=blue --name blue-app kodekloud/simple-webapp
Deploy a mysql
database using the mysql
image and name it mysql-db
. Set the database password to use db_pass123
. Lookup the mysql image on Docker Hub and identify the correct environment variable to use for setting the root password.
docker run -e MYSQL_ROOT_PASSWORD=db_pass123 mysql --name mysql-db

Commands vs Entrypoint
Defines Process with in the containers . eg for nginx image CMD[“nginx”] , for mysql image CMD[“mysqld”] .
Why containers move to exit state? And how can we keep them live using CMD or Entrypoint ?
Let’s say for ubuntu image we specified cmd as CMD[“bash”] .For bash it’s listen inputs from terminal . If terminal not finds container exits. If you are creating ubuntu container and launch bash program. By default docker doesn’t attach to a container when it is run. So the bash program doesn’t finds it’s terminal so exits. Since the process was started when container was created finished. Container exits as well.
To avoid container to exit ,One way is using docker run command. It override default commands within image.
docker run ubuntu sleep 5
Same we can achieve by passing CMD and Entrypoint.
CMD[“sleep”,”5″] – If you want to specify time in command.
Entrypoint[“sleeep”] – In this way you need to pass parameter in docker run command. By default it will take parameter from docker run but if you didn’t specify parameter in docker run It will throw error. To avoid that, Set both CMD instructions and entrypoint as default value but it can be override by passing parameter in run command. In given below example parameter 10 will override default value of sleep 5.
docker run ubuntu 10
From Ubuntu
Entrypoint[“sleep”]
CMD[“5”]
How can you find entrypoint for an docker image ?
By using command , docker inspect imagename
Run an instance of the ubuntu image to run the sleep 1000 command at startup. Run it in detached mode ?
docker run ubuntu sleep 1000
Eg-1 Create a container with name redis and image redis:alpine
docker run --name=redis redis:alpine
Eg-2 Create a simple container called clickcounter
with the image asif1202/click-counter
, link it to the redis
container that we created in the previous task and then expose it on the host port 8085
.The clickcounter
app run on port 5000
.
docker run -d --name=clickcounter --link redis:redis -p 8085:5000 asif1202/click-counter
Eg-3 Create a docker-compose.yml file under the directory /root/clickcounter. Once done, run docker-compose up.
The compose file should have the exact specification as follows –
- redis service specification – Image name should be redis:alpine.
- clickcounter service specification – Image name should be asif1202/click-counter, app is run on port 5000 and expose it on the host port 8085 in the compose file.
services:
redis:
image: redis:alpine
clickcounter:
image: asif1202/click-counter
ports:
- 8085:5000
version: '3.0'
docker-compose up -d
Docker Storage :-
In which docker storage stores the files ?
/var/lib/docker
1- What directory under /var/lib/docker
are the files related to the container (eg. alpine-3)
image stored?
The directory name is the same as the container id.
2- Run a mysql
container named mysql-db
using the mysql
image. Set database password to db_pass123
Note: Remember to run it in the detached mode.
docker run -d --name mysql-db -e MYSQL_ROOT_PASSWORD=db_pass123 mysql
3- Run a mysql container again, but this time map a volume to the container so that the data stored by the container is stored at /opt/data
on the host. Use the same name : mysql-db
and same password: db_pass123
as before. Mysql stores data at /var/lib/mysql
inside the container
docker run -v /opt/data:/var/lib/mysql -d --name=mysql-db -e MYSQL_ROOT_PASSWORD=db_pass123 mysql
Note : In case of database crash data will be stored in /opt/data location only . We can retrieve data by redeploying container with same above command.
A
Commands | Description |
---|---|
docker ps | List all running containers |
docker ps -a | List all containers stopped, running |
docker stop container-id | Stop the container which is running |
docker start container-id | Start the container which is stopped |
docker restart container-id | Restart the container which is running |
docker port container-id | List port mappings of a specific container |
docker rm container-id or name | Remove the stopped container |
docker rm -f container-id or name | Remove the running container forcefully |
docker pull image-info | Pull the image from docker hub repository |
docker pull stacksimplify/springboot-helloworld-rest-api:2.0.0-RELEASE | Pull the image from docker hub repository |
docker exec -it container-name /bin/sh | Connect to linux container and execute commands in container |
docker rmi image-id | Remove the docker image |
docker logout | Logout from docker hub |
docker login -u username -p password | Login to docker hub |
docker stats | Display a live stream of container(s) resource usage statistics |
docker top container-id or name | Display the running processes of a container |
docker version | Show the Docker version information |
docker run | To run the docker container |