Getting Started With Docker

getting started with docker

In our previous tutorial, we have explained how to install Docker in Ubuntu, and how to install Docker in CentOS. Today, we will see the Docker basic usage. This guide will cover only the basics such as how to create a new container, how to run the container, remove a container, and how to build your Docker image from the Container.

Let us get to the topic now.

Docker basics

Working with Docker is really fun and interesting! You’ll know after doing a couple of things with Docker.

As I already explained in the previous tutorial, Docker Image is the file that decides how a Container should behave, and Docker Container is the running or stopped stage of a Docker image. Don’t confuse with Docker images and Docker Containers.

1. Download Docker image

To download Docker image for Ubuntu OS, run the following command from the Terminal:

sudo docker pull ubuntu

The above command will download the latest Ubuntu image from the Docker hub. Docker hub is cloud hosted place where all Docker users build, test, and save their Docker images.

Sample output:

Using default tag: latest
latest: Pulling from library/ubuntu

759d6771041e: Pull complete 
8836b825667b: Pull complete 
c2f5e51744e6: Pull complete 
a3ed95caeb02: Pull complete 
Digest: sha256:b4dbab2d8029edddfe494f42183de20b7e2e871a424ff16ffe7b15a31f102536
Status: Image is up to date for ubuntu:latest

Docker allows your to download any images and start the container regardless of OS.

Can I create only Ubuntu containers? No, you are free to create any container of your choice.

For example, to create a CentOS guest machine (Container, of course), run the following command:

sudo docker pull centos

You can find the downloaded Docker images in /var/lib/docker/ directory.

To view the list of downloaded Docker images, run:

sudo docker images

Sample output:

REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest b72889fa879c 35 hours ago 187.9 MB
hello-world latest 690ed74de00f 6 months ago 960 B

As you see above, I have downloaded two Docker images.

Now, let us see how to start or run the containers.

We can start the containers in two methods. We can either start a container using its TAG or IMAGE ID. As you in the above results “latest” is the TAG for both containers, and b72889fa879c is the IMAGE ID of Ubuntu Docker image, and 690ed74de00f is the image id of hello_world Docker image.

2. Run Docker Containers

Once you downloaded the Docker images of your choice, run the following command to start a Docker container by using its TAG.

sudo docker run -t -i ubuntu:latest /bin/bash

Here,

  • -t : Assigns a new Terminal inside the Ubuntu container.
  • -i : Allows us to make an interactive connection by grabbing the standard in (STDIN) of the container.
  • ubuntu:latest : Ubuntu container with TAG “latest”.
  • /bin/bash : BASH shell for the new container.

Or, you can start the container using IMAGE ID as shown below:

sudo docker run -t -i b72889fa879c /bin/bash

Here,

  • b72889fa879c – Image id

After starting the container, you’ll be entered automatically into the Container’s shell (Command prompt):

To return back to the host system’s Terminal (I mean Lubuntu 14.04 LTS) without terminating the Container (guest os), press CTRL+P followed by CTRL+Q. Now, you’ll be safely return back to your original host computer’s terminal window. Please note that the container is still running, we didn’t terminate it.

To view the list running of containers, run the following command:

sudo docker ps

Sample output:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
541256222acc ubuntu:latest "/bin/bash" 5 minutes ago Up 5 minutes sad_galileo

Here,

  • 541256222acc – Container ID
  • ubuntu:latest – Docker image

Please note that Container ID and Docker image ID are different.

To list all available (running or stopped) containers:

sudo docker ps -a

To stop (power off the container) from the host’s shell, run the following command:

sudo docker stop <container-id>

Example:

sudo docker stop 541256222acc

To login back to or attach to the running container, just run:

sudo docker attach 541256222acc

As you already know, 541256222acc is the container’s ID.

To power off a Container, run:

exit

3. Build your custom Docker images

Docker is not just for downloading and using the existing containers. You can create your custom docker image if you want.

To do so, start any one the downloaded container:

sudo docker run -t -i ubuntu:latest /bin/bash

Now, you will be in the container’s shell.

Then, install any software or do what ever you want to do in the container.

For example, let us install MySQL in the container

Once you did all tweaks, installed all necessary softwares, run the following command to build your custom image:

apt-get install mysql-server mysql-client

Please note that we are installing MySQL server and client packages inside the Container, not in the host system.

Enter new password for the MySQL “root” user:

Similarly, install and test any software of your choice in the Container.

Once you all set, return back to the host system’s shell. Do not stop or poweroff the Container. To switch to the host system’s shell without stopping Container, press CTRL+P followed by CTRL+Q.

