Basic Docker Commands

Docker Pull

Syntax:

docker pull <image_name>

Example:

docker pull nginx/latest

Use: The docker pull command is used to fetch (download) Docker images from a specified registry (like Docker Hub) and store them locally on your machine. It allows users to obtain the necessary images before creating and running containers based on those images.

Docker Run

Syntax:

docker run <flags> <imagename>

Example:

docker run --name nginx nginx/latest

Use: The docker run command creates and starts a new Docker container from a specified image. It can also be used to configure how the container behaves, such as attaching it to networks or mounting volumes.

Docker Stop

Syntax:

docker stop <image_name_or_id>

Example:

docker stop nginx

Use: The docker stop command halts a running container, allowing it to gracefully shut down. This command sends a SIGTERM signal to the container, giving it time to clean up before stopping completely.

Docker Start

Syntax:

docker start <image_name_or_id>

Example:

docker start nginx

Use: The docker start command restarts a previously stopped container. It resumes the container from its previous state, including any changes made to its file system or settings before it was stopped.

Docker Remove

Syntax:

docker rm <image_name_or_id>

Example:
(Note: A container can’t be removed while it’s running, so if you’re following along, stop the nginx container before removing it.)

docker rm nginx 

Use: The docker rm command deletes one or more stopped containers from your local machine. This is useful for cleaning up after containers that are no longer needed.

Docker Inspect

Use: The ‘inspect’ command provides detailed information about Docker objects such as containers, images, volumes, networks, and more. It returns JSON metadata that includes configuration details, network settings, and volume mounts, which can be useful for troubleshooting or understanding how Docker components are configured.

To use this command, let’s first create a container and attach it to a volume.

docker run -d -v nginx_logs:/var/log/nginx -p 80:80 --name nginx_example nginx/latest

If you’re following along: pause your reading now, and open your browser pointing it to http://localhost:80 (or the ip for the machine that’s hosting Docker) this will show you the nginx welcome page (the pudding in the proof, that our container is up and running)
Now let’s go back to reading…
So what did we do here?

  • docker run : Command to start a new Docker container.
  • -d : Runs the container in detached mode, meaning it runs in the background.
  • -v nginx_logs:/var/log/nginx : Creates a Docker volume named nginx_logs and mounts it to /var/log/nginx inside the container. This allows logs generated by Nginx within the container to be stored persistently on the host machine.
  • -p 80:80 : Maps port 80 of the host to port 80 of the container. This allows HTTP traffic to reach the Nginx server running inside the container.
  • name nginx_example : Names the container as nginx_example.
  • nginx:latest : Specifies the Docker image to use (nginx) and the tag (latest), which pulls the latest version of the Nginx image from the Docker Hub repository if not already available locally.

Now that we have that running, let’s inspect the container and volume. Syntax:

docker inspect <image_name_or_id>
docker volume inspect <volume_name_or_id>

Example(s):

docker inspect nginx # Will show information about the container we've created named 'nginx'
docker volume inspect nginx_data # Will show information about the volume we've created named 'nginx_logs'

Before deleting everything let’s run the following command that will list all running containers, the -a flag shows ALL containers even if they’re not currently running.

docker ps -a

Now we can see our nginx container.

Lastly, lets stop and delete the container and its associated volume.

docker stop nginx

# Take a moment to list runnning containers without the `-a` flag
docker ps
# Notice you're not seeing the nginx container because it's not running anymore.

docker rm nginx
docker volume rm nginx_logs

Docker List (ls)

docker image ls # Lists all downloaded images
docker volume ls # Lists all volumes available on current host
docker network ls # Lists all available docker networks

Docker Journey

Now that we know these basic commands, the next time we’ll use them won’t be your first. Let’s head over to this next tutorial and get some hands on experience before deploying our first USABLE service using Docker.