Docker Command Tutorial

Date: Oct 27, 2022

Related Tags:1. Basic operations on Docker
2. A Quick Start to Docker Image Management

Abstract: Docker containers have transformed from a icing on the cake technology to a must for deployment environments. Sometimes, as developers, we need to spend a lot of time debugging or studying Docker tools to help us be more productive. With every new wave of technology, we spend a lot of time learning.

Have you had a similar experience spending 1-2 days setting up a Docker cluster, or figuring out what code is causing a Docker container to fail to start? For most developers, it seems that the time we spend researching configurations and finding bugs outweighs the time it takes to actually roll out new features, especially when the environment you're using is still relatively new or immature.

Some of us are not so lucky to have a stable environment with a well-established CI/CD process. To this end, I have collected and sorted out the most commonly used Docker command tutorials for this day-to-day job from major mainstream Docker technical documentation websites.

For an exhaustive list of optional flags and parameters, see the Docker manual https://docs.docker.com/engine/reference/commandline/cli/

Tip: Every Docker command has built-in documentation, such as typing docker run --help to get help documentation. Typing docker run --help will generate the following help documentation.



I hope this article helped you get away from debugging and using Docker. As you read each command, pay attention to the description of the command.



Docker build



$ docker build
--build-arg ARTIFACTORY_USERNAME=timothy.mugayi
--build-arg ARTIFACTORY_SECRET_TOKEN=AP284233QnYX9Ckrdr7pUEY1F
--build-arg LICENSE_URL='https://source.com/license.txt'
--no-cache -t helloworld:latest .

You can build Docker images with optional build parameters. By default, Docker caches the first Dockerfile build, or builds after adding new layers to the image according to the run command in the Dockerfile, in order to speed up subsequent builds.

If you don't need caching, you can add a non-cached parameter as in the example above.

Note: Docker commands can be executed by name or by Docker container ID. in the following commands can be replaced with container ID or container name.





Run Docker containers



$ docker start

Start an existing container. We assume that the container has been downloaded and created.

$ docker stop
Stop a running Docker container.

$ docker stop $(docker container ls -aq)

If you have multiple Docker containers running and you want to stop all of them, you can type docker stop along with a list of all container IDs.

$ docker exec -ti [COMMAND]
Run a shell command inside a specific container.

$ docker run -ti —image [COMMAND]
Docker run is distinct from start . Fundamentally, Docker run does two things: (1) creates a new container of the image; (2) executes the container. If you wish to rerun a failed or exited container, use the docker start command.

$ docker run -ti —rm —image [COMMAND]
This is an interesting command designed to create and start containers at the same time. It also wants to run the command inside the container and then delete the container after the command execution is complete.

docker run -d :
Usage:
docker run -d helloworld:latest
If you wish to start the docker run command in a detached state (for example, as a daemon for Linux), you can add -d to the end of the run command.

$ docker pause
Suspend all running processes within a specific container.

$ docker ps -a
The above command will list all the Docker images that were run before. After figuring out the image you want to run, you can execute the following command. Make sure to change the container ID to reflect what the original docker ps -a command showed.

sudo docker run {container ID} -e AWS_DEFAULT_REGION=us-east-1
e INPUT_QUEUE_URL="https://sqs.us-east-1.amazonaws.com/my_input_sqs_queue.fifo"
e REDIS_ENDPOINT="redis.dfasdf.0001.cache.amazonaws.com:8000"
e ENV=dev
e DJANGO_SETTINGS_MODULE=engine.settings
e REDIS_HOST="cmgadsfv7avlq.us-east-1.redis.amazonaws.com"
e REDIS_PORT=5439
e REDIS_USER=hello
e REDIS_PASSWORD=trasdf**#0ynpXkzg
The above command shows how to run a Docker image with multiple environment variables passed in as arguments, representing a newline.



Debug Docker containers



You can use docker ps to get the current running container name.

$ docker history
example usage:
$ docker history my_image_name
The above command can display the history of a specific image. This information is very useful when you want to dig deeper into Docker images. Let's dive in, because it's very important to understand what this command does, and there's very little documentation on it.

When we're talking about Docker, images are built on layers that are the foundation of a Docker image. Each container contains an image of a readable/writable layer (you can - think of it as persistent state or a file). On top of this are other read-only layers. These layers (also known as intermediate images) are generated when the commands in the Dockerfile are executed when the build command for the Docker image is executed.

If your Dockerfile contains from, run and/or copy instructions, then when building the image, the run instruction will create a layer with its own image ID. The image/layer will then show up in the docker history, along with the image's ID and build date. Subsequent instructions will generate another entry, and so on. The CREATED BY column roughly corresponds to a line in the Dockerfile. As shown below.



Illustration of the 'docker history' command

$ docker images

List all images currently stored on the machine.

$ docker inspect
Docker inspect will display low-level information about a specific Docker object. The data stored in this object can be very useful in debugging situations, such as cross-checking Docker mount points.

Note: The command gets two main responses: image-level details and container-level details. You can get the following information from this command:

Container ID and timestamp of creation

Current state (useful when trying to identify if a container has stopped and why)

Docker image information, file system bindings, volume information, and mounts

Environment variables, such as command line arguments passed to the container

Network configuration: IP addresses for IPv4 and IPv6 as well as gateway and secondary addresses

$ docker version
This command displays the version of Docker, including the client and server versions currently installed on the computer.

That's right, Docker is a client-server application. The daemon (a long-running Linux background service) is the server and the CLI is one of many clients. The Docker daemon exposes a REST API through which many different tools can communicate with the daemon.

$ docker version
Client: Docker Engine - Community
Version: 19.03.5
API version: 1.40
Go version: go1.12.12
Git commit: 633a0ea
Built: Wed Nov 13 07:22:34 2019
OS/Arch: darwin/amd64
Experimental: false

Server: Docker Engine - Community
Engine:
Version: 19.03.5
API version: 1.40 (minimum version 1.12)
Go version: go1.12.12
Git commit: 633a0ea
Built: Wed Nov 13 07:29:19 2019
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v1.2.10
GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339
runc:
Version: 1.0.0-rc8+dev
GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
docker-init:
Version: 0.18.0
GitCommit: fec3683

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us