Docker is an Open Source project (container management service) for developers and system administrators to build, ship, and run distributed applications on laptops, data center VMs, cloud and ship them into containers which can then be deployed anywhere. Docker provides an automation of operating system level virtualization on Windows and Linux based operating system.
You can easily building blocks for deploying and scaling web apps, databases, and back-end services without depending on a particular stack using Docker.
Docker is made up from several components:
1) <b>Docker for Linux</b>: Allows us to run Docker containers on the Linux OS.
2) <b>Docker Engine</b>: Used for building Docker images and creating Docker containers.
3) <b>Docker Hub</b>: Used to store various Docker images.
4) <b>Docker Compose</b>: Used to define applications using multiple Docker containers.
• Docker Swarm provides a clustering solution for Docker containers that turns a group of Docker engines into a single, virtual Docker engine.
• Docker has the ability to reduce the size of development by providing a small footprint operating system.
• Docker containers are very lightweight and easily scalable.
• Provides easy and faster configuration and increase productivity.
• Provides containers that allow us to execute any kind of application in isolation environment.
In this tutorial, I will explain how to install Docker and explain some important Docker commands. I will also share some hands-on experience on how the commands are used and what they do.
• A fresh Alibaba Cloud ECS instance with Ubuntu 16.04 installed.
• A root password is set up on the instance.
Before starting, you will need to install the latest version of the Docker to your server. By default, the latest version of the Docker is not available in Ubuntu 16.04 repository. So you will need to add the official Docker repository to your server.
First, download and add the GPG key for the official Docker repository to the system with the following command:
<code>curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -</code>
Next, add the Docker repository to APT sources with the following command:
<code>echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable" | sudo tee -a /etc/apt/sources.list.d/docker.list</code>
Next, update the repository and install Docker using the following command:
Once the Docker is installed, you can check the status of the Docker with the following command:
First of all, you will need to pull a Docker image because containers are built using Docker image. There are many images already available on Docker website. You can find any image through a simple search command.
For example, to search Ubuntu 16.04 image, run the following command:
<code>docker search ubuntu:16.04</code>
You should see the following images available on Docker's website:
Next, Download the Ubuntu-16.04 base image from above listed images:
<code>docker pull ubuntu</code>
The output looks something like this:
Once download has been finished, you can list all available images on your system by running the following command:
<code>docker images</code>
Output:
Now, to setup a basic ubuntu-16.04 container with a bash shell, you just need to run one command.
<code>docker run -i -t ubuntu /bin/bash</code>
You should see the following output:
<code>root@0b775c3f606d:/#</code>
Now, after Ubuntu base image with instance is ready, you can easily install Apache Server interactively for it. To do so, you will need to run following command in a terminal.
You are now using a bash shell inside of an Ubuntu Docker container. To disconnect, or detach, from the shell without exiting use the escape sequence Ctrl-p + Ctrl-q.
Now, you can save the changes you made in the Ubuntu instance. To do that, first you will need the Container ID of running Ubuntu instance. To get that, run:
<code>docker ps</code>
Now, save the changes as a new image with the name ubuntu-apache by running the following command:
You can see that the changes are saved using Container ID and image name ubuntu-apache.
To verify new image is running or not, run:
Now, you have a new image that contains an Apache Web server. You can build a Dockerfile based on that image and add the necessary files. Given the relative path to a tarball of the site content, Docker automatically untars or unzips the files in a source tar or zip file into the target directory.
To do this, you need to create an index.html file on the host system and add it to a tarball called application.tar in current directory:
Add the following lines:
Save and close the file.
Now, compress website directory using tar:
<code>tar -cvf application.tar application</code>
Now, create Dockerfile to add the web site content to the ubuntu-apache image and launch Apache on port 80:
<code>nano Dockerfile</code>
In the above Dockerfile, the website content in website.tar will get automatically extracted to /tmp/ folder. Then, the entire site will move to the Apache root directory /var/www/html/ and the expose 80 will open port 80 so that the website will be available normally. Then, the entry point is set to /usr/sbin/apache2 so that the Apache Server will execute.
Now, you will build Container using the Dockerfile you just created in order to add website on it.
To do this, run the following command:
<code>docker build -t application .</code>
After an image has been built, you can now proceed by creating a container running Apache instance on it.
<code>docker run -d -P application</code>
<code>a749b23fb87c821b69479353bf067f2b1af53fd0fd300ad86ac5c028dd5dcbde</code>
Now, use the "docker ps" command to determine the port activated and then use curl to inspect the sample content.
Now, verify your Apache Web server by running the following command:
<code>curl localhost:32768</code>
Or
<code>curl "Container IP Address":80</code>
You should see the Apache Web page you have created earlier:
Let's start by seeing all available commands Dockers have. You can list all available Docker commands by running the following command:
<code>docker</code>
To check Docker version, run:
<code>docker version</code>
To check system-wide information on Docker, run:
<code>docker info</code>
To list all the running containers, run:
To list the latest container you created, run:
<code>docker ps -l</code>
To list both running and non-running containers, run:
<code>docker ps -a</code>
To start and stop container's process, run:
`docker start "Container ID"
docker stop "Container ID"`
To stop all running containers, run:
<code>docker stop $(docker ps -a -q)</code>
Note: You can find Container ID using sudo docker ps command.
If we want to attach into a running container, Docker allows you to interact with running containers using the attach command:
<code>docker attach "Container ID"</code>
You can check every information about a Docker Container using the inspect command with container ID.
<code>docker inspect "Container ID"</code>
To delete a single container, run:
<code>docker rm "Container ID"</code>
To delete all existing containers, run:
<code>docker rm $(docker ps -a -q)</code>
Note: Before deleting any container you will need to stop it first.
To delete a single image, you can use rmi command with image ID. The image id can be fetched using the command "docker images":
<code>docker rmi "Image ID"</code>
To delete all existing images, run:
<code>docker rmi $(docker images -q -a)</code>
If you run Docker container as a daemon then it may be useful to know what appears on the console output of the running container.
The Docker logs command retrieves logs present at the time of execution.
You can use Docker log command with container ID.
<code>docker log -f "container ID"</code>