Install Docker:

curl -fsSL https://get.docker.com/ | sh

Test docker :

docker run hello-world

– The docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can access it with sudo. For this reason, docker daemon always runs as the root user.

** Command to get docker info:
docker info

** Search for a package:
docker search ubuntu

** Download an image to local:
docker pull ubuntu

** list all available images in your system:
docker images

** To remove a docker image:
docker rmi ubuntu

When you execute a command against an image you basically obtain a container. After the command that is executing into container ends, the container stops (you get a non-running or exited container). If you run another command into the same image again a new container is created and so on.

** To get container ID:
docker ps -l

Once the container ID has been obtained, you can start the container again with the command that was used to create it, by issuing the following command:

docker start c629b7d70666

*** A more elegant alternative so you don’t have to remember the container ID would be to allocate a unique name for every container you create by using the –name option on command line, as in the following example:

# docker run –name myname  ubuntu cat /etc/debian_version

*** In order to interactively connect into a container shell session, and run commands as you do on any other Linux session, issue the following command:

# docker run -it ubuntu bash

-i => interactive
-t => gives a tty for input and output

*** To detach from a container bash shell hit ctl+p and ctl+q
*** to attach back again use :
# docker attach <container id>

To stop a running container from the host session issue the following command:

# docker kill <container id>

*** Installing ngin inside a docker ubuntu container:

– start the container with the nginx package installed

docker run –name kevin-nginx ubuntu bash -c “apt-get -y install nginx”

**** Commit changes to a docker: Commiting changes to a container will create a new docker image (`docker images`)

docker commit <container ID> <new image tag name to be given>

*** tag a docker image:

docker tag <docker id> <repo_name:tag_name>

*** Running an interactive terminal on an docker image that was created :
docker run -it kevin:nginx bash

docker run -it <repo_name:tag_name> bash

*** Run a command inside an image without entering the image:
docker run kevin:nginx which nginx

docker run <repo_name:tag_name> <command_to_excecute>

*** execute a command with an image by giving the container thus formed with a custom name :
# docker run –name <custum_name> <repo_name:tag_name> <command_to_excecute>

eg: docker run –name test kevin:nginx /etc/init.d/nginx stop

*** We need to map the nginx port running inside a docker container to the host to make it available for access. For that, start the container by mapping the nginx port to an arbitary unused port of the host :
# docker run -it -p <host_port>:<docker_contnr_port> <repo_name:tag_name> /bin/bash

eg: docker run -it -p 81:80 kevin:nginx bash   (nginx will be available from port 81 of your host IP)

 

 

 

References:

Install Docker and Learn Basic Container Manipulation in CentOS and RHEL 7/6 – Part 1

https://docs.docker.com