From your host computer’s shell, run the following command to create a Docker image of the running Container.

sudo docker commit 12e892156219 ostechnix/ubuntu_mysql

Sample Output:

sha256:40d5bc9614a7b7c5cd9506421c91a11d23f0565b6ed9fb19dbcc0b1bafbdea3b

Here,

  • 12e892156219 – Ubuntu container ID. As you already, we can find the container ID with command “sudo docker ps”.
  • ostechnix – Name of the user who created the container.
  • ubuntu_mysql – Name of the docker image created by user ostechnix.

Let us check whether the new Docker image is created or not with command:

sudo docker images

Sample output:

REPOSITORY TAG IMAGE ID CREATED SIZE
ostechnix/ubuntu_mysql latest 40d5bc9614a7 3 minutes ago 317.7 MB
ubuntu latest b72889fa879c 4 days ago 187.9 MB
hello-world latest 690ed74de00f 6 months ago 960 B

As you see in the above output, the new Docker image has been created in our localhost system from the running Container.

Now, you can create a new Container from the newly created Docker image as usual suing command:

sudo docker run -t -i ostechnix/ubuntu_mysql /bin/bash

4. Removing Docker images and Containers

Once you done all R&D with Docker images, you can delete if you don’t want them anymore.

To do so, First we have to stop (power off) the running Containers.

Let us find out the running containers with command:

sudo docker ps

Sample output:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
12e892156219 ubuntu:latest "/bin/bash" 28 minutes ago Up 28 minutes goofy_easley

Stop the running container by using it’s ID:

sudo docker stop 12e892156219

Then, delete all downloaded Docker images using command.

To find the list of the Downloaded Docker images:

sudo docker images

Sample output:

REPOSITORY TAG IMAGE ID CREATED SIZE
ostechnix/ubuntu_mysql latest 40d5bc9614a7 15 minutes ago 317.7 MB
ubuntu latest b72889fa879c 4 days ago 187.9 MB
hello-world latest 690ed74de00f 6 months ago 960 B

As you see above, we have three Docker images in our host system.

Let us delete them by using their IMAGE id:

sudo docker rmi 40d5bc9614a7

sudo docker rmi b72889fa879c

sudo docker rmi 690ed74de00f

Troubleshooting

Sometimes Docker won’t let you to delete the Docker images.

For example, when I try to delete a Docker Image with ID b72889fa879c, I got the following error:

Error response from daemon: conflict: unable to delete b72889fa879c (must be forced) - image is being used by stopped container dde4dd285377

This is because the Docker image that you want to delete is currently being used by another Container.

So, let us check the running Container using command:

sudo docker ps

Sample output:

Oops! There is no running container.

Let us again check for all containers (Running and stopped) with command:

sudo docker ps -a

Sample output:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
12e892156219 ubuntu:latest "/bin/bash" About an hour ago Exited (137) 40 minutes ago goofy_easley
3a335b532ba2 b72889fa879c "/bin/bash" About an hour ago Exited (0) About an hour ago cranky_noether
dde4dd285377 ubuntu:latest "/bin/bash" 2 days ago Exited (130) 2 days ago desperate_minsky
d08bdc436693 b72889fa879c "/bin/bash" 3 days ago Exited (0) 3 days ago elated_gates
541256222acc ubuntu:latest "/bin/bash" 3 days ago Exited (0) 3 days ago sad_galileo
3792618c9f92 b72889fa879c "/bin/bash" 3 days ago Exited (0) 3 days ago elegant_panini
00f90d9b375f ubuntu:latest "/bin/bash" 3 days ago Exited (0) 3 days ago modest_goodall
98e6a091a773 hello-world "/hello" 3 days ago Exited (0) 3 days ago thirsty_mcclintock

As you see there are still some stopped containers are using one of the Docker images. So, let us delete all of the containers.

Example:

sudo docker rm 12e892156219

Similarly, remove all containers as shown above using their respective container’s ID.

Once you deleted all Containers, finally remove the Docker images.

Example:

sudo docker rmi b72889fa879c

That’s it. Let us verify is there any other Docker images in the host with command:

sudo docker images

Have any questions?

Refer the official resource links given at the end of this guide or drop a comment in the comment section below.

Also, download and use the following Docker Ebooks to get to know more about it.

That’s all for now. Hope you a got a basic idea about Docker usage. If you find this guide useful, please share it on your social networks, and support us.

Cheers!!

Reference links:

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You may also like...