Dev, Sec & Ops
Published in

Dev, Sec & Ops

Docker 101: Introduction

A beginner’s introduction to Docker & why it’s awesome?

Let’s begin!

First, to give you a understanding of what Docker is I need you to remember everything you know about virtualisation or virtual machines. So, let’s recap for a moment and see what virtual machines do. Virtual machines allow you to setup a guest operating system over your current operating system i.e. you can run Ubuntu or Kali Linux machines on your Windows computer without a problem. All you have to do is download the .iso files for the guest OS and then install it in your virtualisation software like VMware, VirtualBox, etc. A pictorial representation of it would look something like this.

Depiction of Virtual Machine Setup
Depiction of Docker Setup
Differences between Docker & Virtual Machine

Docker Commands

Let’s go through the different docker commands and what functions are they used to perform. You can open a new tab and practise these commands alongside while going through the article, click here this is a website designed for Docker beginners to learn about Docker and how it works.

$ docker run

$ docker run <docker-image>e.g. 
$ docker run ubuntu
$ docker run nginx
  • If the image is not locally present then it downloads the image.
  • If the image is present locally then it executes it from the local file.
$ docker run kodekloud/simple-webapp
$ docker run -d kodekloud/simple-webapp
$ docker run redis:4.0
  • 4.0 here is the tag.
$ docker run -p 80:5000 kodekloud/simple-webapp
  • The port on the left side is the port of the host system and the one on the right is the port on the container.
$ docker run -v /opt/datadir:/var/lib/mysql mysql
  • As before the directory on left in the one present in the host system and the one on the right is the container
  • Using this command the data from the docker container is saved on the given directory on the host system and so the data is protected even if the container is deleted.
$ docker run -e APP_COLOUR=blue kodekloud/simple-webapp
  • To check what environment variables are being utilised by a container, you can run the inspect container to see all the details.

$ docker ps

$ docker ps
  • It also provides us with basic stats for these running containers
$ docker ps -a
  • Provides the same detail as “docker ps”

$ docker stop

$ docker stop <container-id> | <container-name>e.g.
$ docker stop ubuntu
$ docker stop a3772d4a2fed

$ docker rm

$ docker rm <container-id> | <container-name>e.g.
$ docker rm nginx
$ docker stop a3742d5a2fhd
  • This is done to stop the containers from just lying around & taking up space

$ docker images

$ docker images

$ docker rmi

$ docker rmi <image-name>e.g.
$ docker rmi nginx
$ docker rmi ubuntu
  • To delete the image you must stop all containers that are using the image you want to delete

$ docker pull

$ docker pull
  • Pulling essentially downloads the image from the docker hub for future use.

$ docker exec

$ docker exec <docker-name> <command>e.g.
$ docker exec -it ubuntu sh -c "echo a && echo b"
  • First we need to enter the name of the container and then enter the command we want to be executed like cat /etc/hosts

$ docker attach

$ docker attach <container-name> | <container-id>e.g.
$ docker attach nginx
$ docker attach a3742d5a2fhd

$ docker -i | docker -t

$ docker -i kodekloud/simple-prompt-docker
  • When the docker is initialised you can then go ahead and properly interact with it.
$ docker -it kodekloud/simple-prompt-docker
  • It is recommended to use both “-i” and “-t” tag to establish proper interaction with the docker

$ docker inspect

$ docker inspect <container-name>

$ docker logs

$ docker logs <container-id> | <container-name>

Conclusion

This article have shed light on the basics of Docker and the command that we use on a daily basis to properly maintain our running containers and to manage our dockers. I have more articles related to Docker that I will be publishing in this upcoming week. Stay tuned for those.

Credits

To present you with this content I had to go through a lot of video content and lab environments.

  1. Docker for Beginners — Lab Environment
  2. Docker Tutorial for Beginners — Edureka

Follow us on Dev, Sec & Ops to read related articles.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store