Main Docker Commands

Pull an image or a repository from a registry

If no tag is provided, Docker Engine uses the :latest tag as a default. This command pulls the debian:latest image:

 docker pull redis

Displaying Docker Images

AsafsMacBook:~ aahmadov$ docker images 
REPOSITORY               TAG       IMAGE ID       CREATED        SIZE
hello-world              latest    46331d942d63   2 months ago   9.14kB
redis                    latest    23d787aaa419   2 months ago   107MB
docker/getting-started   latest    adfdb308d623   3 months ago   27.4MB

Run a command in a new container

AsafsMacBook:~ aahmadov$ docker run redis
1:C 28 May 2022 04:26:22.809 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 28 May 2022 04:26:22.809 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 28 May 2022 04:26:22.809 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 28 May 2022 04:26:22.809 * monotonic clock: POSIX clock_gettime
1:M 28 May 2022 04:26:22.811 * Running mode=standalone, port=6379.
1:M 28 May 2022 04:26:22.811 # Server initialized
1:M 28 May 2022 04:26:22.812 * Ready to accept connections

List containers

AsafsMacBook:~ aahmadov$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS      NAMES
4683ec9514aa   redis     "docker-entrypoint.s…"   About a minute ago   Up About a minute   6379/tcp   serene_galois
OMLF4D4887B80A5:~ aahmadov$ 

The docker ps command only shows running containers by default. To see all containers, use the -a (or --all) flag:

AsafsMacBook:~ aahmadov$ docker ps -a
CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS                        PORTS      NAMES
b7421b65d01a   redis:4.0                "docker-entrypoint.s…"   6 minutes ago    Up 6 minutes                  6379/tcp   quizzical_wilson
9e0452128f10   redis:latest             "docker-entrypoint.s…"   6 minutes ago    Up 6 minutes                  6379/tcp   strange_jones
98b3dd2527ac   hello-world              "/hello"                 2 months ago     Exited (0) 2 months ago                  gracious_shirley
1e606fd2c79b   docker/getting-started   "/docker-entrypoint.…"   2 months ago     Created                                  stoic_kilby

Stop and Start container

AsafsMacBook:~ aahmadov$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS      NAMES
7e6fb58a6255   redis     "docker-entrypoint.s…"   24 seconds ago   Up 23 seconds   6379/tcp   vigilant_lalande
AsafsMacBook:~ aahmadov$ docker stop 7e6fb58a6255 [Container ID]
7e6fb58a6255
AsafsMacBook:~ aahmadov$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
AsafsMacBook:~ aahmadov$ docker start 7e6fb58a6255

Docker detached mode

We use Docker detached mode (-d) when we want to connect to the container in an interactive mode or we can say when we just want application to be running inside the container. This means, we can start up the container and could use the console after startup for other commands.

It runs the container in the background of your terminal. It does not receive input or display output.

AsafsMacBook:~ aahmadov$ docker run -d redis

Download and run different version of images

AsafsMacBook:~ aahmadov$ docker pull redis:4.0
 4.0: Pulling from library/redis
 bdc84a41f251: Pull complete 
 698d3e76f19f: Pull complete 
 ac4bbea6248c: Pull complete 
 2aeaea3e9c04: Pull complete 
 6fb4291f955c: Pull complete 
 14ae5d3407b5: Pull complete 
 Digest: sha256:2e03fdd159f4a08d2165ca1c92adde438ae4e3e6b0f74322ce013a78ee81c88d
 Status: Downloaded newer image for redis:4.0
 docker.io/library/redis:4.0
AsafsMacBook:~ aahmadov$ docker images
 REPOSITORY               TAG       IMAGE ID       CREATED        SIZE
 hello-world              latest    46331d942d63   2 months ago   9.14kB
 redis                    latest    23d787aaa419   2 months ago   107MB
 docker/getting-started   latest    adfdb308d623   3 months ago   27.4MB
 redis                    4.0       7a8c2384752f   2 years ago    83.5MB
AsafsMacBook:~ aahmadov$ docker run -d redis:latest
9e0452128f1068aef41bba90529221631789d8728aae8b05272c63e141ab8790
AsafsMacBook:~ aahmadov$ docker run -d redis:4.0
b7421b65d01a422cea68b7ee8073658af4822659591e15d142b5c6a4fce8f116
AsafsMacBook:~ aahmadov$ docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS      NAMES
b7421b65d01a   redis:4.0      "docker-entrypoint.s…"   5 seconds ago    Up 5 seconds    6379/tcp   quizzical_wilson
9e0452128f10   redis:latest   "docker-entrypoint.s…"   11 seconds ago   Up 10 seconds   6379/tcp   strange_jones

Example – install and configure Jenkins container

I will use pull command to download the Jenkins Image into the local Ubuntu server.

AsafsMacBook:~ aahmadov$ docker pull jenkins 

To understand what ports are exposed by the container, we should use the Docker inspect command to inspect the image. This method allows one to return low-level information on the container or image.(JSON format)

AsafsMacBook:~ aahmadov$ docker inspect jenkins 

To run Jenkins and map the ports, you need to change the Docker run command and add the ‘p’ option, which specifies the port mapping. So, I need to run the following command :

AsafsMacBook:~ aahmadov$ docker run -p 8080:8080 -p 50000:50000 jenkins 

The left-hand side of the port number mapping is the Docker host port to map to, and the right-hand side is the Docker container port number.

When i open the browser and navigate to the Docker host on port 8080, you will see Jenkins up and running.

Last updated