DEV Community

Cover image for 1MinDocker #4 - Docker CLI essentials
Astra Bertelli
Astra Bertelli

Posted on

1MinDocker #4 - Docker CLI essentials

In the last article we explored the fundamental concepts of Docker: with this theory background, we can now start getting our hands dirty with some code!

From now on, we will use the command line interface (CLI) instead of Docker Desktop, as this is the easiest way to get to know Docker from the developer side🤗

0. Where do we work?

As the name says, a CLI needs a command line to work. A command line is actually part of a bigger interface, a command prompt or terminal. Docker's commands, differently from most of other commands, work in the same way on every platform, we just need to find out where we will be working:

You will find guides on how to activate the mentioned tools following the links.

1. Download and run our first image

Let's say we don't have Ubuntu on our machine and we want to have it up and running. Instead of downloading it as a distro for WSL or setting up a whole machine, we can pull the image from Docker Hub and run it on our machine:

docker pull ubuntu
Enter fullscreen mode Exit fullscreen mode

This is the easiest way in which we could perform the pull: we are directly calling the image name, without specifying the author or the version.
To do a complete pull, we should use this convention:

docker pull author/image:tag
Enter fullscreen mode Exit fullscreen mode

The tag is, in a way, similar to a version (it can be an actual version, a name or a product status)

We can write, for example:

docker pull astrabert/books-mixer-ai:1.1.0
Enter fullscreen mode Exit fullscreen mode

By default, when we don't specify the tag, Docker pulls the latest image. Find a complete reference for the pull command here.

Now that we have Ubuntu as a Docker image, we can try to run it, by simply doing:

docker run ubuntu:20.04
Enter fullscreen mode Exit fullscreen mode

Or we can add a command, like:

docker run ubuntu:20.04 echo "Hello world!"
Enter fullscreen mode Exit fullscreen mode

If we want to really interact with our container from command line, we should run it in interactive mode:

docker run -i ubuntu:20.04
Enter fullscreen mode Exit fullscreen mode

This will actually start an Ubuntu shell directly in our terminal.

If we want to expose some ports because we will run programs inside of the container that will require that, we can simply map our local machine ports to the container's ones:

docker run -i -p 3000:3000 -p 4000:8000 ubuntu:20.04
Enter fullscreen mode Exit fullscreen mode

In this case, our local port 3000 corresponds to container's port 3000, whereas our local port 4000 corresponds to container's port 8000.

If we also would like to inject our local file system into our Docker container, we need to mount a volume:

docker run -p 3000:3000 -p 4000:8000 -i -v /home/user/data/:/opt/volume/ ubuntu:20.04
Enter fullscreen mode Exit fullscreen mode

Now all the data under /home/user/data on your machine will be accessible under /opt/volume in Ubuntu's container.

We can also add environment variables or an environment file:

docker run -p 3000:3000 -p 4000:8000 -i -v /home/user/data/:/opt/volume/ -e VARENV=foo --env-file ./.env.local ubuntu:20.04
Enter fullscreen mode Exit fullscreen mode

There are lots of other options we can add to the run command, and we'll see some of them later on: for now, you can find a complete reference here

2. Let's see available image and containers

If we want to see our current downloaded images, we can run:

docker images
Enter fullscreen mode Exit fullscreen mode

And if we want to find all the containers, we can just run:

docker ps -a
Enter fullscreen mode Exit fullscreen mode

We can also filter our containers: for example, if we started multiple containers and we want to see which ones are running, we can do this by inputting:

docker ps --filter "status=running"
Enter fullscreen mode Exit fullscreen mode

If we want only the container IDs, we can run:

docker ps -a -q
Enter fullscreen mode Exit fullscreen mode

Find the complete reference for docker images and docker ps following the two links.

3. Stop and eliminate a container, remove an image

To stop a container, we can use:

docker stop <container-id>
Enter fullscreen mode Exit fullscreen mode

We can find the container ID in the list previously generated with ps.

Once a container is stopped, we can remove it by running:

docker rm <container-id>
Enter fullscreen mode Exit fullscreen mode

If we want to stop all the containers and then remove them, we can run:

docker stop $(docker ps -a)
docker rm $(docker ps -aq)
Enter fullscreen mode Exit fullscreen mode

If we want to remove an image from our local registry, we can run:

docker rmi author/image:tag
Enter fullscreen mode Exit fullscreen mode

Always remember that rm and rmi are different!😊

A complete reference for the two commands can be found here and here.

We will stop here for this article, but in the next one we will dive deep into how to build our first image ever and to push it to the Docker Hub!
For now, remember to sign up to Docker portal, as it will be very important for our next steps🥰

Top comments (2)

Collapse
 
michelemauro profile image
michelemauro

This will actually start an Ubuntu shell directly in our terminal.

Well... almost. That command will launch a container based on the main ubuntu image, where the only program running will be the shell that you launched. That shell will only see the filesystem contained in the image. It will appear as a single process in your machine, among the others; it will however believe to be completely alone, and it won't be able to access your machine's hardware; i.e. it won't be able to start a graphic desktop and display windows. And when you close the shell, the container will stop.

A Virtual Machine would have quite a different experience.

This article series is well written, with a fresh and readable prose; while your descriptions are not really wrong per se, they are not completely correct either. That's not necessarly a bad thing - sometimes correct and precise definitions can wait until you have gained enough experience. But I'm afraid this may confuse someone that is reading and trying to match it with other documentation.

Collapse
 
astrabert profile image
Astra Bertelli

Hi @michelemauro and thanks so much for your feedback!
I'll be correcting the errors/inaccuracies that you pointed out both for this and for 1minDocker #3 :)
I'm really grateful for your attentive feedback and would be even more grateful if you could report everything directly on the GitHub repo ( see CONTRIBUTION GUIDELINES) so that I can also list you as a contributor for your valuable reviews!