DEV Community

prash 1
prash 1

Posted on

# My First Docker Container: Installation and Essential Commands

My First Docker Container: Installation and Essential Commands

In the previous article, we learned what Docker is and why it became one of the most important tools in modern software development and DevOps.

Now it’s time to do something practical.

In this article, we will install Docker and run our first Docker container. By the end, you will understand the most important Docker commands that every beginner should know.

Installing Docker

Docker is available for Windows, macOS, and Linux.

Windows

Download Docker Desktop from the official Docker website and complete the installation.

After installation, open Docker Desktop and wait until Docker starts successfully.

Linux (Ubuntu)

Update the package list:

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Install Docker:

sudo apt install docker.io -y
Enter fullscreen mode Exit fullscreen mode

Start the Docker service:

sudo systemctl start docker
Enter fullscreen mode Exit fullscreen mode

Enable Docker to start automatically after reboot:

sudo systemctl enable docker
Enter fullscreen mode Exit fullscreen mode

Check whether Docker is installed correctly:

docker --version
Enter fullscreen mode Exit fullscreen mode

You should see an output similar to:

Docker version 28.x.x
Enter fullscreen mode Exit fullscreen mode

Running your first Docker command

The easiest way to test Docker is:

docker run hello-world
Enter fullscreen mode Exit fullscreen mode

This command downloads a small image called hello-world and runs it.

You should see a welcome message confirming that Docker is working correctly.

Congratulations! You have just run your first Docker container.

Understanding what happened

The command:

docker run hello-world
Enter fullscreen mode Exit fullscreen mode

actually performs several steps automatically.

  1. Docker looks for the hello-world image locally.
  2. If it is not available, Docker downloads it from Docker Hub.
  3. Docker creates a container from that image.
  4. Docker starts the container.
  5. The container prints a message and exits.

This single command demonstrates the basic Docker workflow.

Docker images

A Docker image is a template used to create containers.

To see downloaded images:

docker images
Enter fullscreen mode Exit fullscreen mode

Example output:

REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    abc123xyz      2 weeks ago   13KB
Enter fullscreen mode Exit fullscreen mode

The most important columns are:

  • Repository – image name
  • Tag – version
  • Image ID – unique identifier
  • Size – image size

Downloading an image manually

Instead of running a container immediately, you can download an image first.

For example:

docker pull nginx
Enter fullscreen mode Exit fullscreen mode

This downloads the Nginx web server image.

Verify:

docker images
Enter fullscreen mode Exit fullscreen mode

You should now see the Nginx image.

Running a container

To create and start a container:

docker run nginx
Enter fullscreen mode Exit fullscreen mode

Docker starts the Nginx web server.

However, the terminal becomes occupied because the container is running in the foreground.

Stop it with:

Ctrl + C
Enter fullscreen mode Exit fullscreen mode

Running a container in the background

Use the -d option (detached mode):

docker run -d nginx
Enter fullscreen mode Exit fullscreen mode

Docker returns a long container ID.

The container continues running in the background.

Viewing running containers

To see active containers:

docker ps
Enter fullscreen mode Exit fullscreen mode

Example:

CONTAINER ID   IMAGE   STATUS
a12b34c56d78   nginx   Up 2 minutes
Enter fullscreen mode Exit fullscreen mode

Useful columns:

  • Container ID
  • Image
  • Status

Viewing all containers

Even stopped containers are stored on your system.

To see them:

docker ps -a
Enter fullscreen mode Exit fullscreen mode

This shows:

  • running containers
  • stopped containers
  • exited containers

Stopping a container

First, find the container ID:

docker ps
Enter fullscreen mode Exit fullscreen mode

Then stop it:

docker stop container_id
Enter fullscreen mode Exit fullscreen mode

Example:

docker stop a12b34c56d78
Enter fullscreen mode Exit fullscreen mode

Docker stops the running container.

Starting a stopped container

To start it again:

docker start container_id
Enter fullscreen mode Exit fullscreen mode

Example:

docker start a12b34c56d78
Enter fullscreen mode Exit fullscreen mode

The container resumes execution.

Removing a container

A stopped container can be deleted.

docker rm container_id
Enter fullscreen mode Exit fullscreen mode

Example:

docker rm a12b34c56d78
Enter fullscreen mode Exit fullscreen mode

If the container is running, stop it first.

Removing an image

To delete an image:

docker rmi nginx
Enter fullscreen mode Exit fullscreen mode

If a container is using the image, Docker will prevent removal.

Delete the container first, then remove the image.

Running Ubuntu interactively

One of the most useful beginner commands is:

docker run -it ubuntu
Enter fullscreen mode Exit fullscreen mode

The options mean:

  • -i = interactive
  • -t = terminal

You enter a shell inside the Ubuntu container.

Try:

ls
pwd
echo Hello Docker
Enter fullscreen mode Exit fullscreen mode

To exit:

exit
Enter fullscreen mode Exit fullscreen mode

The container stops after exiting the shell.

Giving a container a name

Instead of using random IDs:

docker run -d --name mynginx nginx
Enter fullscreen mode Exit fullscreen mode

Now the container has the name mynginx.

Stop it:

docker stop mynginx
Enter fullscreen mode Exit fullscreen mode

Start it:

docker start mynginx
Enter fullscreen mode Exit fullscreen mode

Remove it:

docker rm mynginx
Enter fullscreen mode Exit fullscreen mode

Names are much easier to remember than container IDs.

Checking container logs

To view application logs:

docker logs mynginx
Enter fullscreen mode Exit fullscreen mode

This is extremely useful for debugging.

For continuous log monitoring:

docker logs -f mynginx
Enter fullscreen mode Exit fullscreen mode

Press Ctrl + C to stop following the logs.

Executing commands inside a running container

Suppose Nginx is running.

Open a shell inside it:

docker exec -it mynginx bash
Enter fullscreen mode Exit fullscreen mode

Some images use sh instead of bash:

docker exec -it mynginx sh
Enter fullscreen mode Exit fullscreen mode

Now you are working inside the running container.

Exit with:

exit
Enter fullscreen mode Exit fullscreen mode

The five commands you should memorize

If you are completely new to Docker, focus on these commands first.

Download an image:

docker pull nginx
Enter fullscreen mode Exit fullscreen mode

Run a container:

docker run -d --name mynginx nginx
Enter fullscreen mode Exit fullscreen mode

View containers:

docker ps
Enter fullscreen mode Exit fullscreen mode

Stop a container:

docker stop mynginx
Enter fullscreen mode Exit fullscreen mode

Remove a container:

docker rm mynginx
Enter fullscreen mode Exit fullscreen mode

These commands cover most beginner Docker tasks.

A quick practice exercise

Try this sequence yourself:

docker pull nginx
docker run -d --name web nginx
docker ps
docker logs web
docker stop web
docker start web
docker exec -it web bash
exit
docker stop web
docker rm web
Enter fullscreen mode Exit fullscreen mode

Running these commands once is far better than reading them ten times.

What’s next?

In the next article, we will learn Docker architecture in detail. We will understand images, containers, Docker Hub, and the Docker engine, and see what happens behind the scenes when you run a Docker command.

The more you practice these basic commands, the easier Docker will become.

Happy learning!

Top comments (0)