Below are some basic docker commands which I learnt from PIAIC Docker course by Amir Pinger. Made this gist to have all the basic commands listed together
For official images : docker image pull image_name:tag
For unofficial images : docker image pull username/imagename:tag
- not necessary to write
image
, can also dodocker pull
direct - if no tag given then
latest
tag considered by default.
docker image ls
or docker images
docker image rm imagename:tag
- Images can be removed only when they have no containers in stop or running state.
docker run -it username/imagename sh
-
run : to run the container after creating it
-
-it : to bring the running container in interactive mode i.e. to start its terminal on our command line.
-
sh : type of terminal for interactive mode. Can also use
bash
instead which is another kind of terminal. -
If no name provided for container then docker will give a default name to it
-
After running this command, main terminal will switch to the running containers shell.
-
To exit this interactive terminal and go back to the main terminal, use
exit
.
Note :
-
If the image doesn't exist locally, then it will be downloaded first, then its container is created.
-
exit
will also stop that container. -
For exiting the interactive terminal without stopping the container, use
Clt+p+q
.Once used this -
later if you use
exit
on the shell of this running container, the interactive shell will close without stopping the container.
docker container ls
or docker ps
docker container ls -a
or docker ps -a
will list all containers in any state ( running or stop )
docker exec -it container_name sh
or
docker exec -it container_id sh
docker stop container_name
or docker stop container_id
If container stop is successful, the container_name
or container_id
will be displayed.
docker start container_name
If container start is successful, the container_name
or container_id
will be displayed.
docker container rm container_name
or
docker container rm container_id
Note : container must be in stop state inorder to delete/remove it.
docker container run -it --name cont_name username/imagename sh
docker container run -d -p local_host_port:tcp_port username/image_name
You can see it from browser at localhost:port