Working with Docker Images, Containers, and the DockerHub

Docker is a handy tool designed to make it easier to create, deploy, and run applications by using containers.

Previously we showed how to install Docker on Ubuntu. Docker is a handy tool designed to make it easier to create, deploy, and run applications by using containers. Today, let’s talk about working with Docker images, Containers, and the DockerHub.

Docker Hub, Images, and Containers

As the name indicates, DockerHub is a hosted repository service provided by the publisher for finding and sharing container images with developers/users. A Docker image has everything needed to run an application as a container.

A container is a runtime instance of an image. We will show you how to work with these things with easy to follow examples.

1. Pulling the docker image and setting up a software

To pull the docker image, use the following command:

docker pull ubuntu

Pull Docker Image
Pull Docker Image

It should download the latest ubuntu image. If needed, you can get a specific version using tags. You can go to the docker hub and see the Ubuntu docker images.

Docker Hub
Docker Hub

I. Get a specific version

For example, pull Ubuntu xenial:

docker pull ubuntu:xenial

Pull Specific Version
Pull Specific Version

II. Listing images

Run the following command to list downloaded images

docker images

List Docker Images
List Docker Images

III. Running docker image

Here we run ubuntu latest image and give name called “ubuntu_apache”:

docker run -it --name="ubuntu_apache" ubuntu:latest /bin/bash

docker run:– command lets you run any Docker image as a container.

-it:– executes the container in interactive mode (not in the background).

Run Ubuntu Image
Run Ubuntu Image

IV. Configuring new image

Now you are inside ubuntu latest container. Here we are going to install apache and git.

Let’s update Ubuntu first:

apt-get update

Install apache2 and check the version.

apt-get install apache2
apache2 -version

Now install GIT:

apt-get install git

Start apache 2:

/etc/init.d/apache2 start

2. Creating an account on Docker Hub

Now we are going to build a new image and push it to the docker hub. So you must create a docker hub account first.

Sign Up Docker Hub
Sign Up Docker Hub

Then go to your docker host. Exit from the docker container.

exit

Exit Docker Container
Exit Docker Container

3. Building a new image

I. Commit modifications

docker commit -m="ubuntu-apache-git-image" ubuntu_apache ddarshana/ubuntuapache

-m:–  Commit message.
ubuntu_apache: Name of the container that we are going to creating the image.
ddarshana/ubuntuapache: Name of the Docker Hub repository. (replace this with your docker hub name and repo name)

Docker Commit
Docker Commit

After you list images, you can see a new image.

docker images

docker images
docker images

Push docker image to docker hub repository:

II. Login into the docker hub repository.

docker login

Login to Docker Hub
Log in to Docker Hub

III. Now we can push the image to the Docker hub repository.

docker push ddarshana/ubuntuapache

Docker Push

Docker PushNow, you browse a login to your docker hub account, and under repositories, you can see a new image.

Pushed Image
Pushed Image

IV) Removing Docker Images

Now we are going to remove the docker image (ddarshana/ubuntuapache) in our host machine. Before that, you need to list docker images. Then get an image ID to execute the following command to remove it.

docker rmi -f "Image ID"

-f:- force

4. Now again, we are going to pull our docker image from the docker hub.

docker pull ddarshana/ubuntuapache

Pull New Image
Pull New Image

I. Running the new Docker image as a container and in a background

docker run -itd -p 80:80 ddarshana/ubuntuapache

-itd:- executes the container in the background

-p 80:80:- using the -p option we’re connecting the internal port 80 of the container the external port 80. Apache uses port 80. So we can send the HTTP request to the server from our local machine by opening up URL http://IP:80.

Run Docker Image background
Run Docker Image background

II. Listing Docker containers

docker ps -a

List Docker Containers
List Docker Containers

III. Login to docker container

docker exec -it "container ID" /bin/bash

Login to Docker Container
Log in to Docker Container

/etc/init.d/apache2 status
/etc/init.d/apache2 start

Apache Status
Apache Status

Before we create an image, we install git too. You can run the following command and check the git version:

 git --version

