loading...
Cover image for Getting familiar with Docker

Getting familiar with Docker

kshitizsaini113 profile image Kshitiz Saini ・3 min read

Docker is an OpenSource containerization platform for Building, Shipping and Running applications using Container Virtualization technology. Docker packages all the dependencies in form of a Docker containers to ensure that our applicaton works seamlessly in any of the environment.

If you are not familiar with Docker and have not installed Docker in your device, you can go through my last article mentioning the installation here.

Docker Images

Docker uses the concept of Docker images to deploy the Docker Containers. Docker Images can be compaed with a template that can be used to create the Docker Containers. Docker Images are read-only templates. Docker Images are available on hub.docker.com and can also be created by users.

Docker Process

Some Docker Commands

  1. docker search: Used to search for a particular image on DockerHub via command line.
docker search IMAGE_NAME
Enter fullscreen mode Exit fullscreen mode

Screenshot from 2020-11-20 04-25-42.png

  1. docker pull: Used to pull images from DockerHub to Docker Engine for our use.
docker pull IMAGE_NAME
Enter fullscreen mode Exit fullscreen mode

Screenshot from 2020-11-20 04-28-45.png

  1. docker images: Used to list all the available images on our Docker Engine.
docker images
Enter fullscreen mode Exit fullscreen mode

Screenshot from 2020-11-20 04-28-58.png

  1. docker run: Creates the container and runs the default or specified command.
docker run IMAGE_NAME
docker run IMAGE_NAME COMMAND
Enter fullscreen mode Exit fullscreen mode

Screenshot from 2020-11-20 04-34-34.png

The life of a container is the running time of the command or code.

More about running Docker Containers

docker run command attaches and runs an image inside of a docker container. The docker run command requires one parameter and that is the image name.

docker run IMAGE_NAME
Enter fullscreen mode Exit fullscreen mode
  • To run a specific command in Docker Container,
docker run IMAGE_NAME COMMAND
Enter fullscreen mode Exit fullscreen mode
  • To run a container with a specific name,
docker run --name CONTAINER_NAME IMAGE_NAME COMMAND
Enter fullscreen mode Exit fullscreen mode
  • To run a container wth an interactive terminal,
docker run -it IMAGE_NAME
Enter fullscreen mode Exit fullscreen mode
  • To run a container in background,
docker run -d IMAGE_NAME COMMAND
Enter fullscreen mode Exit fullscreen mode
  • To delete a container as soon as it finishes it's execution,
docker run --rm IMAGE_NAME COMMAND
Enter fullscreen mode Exit fullscreen mode

The options with the docker run command can be coupled together and used accordingly.

Listing the Docker containers

docker ps: Used to list the docker containers.

docker ps
Enter fullscreen mode Exit fullscreen mode

Screenshot from 2020-11-20 05-18-45.png

  • To list all the running docker containers,
docker ps
Enter fullscreen mode Exit fullscreen mode
  • To list all the docker containers, (weather running or stopped),
docker ps -a
Enter fullscreen mode Exit fullscreen mode
  • To list only the ID's of docker containers,
docker ps -q
Enter fullscreen mode Exit fullscreen mode

The options with the docker ps command can be coupled together and used accordingly.

Playing with Containers

  1. docker stop: Stops a container after closing all the running programs.
docker stop CONTAINER_ID
docker stop CONTAINER_NAME
Enter fullscreen mode Exit fullscreen mode

(Can be considered as properly shutting down the device)

  1. docker kill: Stops a container forcefully.
docker kill CONTAINER_ID
docker kill CONTAINER_NAME
Enter fullscreen mode Exit fullscreen mode

(Can be thought of forcefully stopping the device by removing the power cable)

  1. docker start: Used to start an existing stopped docker container.
docker start CONTAINER_ID
docker start CONTAINER_NAME
Enter fullscreen mode Exit fullscreen mode
  1. docker exec: Used to run a child process along with the parent process.
docker exec CONTAINER COMMAND
Enter fullscreen mode Exit fullscreen mode
  1. docker rm: Used to remove a container from the memory. (Permanently delete)
docker rm CONTAINER_ID
docker rm CONTAINER_NAME
Enter fullscreen mode Exit fullscreen mode
  1. docker rmi: Used to remove a pulled docker image.
docker rmi IMAGE_NAME
Enter fullscreen mode Exit fullscreen mode

Discussion

pic
Editor guide