1
ComputingHow To

Basic useful docker commands and docker examples

3 Mins read

The basic useful docker commands are used to run docker containers, manage docker images, and containerize applications. Hopefully, you have installed docker on your local machine, if not, check this article.

docker run

Creates and runs a container from an image.
As we talked about in this article, running hello-world spins up a container and then after execution exits.

Writing more docker commands.

By default, after the image name – we can add a command to run as an override command.
For example: If the image is ubuntu – then will be installed on the container just ubuntu.

docker run ubuntu apt-get update

But if you add “run apt-get update” then on the container it will be installed apt-get and after starting, just run “apt-get update”.

docker ps

This command shows current containers that are running on the machine. If an image has multiple containers running – Show container ID of container that is running.

docker ps --all

or

docker ps -a

Shows all the containers. Including the ID of running containers, stopped containers etc.
The following sections are used to manage containers.

docker rm

This command stops and removes a running container. If the container is not running – exits with an error.

docker rm $(docker ps --all | grep )

deletes all network connections to a specific container. For example, if you have one web server on its own port – then you can clean its connections without stopping or removing your machine network connection to that port (network isolation).

docker top

This shows you the currently running processes on all containers.

docker logs 

Logs the output of the command to STDOUT stream. Useful when debugging docker images. # docker logs hello-world

docker logs <container id>

Example of logging output to file instead of STDOUT.

docker logs -f <container id>

When an image is built with support for logging, you can also use “docker exec” to interact with it directly from the host system, without having to go through VirtualBox or an LXC container.

docker system prune

This command removes all containers that are started just for testing or/and to try out what would have been the default settings of the next image.

Remove a docker container forcefully(Remove a docker container that has refused to stop)

docker rm -f <containerId>

Recap: Docker Container lifecycle

Docker containers are created using an image.

When you start any docker image on your machine – it’s running as a container. A container is simply the name of the process responsible for running a software instance. While it is possible to have multiple containers running on a single machine, they all have their own isolated view of the filesystem and its resources.

This allows a single system to run a lot of software without having to worry about them conflicting with each other or breaking each others’ functionality. The only thing that can affect other containers is the host itself since they’re sharing the same kernel and hardware resources.

Docker run is equivalent to two commands – docker create and docker start
docker create – Creates a container in a halted state.
docker start – Starts a container from a previously created and saved image in a halted state.

Docker example in action

We will use redis – Redis is a popular in memory data structure store. It’s used as a database, cache and message broker.

docker exec -it redis sh
-i – means we can interact with command without terminal or ssh login
-t – means we run in pseudo Terminal mode and not Virtual machine mode
sh – means we will run the container the same way in the host system. Gives full terminal access for the specific container with shell.

Example showing – access to bash shell for the redis container

1.) Create a docker container from an existing image

2.) Start the docker container using it’s containerID

3.) Run the redis bash shell in the container – by passing in an override command

4.) Basic set and get commands in redis

Conclusion

Docker uses its daemon to manage containers. It has very rich set of tools for creating, starting, stopping and managing containers. Those are just a few of these basic commands that you can get started with.

To see official docs from docker, see this.

If you would like to read about namespacing and groups, read here.

Don’t miss amazing tips!

1
Related posts
How ToProgrammingSoftwareTechnology

How to configure Apache Airflow on MacOS locally

4 Mins read
Table of contents0.1 Creating the project folder and the virtual environment0.2 Installing0.3 Running airflow0.4 Starting the webflow server0.5 Starting the scheduler0.6 Running…
Code ChallengesHow ToProgrammingSoftware

How To Implement Merge Sort and Quick Sort Algorithms In Python 3

3 Mins read
Table of contents0.1 Merge Sort0.2 Quick Sort0.3 Conclusion1 Don’t miss amazing tips! Let’s have a look at how to how to implement…
How To

How to write test with Ruby and Capybara with Examples

11 Mins read
Table of contents1 What are the advantages of using Capybara1.1 1) Webdriver agnostic1.2 2) Works with multiple testing frameworks1.3 Capybara DSL1.4 a)…

Leave a Reply

Your email address will not be published. Required fields are marked *

9 + 1 =

×
ComputingHow To

How to install Docker for Windows/Mac