Last active
August 23, 2020 13:49
-
-
Save jagad89/ca822d50d67ba6a12118346d456c8264 to your computer and use it in GitHub Desktop.
docker notes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
docker community installation | |
$ curl -fsSL https://get.docker.com -o get-docker.sh | |
$ sudo sh get-docker.sh | |
Basic docker commands | |
======================= | |
Run - Start a container | |
e.g | |
docker run nginx | |
Above command will download nginx latest image and run the container. | |
------------------------------------------------------------------------------------------------------------------------- | |
ps - list containers | |
e.g to list all running container. | |
docker ps | |
output: | |
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES | |
a0da16a748fc nginx "/docker-entrypoint.…" 32 seconds ago Up 31 seconds 80/tcp thirsty_benz | |
ps command show basic information of container like containerId and containerName | |
adding -a flag, it will list all conatiner present( running or stoped ) on our host. | |
docker ps -a | |
------------------------------------------------------------------------------------------------------------------------- | |
STOP - stop a container | |
To stop a container we need to pass container ID or container name to the stop command | |
e.g | |
docker stop thirsty_benz | |
it will stop the container, and you can check it using "docker ps -a" command | |
------------------------------------------------------------------------------------------------------------------------- | |
Rm - Remove a container | |
if you want to remove container permanentely, you can use rm command | |
e.g | |
docker rm thirsty_benz | |
it will print back the container name if the container removed successfully. | |
you can verify that the container no more listed under "docker ps -a" command | |
------------------------------------------------------------------------------------------------------------------------- | |
images - list existing images on your system | |
e.g | |
docker images | |
it list all images on your sytem with their sizes | |
optput: | |
REPOSITORY TAG IMAGE ID CREATED SIZE | |
nginx latest 08393e824c32 2 days ago 132MB | |
hello-world latest bf756fb1ae65 7 months ago 13.3kB | |
------------------------------------------------------------------------------------------------------------------------- | |
rmi - remove images | |
images eats your disk space[very less than virtual machine ;)]. | |
If the image is no more useful you can remove them using rmi command | |
e.g | |
docker rmi nginx | |
Note: remember to stop and delete containers which are dependent to successfully delete the image. | |
------------------------------------------------------------------------------------------------------------------------- | |
Pull - only download images | |
It will just pull the image, It will not run the image. | |
e.g | |
docker pull nginx | |
------------------------------------------------------------------------------------------------------------------------- | |
Exec - Execute a command on running container | |
e.g | |
docker exec <container_name> <your command> | |
docker exec mynginx nano /var/www/html/index.html | |
------------------------------------------------------------------------------------------------------------------------- | |
Exec - Execute a command on running container | |
e.g | |
docker exec <container_name> <your command> | |
docker exec mynginx nano /var/www/html/index.html | |
------------------------------------------------------------------------------------------------------------------------- | |
Run - attach and detach | |
if you want to run any container in background you can run it in detach mode. | |
e.g | |
docker run -d nginx | |
if you want to attach your console to the perticular container running in detach mode, use attach command | |
e.g | |
docker attach <container_id/container_name> | |
docker attach mynginx | |
------------------------------------------------------------------------------------------------------------------------- | |
Run - Deep dive | |
tag: | |
you can tag your docker image to spcifiy the version. | |
if you don't specify tag, it consider it as "latest" tag | |
e.g | |
docker run nignx:1.0 | |
stdin: | |
"-i" parameter is used to run docker interctive mode. | |
"-t" parameter map your local terminal to container's terminal | |
e.g | |
docker run -it ubuntu | |
port: | |
"-p" flag used to map your local free port to docker container post | |
e.g | |
docker run -p <docker host port>:<docker nginx | |
docker run -p 8080:80 nginx | |
so on above example all traffic on 8080 port of docker host routed to container's 80 port. | |
volume: | |
container destroy it's container as soon as it get terminated. but in many case we need to persist the data. | |
e.g | |
when we running mysql, we need to persist the data. so we can map a directory of docker host to a directory inside | |
the docker container. | |
docker run -v /opt/datadir:/var/lib/mysql mysql | |
------------------------------------------------------------------------------------------------------------------------- | |
Inspect container | |
docker inspect command used when we want to get detaies related to container | |
e.g | |
docker inspect <container_name/container_id> | |
------------------------------------------------------------------------------------------------------------------------- | |
Environment varialbe | |
many docker container require docker environment varialbe | |
e.g | |
docker run -e MYSQL_ROOT_PASSWORD=password mysql:5.7 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment