Docker is a containerization system that packages and runs applications with their dependencies inside containers. There are several Docker commands you must know when working with Docker.
To create the docker group and add your user:
-
Create the docker group:
sudo groupadd docker
-
Add your user to the docker group:
sudo usermod -aG docker $USER -
Activate the changes to groups:
newgrp docker
-
Verify that you can run docker commands without sudo:
docker images
To find the installed Docker version:
docker --versionExample output:
Docker version 27.3.1, build ce12230
To work with any Docker image, we need to download it first:
docker pull <IMAGE>Example of pulling Alpine image:
docker pull alpine:latestTo list all images available locally on your host machine:
docker imagesExample output:
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest c059bfaa849c 6 weeks ago 7.39MB
postgres 16 abc123xyz456 2 days ago 229MB
The docker run command creates a writeable container layer over the specified image and starts it:
docker run [options] <IMAGE>Common options:
-d- Run in detached mode (background)-it- Interactive mode with terminal-p- Publish container port to host-v- Mount volume--name- Assign a name to the container--rm- Automatically remove container when it exits
Explore all options here
Example of running Alpine image with interactive terminal:
docker run -it alpine:latest
# or
docker run -it alpine:latest shThe docker ps command lists only running containers:
docker psExample output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8973c7347905 alpine:latest "/bin/sh" 2 minutes ago Up 2 minutes ecstatic_jang
To list all containers including stopped ones:
docker ps -aThe docker exec command runs a new command in a running container:
docker exec [options] <CONTAINER_ID> <COMMAND>Explore options here
Example: Execute into a running Alpine container and create files:
# First, get container ID from docker ps
docker ps
# Then exec into it
docker exec -it 8973c7347905 sh
# Inside container:
/ # mkdir demo
/ # cd demo
/demo # touch helloworld.txt
/demo # ls
helloworld.txt
/demo # exitCommands used inside container:
mkdir- Create directorycd- Change directorytouch- Create empty filels- List files
Stop a running container:
docker stop [OPTIONS] <CONTAINER_ID>Explore options here
Example:
docker stop 8973c7347905Once stopped, the container still exists locally but is not running.
Start a stopped container:
docker start [OPTIONS] <CONTAINER_ID>Explore options here
Example:
# First list all containers to find the stopped one
docker ps -a
# Start it
docker start 8973c7347905Remove one or more containers:
docker rm [OPTIONS] <CONTAINER_ID>Explore options here
To remove a running container, use the -f (force) flag:
docker rm -f 8973c7347905Remove local images:
docker rmi [OPTIONS] <IMAGE_ID> or <IMAGE_NAME>Example:
docker rmi c059bfaa849c
# or by name
docker rmi alpine:latestTo force removal:
docker rmi -f alpine:latestView logs from a container:
docker logs <CONTAINER_ID>Follow logs in real-time:
docker logs -f <CONTAINER_ID>Clean up all stopped containers:
docker container pruneClean up all unused images:
docker image prune -a| Command | Description |
|---|---|
docker --version |
Show Docker version |
docker pull <img> |
Download an image |
docker images |
List local images |
docker run <img> |
Run a container |
docker ps |
List running containers |
docker ps -a |
List all containers |
docker exec <id> <cmd> |
Execute command in container |
docker stop <id> |
Stop a container |
docker start <id> |
Start a stopped container |
docker rm <id> |
Remove container |
docker rmi <img> |
Remove image |
docker logs <id> |
Show container logs |
docker system prune |
Remove unused data |