Skip to content

Latest commit

 

History

History
204 lines (181 loc) · 4.64 KB

docker_commands.md

File metadata and controls

204 lines (181 loc) · 4.64 KB

Docker Commands

The basic commands for the Docker CLI.


1. Docker without sudo

  • To run docker commands as normal user without sudo, we need to create a group for docker and add the user to it.
  1. Create the docker group
    $ sudo groupadd docker
  2. Add your user to docker group
    $ sudo usermod -aG docker $USER
  3. Activate the changes to groups:
    $ newgrp docker
  4. Verify that you can run docker commands without sudo.
    $ docker ps

2. Docker version

To find the installed docker version Command:

docker  --version

Example:

docker --version
Docker version 19.03.9, build 9d988398e7

3. Downloading image

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

4. List all the docker images

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

5. Run docker image

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

6. Running containers

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

7. Access the docker container

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


8. Stop the container

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.


9. List all the containers

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

10. Start the container

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

11. Remove the container

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


12. Removing image

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