Docker Cheat Sheet
Introduction
Moving towards the world of Dockerization??? Here is the cheat sheet which is going to help you in this journey of containerization and allow you to build and play with Docker Containers. But wait if you are a beginner you will still need a cheat sheet after understanding the very basic concepts of Docker. So what is Docker and why the world is moving towards it????
Docker defines themselves as:
A platform for developers and sysadmins to develop, deploy, and run applications with containers. The use of Linux containers to deploy applications is called containerization. Containers are not new, but their use for easily deploying applications is.
From the long list of Docker’s feature, few top features are flexibility, portable, scalable, interchangeable, stackable and lightweight. Complex applications can be containerized and one can increase and automatically distribute container replicas. Services can be managed vertically and one can upgrade or update the application while it’s running
Containers
Docker says,
Containers are an abstraction at the app layer that packages code and dependencies together.
Containers share the OS kernel with other container running on the same
machine but their processes are isolated.
Create a new container
docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
Rename a container
docker rename ORIGINAL_NAME NEW_NAME
Exampledocker rename nginx nginxCarbonteq
Run a container
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Exampledocker run --name test -it nginx
Remove one or more containers
docker rm [OPTIONS] CONTAINER [CONTAINER...]
Examples
This will remove the container referenced under the link /nginx.docker rm /nginx
This command will force-remove a running container.docker rm --force nginx
This command will remove all stopped containersdocker rm $(docker ps -a -q)
This command will remove a container and its volumesdocker rm -v nginx
Update configuration
docker update [OPTIONS] CONTAINER [CONTAINER...]
Example
To limit the shares or resources a container is utilizing, one can easily update its
configuration. To perform this action you will require the container ID or name.
Container ID can be fetched by running “docker ps” command.docker update --cpu-shares 256 -m 250M abebf7571666 hopeful_morse
docker update --kernel-memory 70M test
Information
To grab the information of a container, below mentioned commands can be very helpful.
List containersdocker ps
Above mentioned command only show the running containers. To see all the running and stopped containersdocker ps –a
Logs of a container
docker logs [OPTIONS] CONTAINER
Exampledocker logs nginx
The docker logs command batch-retrieves logs present at the time of execution.
Docker Inspect
Get low level information about running containers
docker inspect [OPTIONS] NAME|ID [NAME|ID...]
By default, docker inspect will render results in a JSON object.
Example
Get an instance’s IP addressdocker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $INSTANCE_ID
Get Eventsdocker events
Port Mappingsdocker port nginx
Running Process
docker top CONTAINER [ps OPTIONS]
Exampledocker top nginx
File System Diff
docker diff CONTAINER
Exampledocker diff myApp
Starting and Stopping
Start one or more stopped containers
docker start [OPTIONS] CONTAINER [CONTAINER...]
Exampledocker start nginx
Stop one or more running containers
docker stop [OPTIONS] CONTAINER [CONTAINER...]
Exampledocker stop nginx
Restart one or more containers
docker restart [OPTIONS] CONTAINER [CONTAINER...]
Exampledocker restart nginx
Pause all processes within one or more containers
docker pause [CONTAINER, [CONTAINER]]
Exampledocker pause nginx
Import / Export content from container
Copy files/folders between a container and the local filesystem
This command allows you to copy the file from your local machine to the container or from container to your local machine. The main parameters of this command are SRC_PATH and DEST_PATH which allows to copy the file from or to your desired place
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Export a container’s filesystem as a tar archive
Docker export, exports the content of an underlying directory not the contents of whole volume.
docker export [OPTIONS] CONTAINER
Example
Each of these commands has the same result.docker export red_panda > latest.tar
docker export --output="latest.tar" red_panda
Run a command in a running container
Docker execute command allows you to run a command directly into a running container.
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Example
This will create a new file /tmp/execWorks inside the running container ubuntu_bash, in the background.docker exec -d ubuntu_bash touch /tmp/execWorks
Conclusion
Docker, a modern world’s need, is improving the quality of work and efficiency. To handle a huge complex application, it is becoming the need of the hour. With the help of such kind of cheat sheets, one can improve his working capabilities and work more efficiently without spending a lot of time on reading and understanding one concept. Here is the first part of the cheat sheet series. Next are coming soon. STAY TUNED......!!