What is Docker?
Docker is a tool for delivering your software in packages called a container. A Container is OS-level virtualization that allows you to run your application on the isolated environment and has its own libraries and configuration files.
A container is like the virtual machine, but more fast, portable, lightweight. It takes less disk space, but more dependent on the host operating system.
The Docker is a tool for managing a container, it allows you to do both basic and advanced container management, including build, export, import, manage networking, manage volume, etc.
Why using Docker?
For the last 3 years, the container technology has grown so fast. And Docker has become one of the most popular tools for container management.
For the IT industry, the Docker is used by computer software companies to manage their application life-cycles. For the most use cases, the Docker is used because of its simplifying configuration, app isolation, server consolidation, debugging capabilities, and rapid deployment.
And for the IT people, the Docker is worth to learn and become one of the popular tools for DevOps. It has been used by developers to develop their application and used by DevOps on their code pipeline and deployment.
In this tutorial, you will learn some basic of Docker as below:
- Installing Docker on Ubuntu 18.04
- Searching and download for Docker images
- unning the container
- Edit the container and create a custom image
- Push images to DockerHub
Before going any further, run the sudo command below to get the root privileges.
Step 1 – Install Docker on Ubuntu
First, we will install the latest version of Docker CE (Community Edition) to the Ubuntu system.
By default, the ubuntu provides docker packages on its default repository. But, we will keep using the latest version Docker and we will use the Docker version provided by the official Docker repository.
Update ubuntu repositories and install packages dependencies for Docker.
sudo apt update sudo apt install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common
Add the GPG key of Docker to the system.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Add the official Docker repository.
sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable"
Now the latest version Docker using the apt command below.
sudo apt install docker-ce docker-ce-cli containerd.io
Once the installation is complete, start the Docker service and add it to the system boot.
systemctl start docker systemctl enable docker
Check the Docker service and make sure it’s up and running.
systemctl status docker
After that, check the Docker version.
Now you’ve installed Docker on the Ubuntu 18.04.
Step 2 – Docker for Non-root user
By default, the Docker only allows the root user to run containers. And for non-root users, you will need to add your user to the Docker group.
Add a non-root user “yujin” to the “docker” group using the command below.
usermod -a -G docker yujin
Now log in as a user “yujin”, check all group ID for that user.
su – yujin id -nG
And you’ve added a non-root user to the “docker” group.
Next, run the new container as a non-root user docker “hello-world” using the following command.
docker run hello-world
Now you will get the “Hello World” message from the Docker.
And you’ve successfully run a Docker container as a non-root user.
Step 3 – Docker Basic Usage
For this section, you will learn how to use the Docker for managing container.
You will learn how to search and download Docker images, run the container, edit the container and create new custom Docker image, and push the image to the DockerHub.
– Basic Help Command
Use the following command to display all available command on Docker.
If you want to display options for specific comand, you can use the following format.
docker COMMAND --help
And you will see some options of your specific docker command.
– Search Docker Images
By default, the Docker provides an image repository called “DockerHub” that allows you to search , download, and upload Docker images.
To search for image, you can use the search command following by the pattern that you want to search.
docker seearch nginx
Now you will see top Docker images for pattern “nginx”.
– Download Docker Images
To download the Docker image, you can use the pull command as below.
docker pull nginx
Now you’ve downloaded the “nginx” Docker image, and you can use the ‘images’ command to get list of downloaded images on your local system.
And you will get details about “nginx” Docker image that you just downloaded.
– Run Docker Container
To run the Docker container, you must specify the Docker image that you want to use.
If the image are available on your local system, the container will immedietly run after the command invoked.
When the image are not available on your system, the Docker will automatically search and download an image from the “DockerHub” and run the container.
To run the Docker container, use the ‘run’ command as below.
docker run -name nginx01 -P -d nginx
The –name option are used to specify the container name.
The -P option is used to expose the container to the external world. By default, it will expose port between 32768 to 61000.
The -d option is use to run the container on the backgroud.
Now check the container using the ‘ps’ command.
And you will get the ‘nginx01’ container is up and running, exposing the port ‘32768’ on the hsost system.
Open your web browser, type the server IP address with the port ‘32768’.
You will get the default Nginx index page from the container.
– Edit the Container
To edit the container on the fly, you need to access the container it self trhough the shell.
The nginx container has provided the bash shell by default.
Run the ‘exec’ command to execute the bash shell on the ‘nginx01’ container.
docker exec -it nginx01 /bin/bash
Once you’ve logged in to the container, go to the ‘/usr/local/nginx/html’ directory and create a new custom index.html file.
cd /usr/share/nginx/html echo "<h1><center>This is custom index.html yujin/nginx</center></h1>" > index.html
Now you’ve edited the index.html file of the ‘nginx01’ container.
Type ‘exit’ to log out from the container.
– Create Custom Docker Images
After editing the container, we will create a new custom docker image called ‘yujin/nginx’ based on ‘nginx01’ container.
Run the ‘commit’ command below to create the custom docker image based on container ‘nginx01’.
docker commit -m "Change custom index.html" -a "yujin" nginx01 yujin/nginx
After that, check list of docker images available on your local system.
You will get a new docker image called ‘yujin/nginx’.
Next, run a new container named ‘nginx02’, based on a new docker image ‘yujin/nginx’.
docker run --name nginx02 -P -d yujin/nginx
Now check the container.
And you’ve run a new container ‘nginx02’ based on the custom docker image ‘yujin/nginx’.
Now open your web browser and type the server IP address following by the port that use by the ‘nginx02’ container.
You will get the custom index.html file that we’ve created on top.
– Push Image to DockerHub
The DockerHub is a cloud-based repository provided by Docker that allows you download, create, and push docker images.
To push custom images to the DockerHub, you will need to register to the https://hub.docker.com.
Once you’ve registered, back to your terminal and log in to the DockerHub using the following command.
Type your username and password, and you will get the result as below.
And you’ve logged in to the DockerHub.
Before pushing an image to the DockerHub, change the container tag with your own username.
docker tag oldname/nginx username/nginx
docker tag oldname/nginx username/nginx
Now push the image to the DockerHub using the ‘push’ command below.
docker push username/nginx
Once it’s complete, back to your browser and check the DockerHub again.
And you will get the result as below.
You’ve pushed a custom docker image to the DockerHub.