Docker Flags Cheat Sheet



This is just a cheat sheet of commands and terminology for Docker and ASP.NET Core; it contains commands that you can find in the original cheat sheet, plus a Dockerfile for ASP.NET Core and a quick guide on how to created one from Visual Studio. Hopefully, both developers that are in the process of getting into the containerize world with Docker and developers that are already in but need a quick recap will find it useful.

Docker Hub provides Docker image hosting, public or private registries, build triggers and web hooks, and integration with GitHub and Bitbucket. Docker daemon: Runs on host machine creates and manages docker objects such as images, containers, network, volume, data, etc. And for more information on any Docker command, run: docker swarm command –help. Find the most important of these commands and a Docker Commands basic structure reminder on our attached Docker Commands Cheat Sheet. An all in one PDF to keep at hand.

Basic terminology

TermShort explanation
DockerDocker is a set of platform as a service products that uses OS-level virtualization to deliver software in packages called containers. Download Docker for Windows here.
ImageAn image, or more correct, a Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.
ContainerA container image becomes a container at runtime when they run on Docker Engine
Docker EngineDocker Engine is a container runtime that runs on various Linux (CentOS, Debian, Fedora, Oracle Linux, RHEL, SUSE, and Ubuntu) and Windows Server operating systems…
Docker HubDocker Hub is a service provided by Docker for finding and sharing container images with your team.
DockerfileA Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

Read more information about Docker Container Images and Docker Containers here.

Basic commands

Follows, a list of basic commands that you will regularly need. Run them using command line from the root of your application – where the Dockerfile should exists.

Docker Flags Cheat Sheet
TermShort explanation
docker pullRetrieve an image from a registry. If you specify only the repository name, Docker will download the image tagged latest from that repository on Docker Hub.
e.g. docker pull mcr.microsoft.com/dotnet/core/aspnet:3.1 pulls the 3.1 runtime, where docker pull mcr.microsoft.com/dotnet/core/sdk pulls the latest .NET Core SDK.
docker buildCreate a new image by running a Dockerfile. User the -t flag to specify the name of the new image and don’t forget the . (build context for the source files for the COPY command)
e.g. docker build -t image.name.for.my.app:v1 .
docker image listAfter pulling an image, view the images in your local registry with the docker image list command.
docker psView active containers. Use the -a flag to view all.
e.g. docker ps -a
docker runRun an image – it will become a container. Specify the option -p for port mapping (left hand side local port, right hand side port exposed by docker) and -d to run it as a background service.
Speficy the --name option to set the name of the container.
e.g. docker run -p 8080:80 -d --name container.name image.name.for.my.app
docker stopStop an active container by specifying the container ID. Get that with the docker ps command
e.g. docker stop elegant_ramanujan
docker startRestart a stopped container.
e.g. docker start elegant_ramanujan
docker container rmRemove a stopped container. Add the -f flag to force remove a running container (not a graceful shutdown)
e.g. docker container rm -f elegant_ramanujan
docker image rmRemove an image. There is no force flag here, all containers using this image must be stopped.
e.g. docker image rm mcr.microsoft.com/dotnet/core/samples:aspnetapp

A Dockerfile sample

Living in the root of the application, a Dockerfile is just a plain text file; you can either use the following command to create it in Windows, or anyway you like: copy NUL Dockerfile. The sample below contains everything necessary to build and run an image. Comments above each command attempt to provide a bit of clarity:

A cheat with Microsoft Visual Studio

If it happens to have a Visual Studio around, just right click on your main project, select ‘Add’ and then ‘Docker Support…’:

.

Usually, for ASP.NET Core, I choose ‘Linux’ as Operating System; at the end it comes cheaper if you want to host it, for example, in Azure.

Docker technology can never replace the concept of virtualization but has got its own special benefits. The ability to run an application by consuming minimalistic resources is just mindboggling. The feature of complete isolation is an everyday requirement for most of us.

Recently I was creating challenges for winjactf 2021. And all the challenges had to be dockerized. Since I was doing it for the first time, I faced several issues. I felt that any beginner would get into similar troubles so I thought of blogging out the scenario where I made some stupid mistakes, but yes learned a lot in solving them.

With this post, I’ll be sharing some must-know commands in docker that will surely solve lot of your problems.

So let’s jump in.

Docker Flags Cheat Sheet

Basic commands

This is a probably the complete set of commands, that we use on daily basis.

Let’s see a brief explaination for all of them.

  • container ls :- lists all the running containers
  • image ls :- lists all the images residing over the machine
  • ps :- tells you about all the active processes
  • ps -a :- Gives you additional information like details about the process that exited.
  • run -it :- This command starts a container. If the specified image is not present, it will pull it from the official docker repository (docker hub).
    • -p :- it maps the internal port of the docker to the host. For example, -p 8080:80 will map the internal port 80 to the 8080 of the host machine. So when u write, http://localhost:8080, you are actually accessing the web service on container running on port 80.
    • –name :- gives a name to your running container.
    • -v :- It maps the volume on the host machine to the container. Meaning? You can think of it as a shared disk space. This flag is used when u have to persist the state of a container.
    • /bin/bash :- Its the command that you want to run in the container. You can also write “whoami” to just print the current user, or directly get shell access with /bin/bash.
    • -d :- Run the container in background
  • exec -it :- It executes commands in an already running container.
Flags

But there’s more to it….

The commands listed above might solve most of your purpose. But, the endless possibilities of docker got me into more of digging! While working, I came across a few interesting commands that I want to list.

