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.
docker pull — Pulling an ImageTo bring any Docker image from Docker Hub into our local environment, we use the docker pull command:
docker pull hello-worldWhen 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: latestMeaning we have pulled the latest version of this image.
docker images — Listing All ImagesTo check which Docker images exist on our system:
docker imagesRight 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.
docker run — Creating and Running a ContainerTo create a container from an image, we use the docker run command:
docker run hello-worldFrom 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:
When we ran the hello-world container, it printed the entire process of how a container is created and executed:
hello-world image from Docker Hub.The Docker Daemon is the most important part of Docker Desktop that does all the work for us.
It is responsible for:
docker ps — Listing Containersdocker ps -adocker psBoth containers we created have their status as:
ExitedBy default:
docker ps -a.-it)docker run -it ubuntuThe -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:
ls # List files and folders
mkdir test-folder # Create a new directory
env # Print environment variables
exit # Exit the containerAs soon as we type:
exitour container automatically stops and moves into the Stopped state.
docker start and docker stopThe docker run command always creates a new container from an image.
The docker start command is used to start an already existing (stopped) container.
docker start <container-name-or-id>After starting:
docker pswe can see the container is now in the Running state.
docker stop <container-name-or-id>After stopping:
docker pswill show no running containers.
docker rmi — Removing an Imagedocker rmi <image-id-or-name>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 containerTherefore:
docker rm — Removing a Containerdocker rm <container-name-or-id>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 deletedLet'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.
docker pull mysqlSince no tag is specified, Docker automatically pulls:
mysql:latestdocker pull mysql:8.0This downloads the MySQL 8.0 image specifically.
When we pull multiple versions of the same image, some layers say:
Already existsThis 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:
docker run -d --name mysql-latest -e MYSQL_ROOT_PASSWORD=secret mysqldocker run -d --name mysql-older -e MYSQL_ROOT_PASSWORD=secret mysql:8.0This allows us to run different versions of MySQL simultaneously.
For example:
Docker containers make version isolation easy.
docker run-d)By default, all containers run in attached mode.
Using -d, we can run a container in the background.
docker run -d mysqlBenefits:
--name)Instead of Docker assigning a random name, we can provide our own:
docker run -d --name mysql-older mysql:8.0Now the container can be referenced using:
mysql-olderinstead of a randomly generated name.
-e)Some containers require environment variables to be declared.
For example, MySQL requires a root password:
docker run -d -e MYSQL_ROOT_PASSWORD=secret mysqlThe environment variable:
MYSQL_ROOT_PASSWORD=secretis passed into the container during startup.
docker run -d \
--name mysql-older \
-e MYSQL_ROOT_PASSWORD=secret \
mysql:8.0-d--name mysql-older-e MYSQL_ROOT_PASSWORD=secretmysql:8.0After running the containers:
docker psExample output:
CONTAINER ID IMAGE STATUS
abc123 mysql Up
def456 mysql:8.0 UpWe will now have two running containers:
This demonstrates one of Docker's biggest advantages:
Running multiple isolated environments with different software versions on the same machine.