Docker Commands — Getting Started
Before using Docker practically, there are some basic commands we need to know well. We will practice these commands with the help of different images and containers.
We will start with a very simple image called hello-world — this is kind of the official Docker image that we use to start learning Docker. After that, we will increase the complexity of our images — we will look at examples of Ubuntu, MySQL, MongoDB, and multiple Docker images and containers.
Important Docker Commands
1. docker pull — Pulling an Image
To bring any Docker image from Docker Hub into our local environment, we use the docker pull command:
When we run this, Docker first checks if the image is available locally. If not, it pulls it from Docker Hub.
We can see it says:
Using default tag: latest
Meaning we have pulled the latest version of this image.
2. docker images — Listing All Images
To check which Docker images exist on our system:
Right now we have a single image which is hello-world.
Images have associated tags — tags are basically the versions or variants of a Docker image.
We can also see this image in Docker Desktop under the Images tab.
Notice the size of the image — it is literally in KBs.
This is why Docker is called a lightweight technology.
3. docker run — Creating and Running a Container
To create a container from an image, we use the docker run command:
From this, a new container is automatically created for us.
We can also see it in Docker Desktop under the Containers tab.
By default, Docker automatically gives any container a random name.
Containers also have:
- Their own unique ID
- Their current status
- The image they were created from
What Happens Internally?
When we ran the hello-world container, it printed the entire process of how a container is created and executed:
- The Docker client contacted the Docker Daemon.
- The Docker Daemon pulled the
hello-world image from Docker Hub.
- The Docker Daemon created a new container from that image which runs the executable that produced the output we are currently reading.
Docker Daemon
The Docker Daemon is the most important part of Docker Desktop that does all the work for us.
It is responsible for:
- Pulling images
- Creating containers
- Running containers
- Managing Docker resources
4. docker ps — Listing Containers
See All Containers (Including Stopped Ones)
See Only Running Containers
Both containers we created have their status as:
By default:
- Containers are assigned random names.
- Containers have unique IDs.
- Stopped containers are visible using
docker ps -a.
5. Running a Container in Interactive Mode (-it)
The -it option allows us to run a Docker container in interactive mode.
This mode allows us to access the container terminal, which helps us input or output anything.
So here we move from our host terminal (Mac Terminal) into the Ubuntu container's terminal.
Inside the container:
- We are in the root directory.
- The container ID is displayed in the prompt.
- We can execute Linux commands.
Example Commands
ls # List files and folders
mkdir test-folder # Create a new directory
env # Print environment variables
exit # Exit the container
As soon as we type:
our container automatically stops and moves into the Stopped state.
6. docker start and docker stop
The docker run command always creates a new container from an image.
The docker start command is used to start an already existing (stopped) container.
Start a Container
docker start <container-name-or-id>
After starting:
we can see the container is now in the Running state.
Stop a Running Container
docker stop <container-name-or-id>
After stopping:
will show no running containers.
7. docker rmi — Removing an Image
docker rmi <image-id-or-name>
Important
If an image is being used by a stopped container, we cannot delete it directly.
Docker will show an error similar to:
Image is being used by a stopped container
Therefore:
- Delete the container first.
- Then delete the image.
8. docker rm — Removing a Container
docker rm <container-name-or-id>
Full Cleanup Example
docker ps -a # See all containers
docker rm <container-id> # Remove the container
docker rmi hello-world # Now remove the image
docker images # Verify image is deleted
Working with Image Versions (Tags)
Let's say we now want to work with the MySQL image.
Tags are basically the versions or variants of the same Docker image.
Just as different versions of Node.js exist, we can also use different versions (tags) of Docker images.
Pulling the Latest Version
Since no tag is specified, Docker automatically pulls:
Pulling a Specific Version
This downloads the MySQL 8.0 image specifically.
Docker Image Layering
When we pull multiple versions of the same image, some layers say:
This is because those layers are common between the two versions and were already downloaded when we pulled the first version.
Only the layers that differ between the two versions are downloaded separately.
This is known as Docker Image Layering.
Benefits:
- Saves storage space
- Reduces download time
- Improves efficiency
Creating Containers from Different Versions
Container with Latest MySQL
docker run -d --name mysql-latest -e MYSQL_ROOT_PASSWORD=secret mysql
Container with MySQL 8.0
docker run -d --name mysql-older -e MYSQL_ROOT_PASSWORD=secret mysql:8.0
This allows us to run different versions of MySQL simultaneously.
For example:
- Application A can use MySQL Latest.
- Application B can use MySQL 8.0.
Docker containers make version isolation easy.
Additional Options with docker run
Detach Mode (-d)
By default, all containers run in attached mode.
Using -d, we can run a container in the background.
Benefits:
- Terminal remains free.
- Container keeps running.
- Ideal for databases and servers.
Naming a Container (--name)
Instead of Docker assigning a random name, we can provide our own:
docker run -d --name mysql-older mysql:8.0
Now the container can be referenced using:
instead of a randomly generated name.
Environment Variables (-e)
Some containers require environment variables to be declared.
For example, MySQL requires a root password:
docker run -d -e MYSQL_ROOT_PASSWORD=secret mysql
The environment variable:
MYSQL_ROOT_PASSWORD=secret
is passed into the container during startup.
Full Example with All Options
docker run -d \
--name mysql-older \
-e MYSQL_ROOT_PASSWORD=secret \
mysql:8.0
Breakdown
Run in Background
Custom Container Name
Environment Variable
-e MYSQL_ROOT_PASSWORD=secret
Image and Tag
Verifying Running Containers
After running the containers:
Example output:
CONTAINER ID IMAGE STATUS
abc123 mysql Up
def456 mysql:8.0 Up
We will now have two running containers:
- One with MySQL Latest
- One with MySQL 8.0
This demonstrates one of Docker's biggest advantages:
Running multiple isolated environments with different software versions on the same machine.