IV. Checking apache service

Now you can browse apache server page using your host machine IP or hostname:

http://192.168.8.105/

Apache Default Page
Apache Default Page

Now we will stop the container and remove it.

5. Listing containers and get the container ID

docker ps -a

I. Stoping the container

docker stop "container ID"

II. Removing container

docker rm "container ID"

Remove Container
Remove Container

That’s it for now! I hope you liked this section of working with Docker images and containers.

Darshana
Hey! I'm Darshana, a Linux / DevOps Engineer and also a contributor to FOSS Linux. I enjoy working on various kind of Linux distributions and cloud technologies. During my free time, I love to swim and hike across nature trails. Linux is my love and I'm here to share all my learnings with all of you! Hope you enjoyed reading my article.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,521FansLike
364FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

How to setup FTP Server on CentOS

You are allowed to use any one of the FTP servers as you like. However, in this tutorial, we will be installing and using vsftpd, short for Very Secure FTP Daemon. It is a fast, stable, and secure FTP server that will help you transfer files to and from a remote system in a jiffy.

CentOS Vs. Ubuntu Server : Everything You Need to Know

Choosing the perfect Linux distribution to set up your server can be confusing since Linux provides a limitless number of options. The main reason behind these many distributions is because Linux is an opensource platform. Anybody with the required skills can contribute to the development or build and release their distribution. Currently, there are more than 600 Linux Desktop and Server distributions in the market.

Top 10 Reasons to use Xfce as your Desktop Environment

There are many choices for desktop environments for Linux based operating systems. Mainly, you can install any DE of your choice on most of the Linux based distributions, even if they are not offered as a package officially. In our recent articles, we discussed the best of KDE and Cinnamon. In this article, we wish to present to you the top reasons why you should consider Xfce as your desktop environment.

The 10 Best Linux Network Monitoring Tools

Having total control over your network is essential to prevent programs from overusing your network resources and slowing down the overall performance. This is why you should install a network monitoring tool on your system, giving you a visual overview of everything that's happening on your network. Networking Tools are like swiss-knife for the system administrators for troubleshooting system issues.

How to install CouchPotato on Ubuntu

Downloading movies and copying them over to your home server can get frustrating, especially if you are doing it daily! What if you have an option to download videos automatically, quickly, and above all with excellent quality. CouchPotato allows you to download movies easily once they are available and released automatically.

Top 20 Rsync Examples in Linux

The Rsync (remote sync) command is a Linux/Unix utility used to synchronize and copy files and directories either locally or remotely. Most Linux geeks use Rsync to mirror, backup or migrate data across folders, across disks and networks. One notable feature with the Rsync command is that it uses the "delta transfer algorithm."

MUST READ

Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.
Elementary OS 5.1 Hera has received a point release with a handful of new features and bug fixes, and we will be reviewing the significant changes in this article. For those new to elementary OS, this Ubuntu-based Linux distribution uses their inhouse built Pantheon desktop environment and AppCenter.

10 Reasons to use Cinnamon as your Desktop Environment

With the release of Gnome 3 in 2011, there was quite a mixed reaction from users and developers. Most of them preferred the original Gnome that got forked, and one of those forks was Cinnamon. Since the release of Cinnamon 2.0, Cinnamon has evolved to become a desktop environment by itself.

5 ways to send emails using the command-line in Linux

Did you master using the command-line in Linux? There is no limit to what one can do via the Linux Terminal. One of such things we are going to discuss today are methods of sending an email using the command-line.

CentOS Vs. Ubuntu Server : Everything You Need to Know

Choosing the perfect Linux distribution to set up your server can be confusing since Linux provides a limitless number of options. The main reason behind these many distributions is because Linux is an opensource platform. Anybody with the required skills can contribute to the development or build and release their distribution. Currently, there are more than 600 Linux Desktop and Server distributions in the market.

10 Best PDF Editors for Linux

In this article, we will take a look at 10 of the best PDF editors and tools out there in 2019 that are available for Linux platforms. The editors are going to be judged on the basis of their functionalities, portability, ease of installation, price, and convenience.