Docker: most used Docker commands

command line

Docker has been around for a few years but it sounded too complicated and I never knew exactly what problem it was solving. Only recently I learned about it and started using Docker both at work and on my personal projects.

I started writing a post explaining some few basic stuff about Docker but it turned out to be a long and boring post. So instead, here’s more of a get-going-fast information I wish I had back then (and some resources for further information).

What is Docker?

Docker is a platform that allows you to isolate your running environment in a container. You can run your application with all its dependencies inside the container without having to worry about dependency version conflicts, for example.

What is a container?

A container is a “running” instance of an image.

What is an Image?

An image is nothing more than a set of steps that describe what your container should look like when it’s up and running (which OS, applications, volumes, ports, etc). This description goes in the Dockerfile and it is used as the basis to build the image (in other words, to implement the steps and prepare what will be a container). So… maybe we could say that an image is a “implemented” or “executed” description.

The process

  1. Do this only once: install Docker locally

2. get an image: either copy (“pull”) one from a registry (like Docker Hub, for example) or write your own. If you choose to write your own you have to create a “Dockerfile” (the description steps) and you will also have to “build” that image.

3. run the image: this will be a container.

That’s it.

Basic Docker Commands:

Pull (copy) an image from Docker’s registry (Docker Hub):

docker pull image-name

Build an image tagged as “image-tag”:

docker build --tag image-tag --file path-to-your-Dockerfile

List all local images:

docker images

Run an image to create a container (this is the simplest version of this command and I almost never use it like this):

docker run image-tag

where: image-tag is an image that I built

Run an image in the background (‘detached’), mapping host port 80 to container port 8080:

docker run -d -p 80:8080 image-tag

List all running containers:

docker ps

List all containers (including the stopped ones):

docker ps -a

Exec into the container (aka, access the container via its command line):

docker exec -it image-name bash

NOTE: bash is the command that will create a bash session in the container; you might need something else here but this will work for the majority of cases

Stop containers:

docker stop image-name

Remove containers:

docker rm image-name

** NOTE: it’s also possible to use the ‘container id’ instead of the ‘image-name’ in most of the commands above (except build and run because in these cases the container id will only be created when the container starts). This can be handy if you have a very long image name for example, so using the first couple or three letters of the container id will be enough to run the docker command.

What about Docker compose?

Docker compose combine multiple images in one neat package so you can run this package to bring up all images that were listed in the docker compose file at once, instead of manually running each individual image. Each image will be its own container.

One very common use for me is having a docker compose file that has an image for an application (django, rails, .NET) and one image for the database. Once you run that “package” both containers will be up and you can access them individually via docker exec, for example.

To use docker compose, you have to have:

  1. the images of the applications you want to group together
  2. a docker compose file that will describe how these images will interact – very common in the scenario I described above is to make sure to only bring the application container up after the database container is running, for example.

Then, from the directory where the docker compose file is, you can run it with:

docker-compose up -d

** NOTE:  -d runs in the background (detached mode) and is optional

To stop all containers:

docker-compose down

This is a lot of info but it describes some simple use cases that can get one started with Docker.


The post Docker: most used Docker commands was originally published on

Related Posts

One thought on “Docker: most used Docker commands

Comments are closed.