You see something different? The 9c. Its the initials of the container id that might be something of the form :- 9c13df3fd. So instead of copy-pasting you can simply type the initials and your docker command will know which container is being referenced. Same thing works when you are removing the image.

Want to start a stopped container? You can do it by

Running out of space because of containers?

When the purpose of the container is served, it’s better to remove it. Because a lot of 1 GB containers are gonna eat up huge space. Remove images too, that are not in use.

You can check the amount of space that can be reclaimed due to unused images, run

Docker Flags Cheat Sheet

Delete images with command

Use -f if you want to force the delete operation. Here, the reason may be that multiple images are linked together.

Want to submit your container to docker hub?

You read that right! Anyone can submit their container to docker hub for free. It can be an easy way to share your work. Someone else can easily setup your environment with a docker pull.

Docker Cheat Sheet Pdf

Sign-up with dockerhub and click to create a repository. You will get this page.

With the free version, only 1 repo can be created. But obviously, you can use multiple tags to different images that you want to upload.

You dont want the hassle of uploading to dockerhub?

Lets say , You built a web application that you wanna share with your friends. When your container is up and running with all the desired stuff, you just need to save the state of your container. Wondering how to do that?! Its just a command away.

This command will create a new image from the base image that you had chosen to build your application.

Note: Everything in docker is layered. When a new image is committed, new layers are added to the existing ones.

Now once you have the new image, you can directly push it to docker hub.

You can also create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Now simply run a docker push command to push to docker registry.

When you own a private repository, the hostname has to be preceded in the docker tag and docker push command. Example:- docker push example.com/target_img:tag

From image to tar file

Do you know, you can also convert an image to a tar file and move it around like a normal file? The command below makes it possible

Similarly, you can load an image from the tar file using

Dealing with dockerfiles and docker-compose.yaml?

Dockerfiles are just like the script files that will just run whole thing in a go. Anyone with a dockerfile can replicate your whole container. Much handy than the tar file right?!

docker-compose file come into play when your environement requires more than one service (ie more than one container). In compose files, you basically specify the configurations corresponding to different containers that will be spawned up like env variables, port mappings, volume mappings.

To build an image from dockerfile, run

The “dot” corresponds to the path of the dockerfile ie the current working directory.

Then following commands are generally used with the docker-compose files

Mistakes I made while creating challenges.

Let’s suppose, you want to create a challenge that requires the use of ssh service.

Initially, I made the mistake of choosing the standard images like ubuntu (for docker) and I used to install ssh using the package manager apt. If I had a requirement for another service like http, I used to install it again with apt on the same container of ubuntu and saved the state as the final image.

Everything works fine in the above scenario, only if you have a shell inside the container. In the cases, when you are required to automate the whole process, this approach won’t really work. Automation as in that you have a dockerfile or a newly committed image and you should just be able to run the container without any additional docker exec commands. If your application requires you to execute commands after running the container from the image, that means that you did something wrong in the configuration. I’ll explain why these commands fail while automating stuff.

Reason

Docker Flags Cheat Sheet

Docker Flags Cheat Sheet Template

After the completion of the main process, the container exits. The scripts that are run in Dockerfile’s CMD are written such that it wont run forever. And a container requires some foreground process to be running continuously in order to be in a running state. There are a lot of fixes available here, but I don’t find it to be the best way to do things.

If you still want to enclose everything in a single container, follow up this post- https://docs.docker.com/config/containers/multi-service_container/

The correct way

If your challenge involves multiple services, the correct wayout is to have a separate container for each service. The base images of these services on dockerhub are built such that on the creation of the container , corresponding services are automatically started.

For web service we require httpd image from the dockerhub.

Docker Command Line Cheat Sheet

You can choose from various tags are available for httpd that matches your requirements. Always go through the documentation for any service image that you wanna pull. It gives a lot of information about the usage of the image!

You can enable networking between all of your services using docker-compose files. It binds all the containers and gives you a single environment. I will cover the dockerfile and docker-compose file essentials in the follow-up post.

Docker Build Cheat Sheet

Images to use when you create your applications

I found that some of the popular dockerhub images were creating configurational problems while i was implementing them into challenges

I want to collate a list of all the docker images that i used for different services.

  • Mysql : https://hub.docker.com/_/mariadb [The mysql dockerhub image has got some limitations like I faced troubles while setting up the root password for db]. Mariadb image is much stable in every situation.
  • http : https://hub.docker.com/_/httpd [Choose from the enormous tag that is apt for your use-case]
  • ssh : https://hub.docker.com/r/rastasheep/ubuntu-sshd/dockerfile
    • The default image (openssh-server) didn’t served my purpose. I had to create new users, set suid bit on a binary, and had to make sure that any file doesn’t get deleted (esp flag.txt).
  • tomcat: https://hub.docker.com/_/tomcat [Choose from the various tags that serves your purpose]

What not to use while building upon docker

Docker Flags Cheat Sheet Pdf

Unless its too urgent, try avoiding the use of ftp docker image. Why? The way that service works in docker takes a lot of open ports as a pre-requisite. And having a lot of open ports is a bad thing….

Epilogue

I hope you learned something new today around docker and would have learned from my mistakes. I will continue to post my findings on docker. Comment, if you prefer to learn about any specific topic.

Docker Cheat Sheet 2020

Thats all for this blog post! Hit like button if you enjoyed reading the post!

See you in the next one! Until then, happy learning!!!