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
Install Docker:
sudo apt install docker.io -y
Start the Docker service:
sudo systemctl start docker
Enable Docker to start automatically after reboot:
sudo systemctl enable docker
Check whether Docker is installed correctly:
docker --version
You should see an output similar to:
Docker version 28.x.x
Running your first Docker command
The easiest way to test Docker is:
docker run hello-world
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
actually performs several steps automatically.
- Docker looks for the hello-world image locally.
- If it is not available, Docker downloads it from Docker Hub.
- Docker creates a container from that image.
- Docker starts the container.
- 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
Example output:
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest abc123xyz 2 weeks ago 13KB
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
This downloads the Nginx web server image.
Verify:
docker images
You should now see the Nginx image.
Running a container
To create and start a container:
docker run nginx
Docker starts the Nginx web server.
However, the terminal becomes occupied because the container is running in the foreground.
Stop it with:
Ctrl + C
Running a container in the background
Use the -d option (detached mode):
docker run -d nginx
Docker returns a long container ID.
The container continues running in the background.
Viewing running containers
To see active containers:
docker ps
Example:
CONTAINER ID IMAGE STATUS
a12b34c56d78 nginx Up 2 minutes
Useful columns:
- Container ID
- Image
- Status
Viewing all containers
Even stopped containers are stored on your system.
To see them:
docker ps -a
This shows:
- running containers
- stopped containers
- exited containers
Stopping a container
First, find the container ID:
docker ps
Then stop it:
docker stop container_id
Example:
docker stop a12b34c56d78
Docker stops the running container.
Starting a stopped container
To start it again:
docker start container_id
Example:
docker start a12b34c56d78
The container resumes execution.
Removing a container
A stopped container can be deleted.
docker rm container_id
Example:
docker rm a12b34c56d78
If the container is running, stop it first.
Removing an image
To delete an image:
docker rmi nginx
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
The options mean:
- -i = interactive
- -t = terminal
You enter a shell inside the Ubuntu container.
Try:
ls
pwd
echo Hello Docker
To exit:
exit
The container stops after exiting the shell.
Giving a container a name
Instead of using random IDs:
docker run -d --name mynginx nginx
Now the container has the name mynginx.
Stop it:
docker stop mynginx
Start it:
docker start mynginx
Remove it:
docker rm mynginx
Names are much easier to remember than container IDs.
Checking container logs
To view application logs:
docker logs mynginx
This is extremely useful for debugging.
For continuous log monitoring:
docker logs -f mynginx
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
Some images use sh instead of bash:
docker exec -it mynginx sh
Now you are working inside the running container.
Exit with:
exit
The five commands you should memorize
If you are completely new to Docker, focus on these commands first.
Download an image:
docker pull nginx
Run a container:
docker run -d --name mynginx nginx
View containers:
docker ps
Stop a container:
docker stop mynginx
Remove a container:
docker rm mynginx
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
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)