Home TutorialsDocker Part-1: Getting started with Docker

Part-1: Getting started with Docker

by Atif Azad

What is Docker?

Docker is a platform that provides the ability to package and run applications in containers. On a single host machine, several containers can run simultaneously.

I recommend you reading the introductory docs on official Docker website: https://docs.docker.com/engine/docker-overview/ and also about the containers: https://www.docker.com/resources/what-container

Learning Docker is easy with practicing so let us start it practically.

Docker Installation

Mac:

https://docs.docker.com/docker-for-mac/install/

Windows:

https://docs.docker.com/docker-for-windows/install/

Linux:

Follow installation guidelines as appropriate for the Linux distribution you’re using. Ubuntu | Fedora | CentOS | Debian

Dockerhub

Dockerhub is the repository for Docker container images. If you are working with Docker, you’ll often be searching your desired Docker images in Dockerhub repository.

Often you will just be pulling those images and will get your work done but the other times you will use some image from dockerhub as the base image to define your own.

Download (pull) a Docker image

Let us start with a simple image from Dockerhub and that will be the linux alpine image. Go to dockerhub (https://hub.docker.com/) and in search bar , just type ‘alpine’ and hit enter. You will see the list of Docker images which match your search term. We need the alpine linux one (which you’ll probably find on top of the list) click that one and go to details page.

Alpine image listed at dockerhub.com

This is a minimal Docker image based on Alpine Linux. As you go through the search results, you will see that there are so many images having alpine- prefix in their names. This is because it is quite common that many of the images use this Alpine Linux image as the base image.

So let’s go ahead now and run the command that you will find on the details page of alpine image.

docker pull alpine

On running this command, you should see the output similar to following.

The results of docker pull command

Now let’s see what has just happened…

List of downloaded Docker images

To view the list of Docker images you have downloaded, run following command.

> docker image ls

As you just pulled (downloaded) you first image, you will see only one image in your list as shown below.

The downloaded image shown in ls command results

Note that this Alpine Linux image size is just 5.59MB as claimed on Dockerhub.

Run a Docker Container

So you’ve just downloaded you first Docker image from Dockerhub which you should think of as downloading a software installation package which is of no use until you install it!

Now that we have a Docker image, we have to run a container using that image. Execute following command to run the container based on alpine image.

> docker container run -it alpine /bin/sh

This command will run the alpine container and will take us to inside its shell as we have used interactive flags -it.

As we are now running a Linux machine (container) and we are inside its shell, this means that we should be able to execute some of the common commands so try the following commands…

> ls -l
> ifconfig

You will see the results of the above commands as expected. Below are the sample results of the two commands I executed.

The shell of the running Docker container.

Congratulations! So far you have successfully run your first Docker container 🙂

List of Docker containers

To view the list of running Docker containers…

  • open a new terminal tab/window (you’ll not be inside alpine container shell now!)
  • Execute following command
> docker container ls

You will see the list of running containers as…

The results of docker container ls command showing our running container

To see the list of all containers (running + stopped ones), run following command.

> docker container ls -a

To exit that shell of running container, just type…

> exit

Please note that you have exited the shell of the container but it is still running in the background. You can again run the command docker container ls to list the containers to verify that the container is still running.

Stop a Docker container

As the running container use the resources of the host machine, its good idea of stop them when we are done and run them again when needed. Use following command to stop a container.

> docker container stop <container>

<container> should be replaced with the container ID or name that you see in the results of list command shown above.

Please note that stopping a container does not remove it. After running the stop command mentioned above, you will still see your container if you run the following command to list all containers (running + stopped ones)

> docker container ls -a

Remove a Docker container

You can remove a container using following command.

> docker container rm <container>

Again, <container> should be replaced with the container ID or name that you see in the results of list command shown above.

Remove a Docker image

You can remove a docker image with following command but before that you need to remove all the containers which were created using that image.

> docker image rm <image_id>

You can find <image_id> of your image with docker image ls command.

Tip

Before concluding, one useful tip! For the illustration purposes, I pulled the docker image first and then based on it created the container. So actually we did following two steps:

> docker pull alpine
> docker container run -it alpine /bin/sh

However, if we only execute the second command directly, it will serve the purpose as well. When we run the second command directly, Docker checks the local repository for the alpine image. If it can not find that image in local repository, it will pull that image from Dockerhub automatically as you can also see in the screenshot below.

The results of docker container run without doing docker pull.

So guys! that’s all for today. I hope this tutorial will help you taking the first steps with Docker. In second part, we’ll continue further to build our own docker images! I hope you’ll be excited to see the second part and learn more about Docker!

0 0 vote
Article Rating

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy
0
Would love your thoughts, please comment.x
()
x