Breaking down Docker Command
And the end of the phrase “It works on my machine”
Dependency issues really frustrate me. They are not easy to debug and have a tendency to occur at the worst possible times, such as deployment or during releases. There are numerous times when I’ve run a newly cloned repository or deployed to a new environment and had a crash occur. The primary factor in both cases were dependencies that weren’t in sync.
You might be developing on a windows machine and the production environment is in Linux, or the Node version does not match between environments. This causes a problem when you hire a new developer and they have to set up their entire system again so as to work on the project.
Enter Docker to the rescue. Here’s how:
- Rather than installing dependencies on your development pc, Docker runs them in a lightweight, isolated, VM-like environment called a container.
- It is just a single command to download, configure, and run whatever combination of services or platforms that you require.
- Copying this environment to any other development pc can be done in minutes.
- You can easily mount local files into these containers so you can develop and test just as easily as before.
Docker is truly deserving of its logo as a monstrous whale. But, taming the whale requires mastery over its commands. Let’s have a look into the structure and workings of some basic Docker commands.
Breaking down Docker command
Docker commands that we execute in the terminal follow a pattern. They start with a docker keyword followed by the command, flag options, and other arguments.
docker run -it ubuntu sh // Docker commanddocker Command [Options] [Args...] // beaking it down
Command: They are standard docker commands like, run and kill, used to manipulate a container itself and perform some action on it.
[Options]: Options modifies the operation of a command itself. The command still performs their usual operations but adding option flags enhance their functionality.
[Args]: Args stands for arguments. Arguments are data passed to a command. These arguments can be further split into various parts.
[Args..] = image [command to run in the container] [argument for the command that runs in the container]Docker Commands
1. Creating a Container
Using the run command will create a new container and run an executable command within it. Let us take an example:
docker run -it ubuntu shThis statement will not only creates a new container but opens up an interactive terminal.
- -i: This option stands for an interactive standard in or terminal which maps the standard in of the host to the container.
- -t: This option stands for
sudo tty. tty stands for terminal and allows us to interact with the system. - ubuntu: This is the name of the Docker image that is run inside the container.
- sh: This is the executable command that will be associated with the container and will run it after its creation.
We might decide to ignore flags associated with the run command as they are not a compulsion but are necessary in order to interact with the container.
Get Shreejit Rajbanshi’s stories in your inbox
Join Medium for free to get updates from this writer.
Without the -i option we cannot execute any statements as the standard in has not been mapped. And skipping -t will still let us execute commands but the input is not visible at all as a terminal has not been mapped.
sh is the command that will run inside the container. We can replace it with something else such as echo hello.
2. Restart a Container
docker restart ${container id}We can always resume a stopped container using the restart command, as long as it has not been removed. All of them have an id associated, that is unique to a particular container and acts as an identifier.
We can pass multiple ids to a single command to restart all of them.
3. List Containers
docker ps // List all running containersdocker ps -a // List all containers including those not running
The ps command allows us to list all containers. Passing -a along with ps includes stopped containers on the list.
Along with the containers, details such as container id, the image it is running, and name are also displayed.
4. Remove Container
A container that has stopped is still in memory. We can restart or remove it completely. Docker containers are not automatically removed when we stop them unless we create them using the --rm flag with run command.
Until we delete the container completely it sits and consumes space. To remove one or more Docker containers, use the docker rm command, followed by the Ids of containers we want to remove.
docker rm ${container id}To remove all containers we can use the following command.
docker rm $( docker ps -aq )docker ps -aq will give us ids for all the docker container in the system.
5. Attach to a container
Use docker attach to attach the terminal’s standard input, output, and error (or any combination of the three) of a running container using the container’s ID or name. This allows us to view its ongoing output or to control it interactively, as though the commands were running directly in your terminal.
docker attach ${container id} We can attach to the same contained process multiple times simultaneously, from different sessions on the Docker host.
Docker Container and execution command
A container can be created, stopped, attached to, or killed. But we cannot change the command attached to it. Once we create a container it has a command associated with it and that command is what will always get run when we restart it.
So if we have a container with an executing command sh then we can restart and reattach it as it has a shell command.
docker run -it ubuntu echo helloLet's run the above command and which will create a container with echo hello attached to it. This container will just print out hello in the terminal. If we restart this container it will again echo out hello and exit. But we will still have the container laying around. If we look at it using docker ps -a then we can see it.
Wrapping it up
When I started working on Docker I blindly learned all the commands without realizing the pattern in them. I hope this gave all of you a quick overview of docker commands and an intro to the understanding docker.
This article is part of a series on docker concepts for folks new to web development who are keen to learn and grow.






