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

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

Stop and Start container

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.

Download and run different version of images

Example – install and configure Jenkins container

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

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)

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 :

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