Date Posted: 17-03-2017

Docker is a most handy devops tool used nowdays. In this post, we explains few things about docker starts from installation. We tested docker on ubuntu machine. The docker command whatever we deals in the post will be same all linux flavors. Installation and few other command may differ based of base OS.

Implementation:

Installation:

We install docker on the ubuntu host. First, we will install the dependencies.

sudo apt-get install -y --no-install-recommends \
apt-transport-https \
ca-certificates \
curl \
software-properties-common

Add the docker key  to the server.

curl -fsSL https://apt.dockerproject.org/gpg | sudo apt-key add -

Add the docker repo to the server.

add-apt-repository \
"deb https://apt.dockerproject.org/repo/ \
ubuntu-$(lsb_release -cs) \
main"

Perform the repo update.

apt-get update

Install the docker-engine.

apt-get -y install docker-engine

Basic Directory Structure of Docker:

/var/lib/docker – Base Directory
/var/lib/docker/container – Container information
/var/lib/docker/container/container-longname/hostconfig.json – Has full details about host information
/var/lib/docker/volumes/uniquevolumeID/_data – volume which created inside the docker container. This is called persistent data creation

Working on Docker:

Here comes the first command on docker ūüôā The below command download the hello-world docker image from docker repo.

docker run hello-world

To download ubuntu image with terminal prompt, use the below command.

docker run -it ubuntu bash

To view the available images on your images.

docker images

To tag a image use the following command.

docker tag 73fd91f23a84 pheonixsolutions/docker-whale:latest

where 73fd91f23a84 is a image name

Create an account on hub.docker.com where you can store your images on docker hub.

To login to docker hub, use the following command.

docker login

Inorder to push the image to docker hub. The below command will push the docker image docker-whale(previously tagged image) to docker hub

docker push pheonixsolutions/docker-whale

To pull image from github account,

docker run pheonixsolutions/docker-whale

To login to the docker container

docker run -ti ubuntu:14.04 /bin/bash

Presss Ctrl + P + Q – to exit from container without stop.

To view the existing container

docker ps

To Connect to Docker container using container ID

docker attach c26fe223abeb

where, c26fe223abeb is a container ID

To show inact container as well.

docker ps -a

To start any inact container,

docker start 196b121e371b

where 196b121e371b is the container ID.

To Stop the running container.

docker stop 196b121e371b

where 196b121e371b is the container ID.

To name the container and detach container after the launch.

docker run -d -ti --name=pheonixcontainer ubuntu:14.04 /bin/bash

where d stands for detach the container,

name used to represent the name of the container.

To map the base OS volume to the docker container. The below example mounts the base OS volume to /data directory on the container

docker run -ti -v /data --name=pheonixvolume  ubuntu:14.04

where v represents the docker container mount point.

To mount different base OS partition. The below example mounts /home/pheonixsolutions to /data on the container.

docker run -ti -v /home/pheonixsolutions:/data --name=pheonixvolume  ubuntu:14.04

To bind a port to a container,

docker run -d -p 3306 -ti  --name=mysql1 mysql /bin/bash

where p denotes the port number

To forward a port request from base OS to docker container.

docker run -d -p 80:80 -ti --name=mysql2 mysql /bin/bash

where 80:80 forwards port 80 request from base OS to docker container.

2 thoughts on “Overview of Docker|Docker Commands”

Leave a Reply