Do the optional precodure configuration to work better 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
Docker is a containerization system which packages and runs the application with its dependencies inside a container. There are several docker commands you must know when working with Docker.
To find the installed docker version Command:
docker -version
Example:
docker --version
Docker version 19.03.9, build 9d988398e7
To work with any ocker 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 is locallt available in the host machine, simply run the below command. This will list all the docker images in the local system.
Command:
docker images
Example:
REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest c059bfaa849c 6 weeks ago 5.59MB
docker images
The docker run command first creates a writeable container layer over the specified image, and then starts it using the specified command.
Command:
docker run [options] <IMAGE>
Explore options here
Example of running alpine:latest image, the options -t allows us to acces the terminal and -i gets stdin stream added. Basicaly using -ti adds the terminal driver.
docker run -t -i alpine:latest
or
docker run -ti alpine:latest
Let us check what all the container are running currently, The command. docker ps
will list only running containers
Command:
docker ps
Emaple:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8973c7347905 alpine:latest "/bin/sh" 2 minutes ago Up 2 minutes ecstatic_jang
The docker exec
command runs a new command in a running container. We need container id to execute into conatiner, get the container id by docker ps
.
Command to execute into container:
docker exec [OPTIONS] <CONTAINER_ID> COMMAND
Explore options here
Example: Execute into running alpine:latest container, and let us create one directory and a simple blank text file.
docker exec -ti 8973c7347905 sh
/ # mkdir demo
/ # cd demo
/demo # touch helloworld.txt
/demo # ls
helloworld.txt
/demo #
mkdir
command to create directory or folder
cd
change directory used to change the current working directory
touch
Touch command allows to create a blank file
Now let us stop the running container
Command:
docker stop [OPTIONS] <CONTAINER_ID>
Explore options here
Example of stopping alpine:latest running container
docker stop 8973c7347905
Here once you stop the container, the container is available locally but it is not in the running state. In the follwing step we will how to remove stopped container.
docker ps -a
will list all the containers including stopped containers.
Example output:
mis@mispl-lap-19:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4cc4008815d8 alpine:latest "/bin/sh" 57 minutes ago Exited (137) 2 minutes ago
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: Once you stop the container
Let's start the alpine:latest container again. Remember previously you have
Command:
docker start [OPTIONS] <CONTAINER_ID>
Explore options here
Example of starting alpine:latest container. Before to start 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 local images by docker rmi
command.
Command:
docker rmi [OPTIONS] <IMAGE_ID> / <IMAGE_ID...>
Example: Remove alpine:latest container
docker rmi c059bfaa849c