Basic Commands in Docker | Part 1

Download an image

  • Example:
    • Download ubuntu version 16.04 -> sudo docker pull ubuntu:16.04
    • Download the latest version -> sudo docker pull ubuntu:lastest
  • Syntax: sudo docker pull [name_image]:[tag]

Delete an image

  • Example:
    • Method 1 -> sudo docker rm ubuntu:16.04
    • Method 2 -> sudo docker rm [image_id]
  • Syntax:
    • Method 1 -> sudo docker rm [name_image]:[tag]
    • Method 2 -> sudo docker rm [image_id] (no need to write the entire ID if the first characters are not the same)

Note: When the image is running, the executable versions of the image are containers (1 image can create many containers)

Initialize and run an image

  • Syntax: docker run [param] IMAGE command [param_command]
  • Example:
    • docker run -i -t ubuntu:16.04
      • Parameter -i : container created to receive interaction
      • Parameter -t : connect to terminal
    • docker run -it –name “ABC” -h ubuntu1 ubuntu:16.04
      • Parameter –name : name the container
      • Parameter -h : name for host name (eg root@ubuntu1 instead of host name set by docker)

View running containers

  • Syntax: docker ps

To view all containers

  • Syntax: docker ps -a

To shut down a container

  • Syntax:
    • In the container’s terminal -> exit
    • In another terminal window -> docker stop [container_ID]
    • To close the container’s terminal window but the container is still running -> Ctrl + P + Q

To restart the closed containers

  • Syntax: docker start [container_ID]

To delete a container

  • Syntax:
    • docker rm [container_ID]
    • When the container is running and you still want to delete it: docker rm -f [container_ID]

APPENDIX

  • parameters -> param : parameter
  • 1 IMAGE characterized by name + version or ID ([name]:[tag] && [ID]). Any [name]:[tag] can be replaced by [id]. Any information that can be used by [name]:[tag] can be replaced by [ID].

Copyright © Tech Vanguard . All Rights Reserved.