Introduction
In this article, I will explore various Docker commands, which are essential for managing your Docker containers and images. I will provide example code and output for each command to help you better understand their functionality.
docker pull
The docker pull
command allows you to download an image from a remote Docker registry, such as Docker Hub.
$ docker pull ubuntu:20.04
20.04: Pulling from library/ubuntu
Digest: sha256:cfb43ffdb609eb9c3ecd47d8e48e16f7abbb1c95b7340d8f883942e0730e950a
Status: Downloaded newer image for ubuntu:20.04
docker.io/library/ubuntu:20.04
docker build
The docker build
command creates Docker images from a Dockerfile and a build context (typically your project directory).
$ docker build -t yourusername/your-image:tag .
Sending build context to Docker daemon 2.048kB
Step 1/5 : FROM node:14
---> 91e0ff2b2a31
Step 2/5 : WORKDIR /app
---> Using cache
---> 0a0d332bddc2
Step 3/5 : COPY package*.json ./
---> Using cache
---> 7bf6e8c6d4ea
Step 4/5 : RUN npm install
---> Using cache
---> 15a0c0a4812e
Step 5/5 : COPY . .
---> 9adad8d1eeca
Successfully built 9adad8d1eeca
Successfully tagged yourusername/your-image:tag
docker run
The docker run
command creates and starts a new container from a Docker image.
$ docker run -d -p 8080:80 --name your-container yourusername/your-image:tag
ac7e2b3a3f7de342d0a16a7f13ea8c3425d58f5e5c5e5eb5
docker exec
The docker exec
command allows you to execute commands inside a running container.
$ docker exec -it your-container bash
root@ac7e2b3a3f7d:/app#
docker stop
The docker stop
command stops a running container.
$ docker stop your-container
your-container
docker start
The docker start
command starts a stopped container.
$ docker start your-container
your-container
docker restart
The docker restart
command restarts a running container.
$ docker restart your-container
your-container
docker push
The docker push
command uploads your Docker image to a remote registry.
$ docker push yourusername/your-image:tag
The push refers to repository [docker.io/yourusername/your-image]
tag: digest: sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef size: 1234
docker ps
The docker ps
command lists all running containers.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ac7e2b3a3f7d yourusername/your-image:tag "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 0.0.0.0:8080->80/tcp your-container
docker images
The docker images
command lists all Docker images available on your system.
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
yourusername/your-image tag 9adad8d1eeca 4 hours ago 939MB
ubuntu 20.04 2c047404e52d 2 weeks ago 72.9MB
node 14 91e0ff2b2a31 3 weeks ago 943MB
docker rm
The docker rm
command removes a stopped container.
$ docker rm your-container
your-container
docker rmi
The docker rmi
command removes an unused Docker image.
$ docker rmi yourusername/your-image:tag
Untagged: yourusername/your-image:tag
Deleted: sha256:9adad8d1eeca0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e
docker logs
The docker logs
command displays the logs of a container.
$ docker logs your-container
Starting application...
Listening on port 80
docker attach
The docker attach
command allows you to attach your terminal to a running container.
$ docker attach your-container
Listening on port 80
docker cp
The docker cp
command allows you to copy files between a container and your host system.
$ docker cp your-container:/app/data.txt /path/to/host/data.txt
(no output unless there's an error)
docker commit
The docker commit
command creates a new image from a container's changes.
$ docker commit -m "Added new features" -a "Your Name" your-container yourusername/your-image:new-tag
sha256:abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789
docker system prune
The docker system prune
command removes unused data, such as stopped containers, unused networks, and dangling images.
$ docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container