Docker Quick Start

Download

you can install docker manually by downloading

Download Docker for windows Download Docker for Ubuntu

if you have downloaded the .deb package for ubuntu then run the following command :

sudo dpkg -i /path/to/package.deb

Installation

Step One

you can install docker on linux using following command

apt-get update

sudo apt-get install docker.io

OR refer to the documentation here

Step Two

Then you can verify your installation by the following commands :

docker --version

docker info

following command is used to run a test container with a default hello world image :

sudo docker run hello-world

Basic operations

Some basic operations like starting ,stoping ,listing containers

Pulling an image from docker hub

docker pull aamirpinger/helloworld

get list of images on system

docker images

OR

docker image ls

built and run a container from image in detach mode

docker run -d aamirpinger/helloworld

OR

docker run --name <container name> -d -p<port ex= 9000:80> aamirpinger/helloworld

built and run a container from image in interactive mode

docker run -it aamirpinger/helloworld:<tag> sh

OR

docker run --name <container name> -it -p<port ex= 9000:80> aamirpinger/helloworld sh

stop container and get out from interactive mode

exit

with out stoping container and get out from in interactive mode

ctrl + p + q

Interact with an existing container

docker exec -it <container name or id> sh

get list of containers

gets list of runnung containers

docker ps

OR

docker container ls

gets list of all containers

docker ps -a

OR

docker container ls -a

Start or stop an existing containers

Start an existing container

docker start <container name or id>

Stop an existing container

docker stop <container name or id>

Remove an existing containers

docker container rm <container name or id>

Build an image to run on docker

Built a simple html page image that we can run using docker.

  • Make a folder of any name
  • Put all of your static html, css and js in the same folder
  • Make a file named Dockerfile using touch command
  • write the following in the file :

          FROM nginx:alpine
          COPY . /usr/share/nginx/html

  • save this file

COPY . is used to copy all files present in the current folder to the destination folder in the image that is to be created.
Next we need to specific where to paste the files in image environment /usr/share/nginx/html (destination folder in the image).

Build the image

Make sure you are in the same folder that you created

docker image build -t <image name>:<tag> .

here "." represents that Dockerfile is in the current directory, if Dockerfile is not in the same directory then you can write the path to Dockerfile

Push an image to docker hub

First make sure you have created the image you want to push.
then if you haven't created account and repo in docker hub follow the link and register an account and make a repo(repositry)

Login to docker hub

docker login --username <username on docker hub>

now it will ask for your docker hub password type the password and hit enter.

Tag image

first copy the image id of your image

docker images

now tag the image using the following command

docker tag <image id> <hub username>/<repo name>:<tag>

Push image

docker push <hub username>/<repo name>:<tag>