Search This Blog

Friday, March 19, 2021

Docker

Docker Basic Commands 


  • docker -v --- Show Docker version
  • docker --version --- Show Docker version
  • docker or docker --help --- Show Docker all commands
  • docker pull image_name  --- Pull an image or a repository from a registry
  • docker pull image_name:tab  --- Pull an image or a repository from a registry
  • docker pull --all-tabs image_name:tab  --- Pull an image or a repository from a registry with multiple images
  • docker start container_name--- Start one or more stopped containers
  • docker start -a container_name --- attached running container
  • docker start -i container_name --- Attach container's STDIN
  • docker run image_name  --- Start a container from an image -- attached mode
  • docker run -d image_name --- run in back end (run in detached mode)
  • docker run image_name:tag  --- tag is version by default docker pick latest version
  • docker run image_name  sleep 5 --- sleep after 5 seconds
  • docker run --name Provide_new_name image_name    --- Set container name
  • docker run -i image_name  --- prompt interactive mode 
  • docker run -it image_name   --- prompt interactive mode in terminal
  • docker run -p 80:5000 image_name    --- traffic on port 80 on docker host will get routed to port 5000 inside the docker contain
  • docker run -v /opt/datadir:/var/lib/mysql image_name --- /opt/datadir is the outside container where you want to save your container related data
  • docker run -e APP_COLOR=blue image_name   --- set environment variable
  • docker run --network=none image_name  --- run container on network none
  • docker run --network=host image_name   --- run container on network host
  • docker run -v data_volume:/var/lib/mysql mysql   --- mount volume
  • docker run -d -e MYSQL_ROOT_PASSWORD=db_pass123 --name mysql-db --network wp-mysql-network mysql:5.6    --- run container and adding network and environment variable
run = pull + start
CTRL + C stop running 
  • docker exec container_name command [Arg...] --- Run a command in a running container
  • docker ps --- list running containers 
  • docker ps -a --- list all containers (running or stopping)
  • docker ps -q --- list all containers id
  • docker ps | grep test --- list all containers and filter with name test
  • docker ps --last 1 --- Show n last created containers (includes all states)
  • docker ps --latest  --- Show the latest created container (includes all states)
  • docker ps -s  --- Display total file sizes
  • docker ps -f "status=exited"   --- filtering flag with status
  • docker ps -a -f exited=1   --- filtering flag with status exited
  • docker ps -f id=39b7876aa786   --- filtering flag with id
  • docker ps -f name=nginx   --- filtering flag with name
  • docker images  --- list of running images
  • docker images -a --- list of all images
  • docker images --digests   --- list of digests images
  • docker images --no-trunc   --- list of digests images without truncate output
  • docker images -f "before=image_name"   ---  filter shows only images created before the image with given id or reference
  • docker images -f "since=image_name"   ---  filter shows only images created after the image with given id or reference
  • docker images -f  "dangling=true"   ---  show un-tagged images
  • docker tag image_id new_image_name   --- renaming the image name
  • docker stop container_name or container_id --- stop running container
  • docker rm container_name or container_id --- remove container
  • docker rmi container_name --- remove image
  • docker rmi $(docker images -f "dangling=true" -q) --- remove image using filter un-tagged images
  • docker port container_id --- check the port number for the container
  • docker kill container_name  ---  Force stop containers.
  • docker attach docker_container --- run in front end
  • docker logs container_name --- view specified container related logs
  • docker inspect container_name  --- find details for specific container
  • docker build Dockerfile -t image_name --- build docker image
  • docker push image_name   --- push docker image to repository/hub
  • docker history image_name  --- Find the history of image
  • docker network ls  --- list all networks
  • docker network inspect network_name   --- inspect network details
  • docker network create --driver bridge --subnet 182.18.0.1/24 --gateway 182.18.0.1 image_name    --- create network and adding driver, subnet and gateway
  • docker volume create data_volume --- create docker volume
  • docker service create --replicas=3 my-web-server    --- create number of replicas
  • docker commit existing_container_id  new_image_name    --- Create a new image from a container’s changes
  • docker commit --change "ENV DEBUG=true" existing_container_name  new_image_name   --- new configuration
  • docker commit --change='CMD ["apachectl", "-DFOREGROUND"]' -c "EXPOSE 80" existing_container_name  new_image_name  --- commit container with cmd and expose instuction
  • docker service create --replicas=3 my-web-server    --- create number of replicas
  • docker cp container: source_path destination_path  --- Copy files/folders between a container and the local filesystem
  • dcoker create image_name --- Create a new container
  • docker diff container  --- Inspect changes to files or directories on a container’s filesystem
  • docker export container  --- Export a container’s filesystem as a tar archive
  • docker image ls   --- list images
  • docker image pull   --- Pull an image or a repository from a registry
  • docker image push   --- Push an image or a repository to a registry
  • docker image inspect  --- Display detailed information on one or more images
  • docker image history  --- Show the history of an image
  • docker image build  --- Build an image from a Dockerfile
  • docker image rm  --- Remove one or more images
  • docker images --- List images
  • docker login --- Log in to a Docker registry
  • docker lgout --- Log out from Docker registry
  • docker logs container  --- fetch the logs of a container
  • docker system df --- image container details summary
  • docekr stats   --- docker stats
  • docker network connect network_name container_name  --- Connect a container to a network
  • docker network disconnect network_name container_name  --- Disconnect a container from a network
  • docker network create network_name  --- Create a network
  • docker network inspect network_name  --- Display detailed information on one or more networks
  • docker network ls -- list networks
  • docker network rm network_name --- Remove one or more networks
  • docker rename container_name new_Container_name --- Rename a container
  • docker restart container_name --- Restart one or more containers

