The basic commands for the Docker CLI.
- To run docker commands as normal user without sudo, we need to create a group for docker and add the user to it.
- Create the docker group
$ sudo groupadd docker
- Add your user to docker group
$ sudo usermod -aG docker $USER
- Activate the changes to groups:
$ newgrp docker
- Verify that you can run docker commands without sudo.
$ docker ps
To find the installed docker version Command:
docker --version
Example:
docker --version
Docker version 19.03.9, build 9d988398e7
To work with any Docker image we need to download the docker image first.
Command:
docker pull <IMAGE>
Example of pulling alpine:latest image
docker pull alpine:latest
To list all the images that are available in the host machine.
Command:
docker images
Example:
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest c059bfaa849c 6 weeks ago 5.59MB
The docker run
command is used to launch Docker containers.
When the docker run
commands is executed an isolated process with its own filesystem and network is spawned.
Command:
docker run [options] <IMAGE>
Example of running alpine:latest image
- -t allows us to access the terminal
- -i gets stdin stream added to the container stdin
docker run -t -i alpine:latest
or
docker run -ti alpine:latest
exit
docker ps
will list the running containers
Command:
docker ps
Example:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8973c7347905 alpine:latest "/bin/sh" 2 minutes ago Up 2 minutes ecstatic_jang
docker exec
executes a command in a running container.
Command to execute into container:
docker exec [OPTIONS] <CONTAINER_ID> COMMAND
Explore options here
Example: Execute into running alpine:latest
container. Let us create one directory and a simple blank text file
inside the container.
docker exec -ti 8973c7347905 sh
/ # mkdir demo
/ # cd demo
/demo # touch helloworld.txt
/demo # ls
helloworld.txt
/demo #
mkdir
command to create a directory or folder
cd
change directory used to change the current working directory
touch
command to create a blank file
docker stop
command to stop the running container
Command:
docker stop [OPTIONS] <CONTAINER_ID>
or
docker stop [OPTIONS] <CONTAINER_NAME>
Explore options here
Example of stopping alpine:latest running container
docker stop 8973c7347905
or
docker stop <CONTAINER_NAME>
Here once you stop the container, the container is still available locally, but it is not in the running state.
docker ps -a
will list all the containers including stopped containers.
Example output:
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4cc4008815d8 alpine:latest "/bin/sh" 57 minutes ago Exited (137) 2 minutes ago
Let's start the stopped alpine:latest
container again.
Command:
docker start [OPTIONS] <CONTAINER_ID>
Explore options here
Example of starting alpine:latest container.
Before starting the container we need the container id,so let's get the container id by docker ps -a
command.
docker ps -a
docker start 4cc4008815d8
You can remove the container or multiple containers by docker rm
command.
Command
docker rm [OPTIONS] <CONTAINER...>
Explore Options here Example:
docker rm 4cc4008815d8
Note: Execute this only after you stop the container
You can remove the local images by docker rmi
command.
Command:
docker rmi [OPTIONS] <IMAGE_ID> / <IMAGE_ID...>
Example: Remove alpine:latest image
docker rmi c059bfaa849c