Docker Installation & Basic Commands
Introduction
Docker is open source platform for developing, shipping and running your applications. It also enables you to separate your applications from the infrastructure, this results in faster delivery of the software.
Docker comes with two editions
This is mainly used by developers & ideal for small teams, experimenting with container-based apps e g. Dev and Test environment of the applications.
This is mainly used for Production environments & for the big IT firms. Look here for more info
We are going to perform the docker installations and execute basic commands of Docker. For more information of Docker Editions, click here
Installations & Configuration
In this section, we are going to install Docker CE on various Operating System which widely used like Ubuntu, CentOS, RHEL and Windows etc.
Prerequisites
- 64-bit bit Operating System
- Non-root user with sudo privileges
Installation
Ubuntu [16.04]
- Docker package is available in official Ubuntu 16.04 repository, might not be the latest one. So, we will update repo to get the latest repository.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add – |
- Add the repository to the APT sources
sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable” |
- Now, update the package database with newly added docker repository
- Check the docker repo, which we are going to install.
apt-cache policy docker-ce |
Notice: docker-ce is not installed. The docker-ce version might be different in the newly added repository
- Finally install docker using following command. To install the latest docker version, we can remove the version string parameter.
sudo apt-get install docker-ce=<VERSION_STRING> |
- Docker is now be installed, the demon process enabled to start on boot. Check that it’s running:
sudo systemctl status docker |
RHEL & CentOS 7
- Update the official repository
sudo yum update
yum install docker |
- After installation, we need to start daemon, check the status & enable it.
sudo systemctl start docker
sudo systemctl status docker
sudo systemctl enable docker
sudo chkconfig docker on |
Windows
- Download the docker installer file from here. Double click the installer
- Follow the install wizard to accept the license, authorize the installer, and proceed with the install steps on the Installation wizard.
Configuration
Here we are going to configure to execute docker commands without sudo privileges. By default docker commands runs with sudo privileges, so every time we need to add prefix sudo to execute command. If we don’t use the sudo privileges, we get the following error
Ubuntu [16.04]
- To avoid using sudo, first we will check from which user you are logged in & add that particular user to the docker group
sudo usermod -aG docker $(USER) |
- Now, we will add a username of a user that we are not logged in to the docker group
sudo usermod -aG docker username |
- Let’s verify which version of docker is installed
RHEL & CentOS 7
- To avoid using sudo, first we will check from which user you are logged in
sudo usermod -aG docker $(whoami) |
- Now, we will add a username to the docker group
sudo usermod -aG docker username |
- Let’s verify which version of docker is installed
Hands On with Docker
- To view entire System information
- To pull the image from docker hub below is the docker command to pull hello-world image from docker hub
- To search image on docker hub, following is the command
- To run the hello-world image with simple run command
Note: here container runs, displays the text messages and immediately exist itself. However container should be used in interactive way
- Let’s run the centos image in interactive mode, combination of the -i and -t switches gives you interactive shell access into the container
- List the running docker container
- List all active & and inactive containers use –a
- To view latest container, we have created
- To stop the running container
docker stop container-id / container name |
- To delete / remove the inactive or active container
docker rm container-id / container name |
- To remove one or more images
docker rmi image-id / image name |
- To display the live stream i.e. usage statistics of containers
docker stats container-id / container name |
- Pushing Docker images to docker repository
docker login -u docker-registry-username |
- After successful authentication, we can push our image to Docker Hub
docker push docker-registry-username/docker-image-name |
Contributed by :
Sonali Shah
https://www.linkedin.com/in/sonali-shah/