Docker Commands Details


Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes


Form a Docker Compose File From Docker Command



Below are the docker commands

docker run -d --name=redis redis
docker run -d --name=db postgres:9.4
docker run -d --name=vote -p 5000:80 --link redis:redis voting-app
docker run -d --name=result -p 5001:80 --link db:db result-app
docker run -d --name=worker --link db:db --link redis:redis worker

Note: link can be simply notify as
--link db:db
--link db

Compose Docker file from above commands

docker-compose.yml
------------------------------
version:
redis:
image: redis
db:
image: postgres:9.4
vote:
build: ./vote
port:
- 5000:80
links:
- redis
result:
build: ./result
ports:
- 5001:80
links:
- db
worker:
build: ./worker
links:
- redis
- db

Docker Compose Commands


  • docker compose create service_name  --- Creates containers for a service.
  • docker compose convert one --format yaml  --- Converts the compose file to platform’s yaml format
  • docker compose convert one --format json  --- Converts the compose file to platform’s json format
  • docker compose down  --- Stop and remove containers, networks
  • docker compose exec service_name command --- run command
  • docker compose kill service_name  --- Force stop service containers.
  • docker compose ls  --- List running compose projects
  • docker compose ps  --- list containers
  • docker compose pull service_name   --- pull service images
  • docker compose push service_name  --- push service images
  • docker compose rm service_name  --- Removes stopped service containers
  • docker compose run service_name  --- Run a one-off command on a service.
  • docker compose start service_name  --- Start services
  • docker compose stop service_name   --- Stop services
  • docker compose top   --- Display the running processes
  • docker compose up  --- create and start containers


Docker-Compose Commands


  • docker-compose -v   --- Docker version
  • docker-compose --version   --- Docker version
  • docker-compose version  --- Docker version
  • docker-compose config  --- validate docker file
  • docker-compose ps   --- displaying list of running docker-compose containers
  • docker-compose ps -a  --- displaying lift of all docker compose containers
  • docker compose ps  --- displaying lift of running docker compose containers
  • docker compose ps -a  --- displaying lift of all docker compose containers
docker-compose ps == docker-compose ps -a == docker compose ps -a
  • docker-compose ps || grep=test  --- filtering by container name
  • APP_PORT=80 docker-compose ps   --- setting variable in compose file
  • docker-compose ps service_name   ---- list out specific service
  • docker-compose build  --- build container
  • docker-compose build service_name --- build specific service container
  • docker-compose up --- pulling and starting services containers from compose file
  • docker-compose -d up --- detached mode
  • docker-compose -f compose_file_yml up   --- specifiying compose file name
  • docker-compose up --scale service_name=number_of_container   ---adding scaling
  • APP_PORT=80 docker-compose up --- setting variable in compose file
  • docker-compose up -d service_name --- run specific service 
  • docker-compose down --- stopping and removing services containers from compose file
  • docker-compose -f compose_file_yml down  --- specifying compose file name
  • docker compose down service_name --- stop and remove specific service 
  • docker-compose stop  --- stop docker compose containers
  • docker-compose rm  --- remove stopped docker compose containers

down = stop + rm
up = pull + start

  • docker-compose exec service_name command --- execute specific service and specific command
  • docker-compose log --- display all logs created by docker-compose
  • docker-compose log service_name  --- display logs specific service created by docker-compose
  • docker-compose log -f service_name  --- display logs specific service created by docker-compose

Dockerfile instruction arguments



FROM Sets the base image for subsequent
MAINTAINER Sets the author field of the generated images
RUN Execute commands in a new layer on top of the current image and commit the results
CMD Allowed only once (if many then last one takes effect)
LABEL Adds metadata to an image
EXPOSE Informs container runtime that the container listens on the specified network ports at runtime
ENV Sets an environment variable
ADD Copy new files, directories, or remote file URLs from into the filesystem of the container
COPY Copy new files or directories into the filesystem of the container
ENTRYPOINT Allows you to configure a container that will run as an executable
VOLUME Creates a mount point and marks it as holding externally mounted volumes from native host or other containers
USER Sets the username or UID to use when running the image
WORKDIR Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD commands
ARG Defines a variable that users can pass at build-time to the builder using --build-arg
ONBUILD Adds an instruction to be executed later, when the image is used as the base for another build
STOPSIGNAL Sets the system call signal that will be sent to the container to exit

No comments:

Post a Comment


This is a User Friendly Blog.
Simple Interface and Simple Controls are used.
Post your comments so i can modify blog regarding your wish.