Back to all posts

5 Docker Commands to Learn First

Use five basic Docker commands to download an image, start a container, check it, read its logs, and stop it.

Watch the video

Before you start

Make sure Docker is installed and running. You also need a terminal where the docker command works.

In the video, I use the keyboard and NVDA throughout the demonstration. You will hear NVDA read some of the commands and terminal output.

An image contains the files and configuration needed to run an application. A container is an instance created from an image. This example uses the Nginx Alpine image to run a small web server.

1. Download an image with docker pull

docker pull downloads an image from a registry. This command downloads the Nginx Alpine image from Docker Hub:

docker pull nginx:alpine

nginx is the image name. alpine is the tag.

2. Create and start a container with docker run

docker run creates and starts a container from the image:

docker run --name youtube-test -d -p 8311:80 nginx:alpine

Here is what the options do:

  • --name youtube-test gives the container a name.
  • -d runs it in the background.
  • -p 8311:80 connects port 8311 on your computer to port 80 in the container.

Open http://localhost:8311 in a browser. You should see the default Nginx page. If port 8311 is already in use, choose another unused port.

The command in the video publishes port 8311 on all network interfaces. To limit it to your computer, replace -p 8311:80 with -p 127.0.0.1:8311:80.

3. List containers with docker ps

docker ps shows running containers:

docker ps

To include stopped containers, add -a:

docker ps -a

In the video, I use docker ps | grep nginx to filter the output because I already have other containers running. Start with docker ps if you want to see everything that is running.

4. Read container output with docker logs

docker logs shows the output collected from a container. Use the container name from the docker run command:

docker logs youtube-test

5. Stop a container with docker stop

docker stop stops a running container:

docker stop youtube-test

After it stops, docker ps will not show it. docker ps -a will still show it because stopping a container does not remove it.

Bonus: Remove the container with docker rm

When you are finished with the stopped container, remove it with:

docker rm youtube-test

Run docker ps -a again and youtube-test should be gone.

Sources and further reading