Docker Commands


This post serves as a quick set of notes on some of the Docker commands. The intent of this post is not to explain what is Docker or how it works. Instead it is just a quick access to various Docker commands.

Docker Machine 

1) List of all Docker machines 
docker-machine ls  

2) Start a Docker machine 
docker-machine start <machine-name>  
Ex: docker-machine start default 

3) Stop a Docker Machine: 
docker-machine stop <machine-name> 
Ex: docker-machine stop default 

4) Set the environment: 
docker-machine env <machine-name> 
Ex: docker-machine env default 


Docker Client: 

1) Pull an image from the docker hub: 
docker pull <image-name> 
Ex: docker pull hello-world 

2) List of images installed on the current computer: 
docker images 

3) Remove an image from the computer: 
docker rmi <image-name | image-id> 
Ex: docker rmi hello-world 
Ex: docker rmi 12324 

4) Run an image: 
1) This basically creates a container with the specified image. 
If the image is not locally installed on the computer, as a first step, it pulls the image autormatically from the docker hub and then runs the image. 

docker run <image-name> 
Ex: docker run hello-world 

2) However, we might have to specify the port in some cases to say on which port the image should run. 

docker run –p <local-port>:<container-port> <image-name> 
Ex: docker run –p 80:80 hello-world 

Local-port: The port on the local computer   
Container-port: The port on the container that needs to be tied to the local port. 

=> In this case, docker runs the hello-world image. Here the port 80 on the local machine connects to the port 80 on the docker virtual machine. 

3) When we want to create a volume (Remember Volumes are the permanent stores): 
docker run –p <local-port>:<container-port> -v <container-volume> <image-name> 
Ex: docker run –p 80:80 -v /var/www hello-world 
=> In this case, docker runs the hello-world image. Here the port 80 on the local machine connects to the port 80 on the docker virtual machine. 
=> In this case the docker volume on the container is at /var/www. 
However, the volume location on the docker host is decided on the fly by docker. 

4) If we want to decide where docker writes the volume file instead of letting docker decide, the following can be done: 
docker run –p <local-port>:<container-port> -v <host-location>:<container-volume> <image-name> 

Ex: docker run –p 80:80 -v $(pwd):/var/www node 
=>pwd = the present working directory on the local machine. 

docker run –it –p 80:3000 –v $(pwd):/var/www -w "/var/www" node npm start 

-it = To exit the run command when command+c is clicked. 
-w = working directory 



5) List of containers on the computer: 

1) List of running containers on the computer 
docker ps 
2) To fetch the list of containers installed: 
docker ps –a 

6) To remove a container: 

1) docker rm <container-name| container-id> 
Ex: docker rm test 

2) To remove the volume along with the container: 
docker rm -v <container-name| container-id> 
Ex: docker rm -v test 

7) Locate a volume: 

This is used to get the location of the volume. The volume is where the writables are persisted by the contianer. 

docker inspect <container-name| container-id>  




Comments

Popular posts from this blog

Dockerfile

ES-6 classes