DEV Community

Devang Tomar
Devang Tomar

Posted on • Originally published at devangtomar.Medium on

TOP Docker commands (with examples) that you should know

Docker is an open platform for app development, shipping, and running. Docker allows you to decouple your apps from your infrastructure, allowing you to release software more quickly. Docker allows you to package and run an application in a container, which is a loosely isolated environment. The Docker container trend is unstoppable, with firms actively seeking people that are well-versed in Docker commands.

1. Docker Registries & Repositories

Login to a Registry

docker login

docker login localhost:8080
Enter fullscreen mode Exit fullscreen mode

Logout from a Registry

docker logout

docker logout localhost:8080
Enter fullscreen mode Exit fullscreen mode

Searching an Image

docker search nginx

docker search --filter stars=3 --no-trunc nginx
Enter fullscreen mode Exit fullscreen mode

Pulling an Image

docker image pull nginx

docker image pull devangtomar/nginx localhost:5000/myadmin/nginx
Enter fullscreen mode Exit fullscreen mode

Pushing an Image

docker image push devangtomar/nginx

docker image push devangtomar/nginx localhost:5000/myadmin/nginx
Enter fullscreen mode Exit fullscreen mode

2. Running Containers

Create and Run a Simple Container

Start an ubuntu:latest image

* Bind the port 80 from the CONTAINER to port 3000 on the HOST

* Mount the current directory to /data on the CONTAINER

* Note : on windows you have to change -v ${PWD}:/data to -v “C:\Data”:/data

docker container run — name infinite -it -p 3000:80 -v ${PWD}:/data ubuntu:latest
Enter fullscreen mode Exit fullscreen mode

Creating a Container

docker container create -t -i devangtomar/infinite — name infinite
Enter fullscreen mode Exit fullscreen mode

Running a Container

docker container run -it — name infinite -d devangtomar/infinite
Enter fullscreen mode Exit fullscreen mode

Renaming a Container

docker container rename infinite infinity
Enter fullscreen mode Exit fullscreen mode

Removing a Container

docker container rm infinite
Enter fullscreen mode Exit fullscreen mode

A container can be removed only after stopping it using docker stop command. To avoid this, add the — rm flag while running the container.

Updating a Container

docker container update — cpu-shares 512 -m 300M infinite
Enter fullscreen mode Exit fullscreen mode

Running a command within a running container

docker exec -it infinite sh
Enter fullscreen mode Exit fullscreen mode

In the example above, bash can replace sh as an alternative (if the above is giving an error).

3. Starting & Stopping Containers

Starting

docker container start nginx
Enter fullscreen mode Exit fullscreen mode

Stopping

docker container stop nginx
Enter fullscreen mode Exit fullscreen mode

Restarting

docker container restart nginx
Enter fullscreen mode Exit fullscreen mode

Pausing

docker container pause nginx
Enter fullscreen mode Exit fullscreen mode

Unpausing

docker container unpause nginx
Enter fullscreen mode Exit fullscreen mode

Blocking a Container

docker container wait nginx
Enter fullscreen mode Exit fullscreen mode

Sending a SIGKILL

docker container kill nginx
Enter fullscreen mode Exit fullscreen mode

Sending another signal

docker container kill -s HUP nginx
Enter fullscreen mode Exit fullscreen mode

Connecting to an Existing Container

docker container attach nginx
Enter fullscreen mode Exit fullscreen mode

4. Getting Information about Containers

From Running Containers

Shortest way :

docker ps
Enter fullscreen mode Exit fullscreen mode

Alternative :

docker container ls
Enter fullscreen mode Exit fullscreen mode

From All containers.

docker ps -a 

docker container ls -a
Enter fullscreen mode Exit fullscreen mode

Container Logs

docker logs infinite
Enter fullscreen mode Exit fullscreen mode

‘tail -f’ Containers’ Logs

docker container logs infinite -f
Enter fullscreen mode Exit fullscreen mode

Inspecting Containers

docker container inspect infinite

docker container inspect --format ‘{{ .NetworkSettings.IPAddress }}’ $(docker ps -q)
Enter fullscreen mode Exit fullscreen mode

Containers Events

docker system events infinite
Enter fullscreen mode Exit fullscreen mode

Public Ports

docker container port infinite
Enter fullscreen mode Exit fullscreen mode

Running Processes

docker container top infinite
Enter fullscreen mode Exit fullscreen mode

Container Resource Usage

docker container stats infinite
Enter fullscreen mode Exit fullscreen mode

Inspecting changes to files or directories on a container’s filesystem

docker container diff infinite
Enter fullscreen mode Exit fullscreen mode

5. Managing Images

Listing Images

docker image ls
Enter fullscreen mode Exit fullscreen mode

Building Images

From a Dockerfile in the Current Directory

docker build .
Enter fullscreen mode Exit fullscreen mode

From a Remote GIT Repository

docker build github.com/creack/docker-firefox
Enter fullscreen mode Exit fullscreen mode

Instead of Specifying a Context, You Can Pass a Single Dockerfile in the URL or Pipe the File in via STDIN

docker build - < Dockerfile

docker build - < context.tar.gz
Enter fullscreen mode Exit fullscreen mode

Building and Tagging

docker build -t devangtomar/infinite .
Enter fullscreen mode Exit fullscreen mode

Building a Dockerfile while Specifying the Build Context

docker build -f myOtherDockerfile .
Enter fullscreen mode Exit fullscreen mode

Building from a Remote Dockerfile URI

curl example.com/remote/Dockerfile | docker build -f — .
Enter fullscreen mode Exit fullscreen mode

Removing an Image

docker image rm nginx
Enter fullscreen mode Exit fullscreen mode

Loading a Tarred Repository from a File or the Standard Input Stream

docker image load < ubuntu.tar.gz

docker image load --input ubuntu.tar
Enter fullscreen mode Exit fullscreen mode

Saving an Image to a Tar Archive

docker image save busybox > ubuntu.tar
Enter fullscreen mode Exit fullscreen mode

Showing the History of an Image

docker image history
Enter fullscreen mode Exit fullscreen mode

Creating an Image From a Container

docker container commit nginx
Enter fullscreen mode Exit fullscreen mode

Tagging an Image

docker image tag nginx devangtomar/nginx
Enter fullscreen mode Exit fullscreen mode

Pushing an Image

docker image push devangtomar/nginx
Enter fullscreen mode Exit fullscreen mode

6. Networking

Creating Networks

  • Creating an Overlay Network
docker network create -d overlay MyOverlayNetwork
Enter fullscreen mode Exit fullscreen mode
  • Creating a Bridge Network
docker network create -d bridge MyBridgeNetwork
Enter fullscreen mode Exit fullscreen mode
  • Creating a Customized Overlay Network
docker network create -d overlay —-subnet=192.168.0.0/16 -subnet=192.170.0.0/16 --gateway=192.168.0.100 --gateway=192.170.0.100 -- ip-range=192.168.1.0/24 --aux-address=”my-router=192.168.1.5" --aux-address=”my-switch=192.168.1.6" --aux-address=”my-printer=192.170.1.5" --aux-address=”my-nas=192.170.1.6" MyOverlayNetwork
Enter fullscreen mode Exit fullscreen mode

Removing a Network

docker network rm MyOverlayNetwork
Enter fullscreen mode Exit fullscreen mode

Listing Networks

docker network ls
Enter fullscreen mode Exit fullscreen mode

Getting Information About a Network

docker network inspect MyOverlayNetwork
Enter fullscreen mode Exit fullscreen mode

Connecting a Running Container to a Network

docker network connect MyOverlayNetwork nginx
Enter fullscreen mode Exit fullscreen mode

Connecting a Container to a Network When it Starts

docker container run -it -d — network=MyOverlayNetwork nginx
Enter fullscreen mode Exit fullscreen mode

Disconnecting a Container from a Network

docker network disconnect MyOverlayNetwork nginx
Enter fullscreen mode Exit fullscreen mode

Exposing Ports

Using Dockerfile, you can expose a port on the container using :

EXPOSE <port_number>
Enter fullscreen mode Exit fullscreen mode

You can also map the container port to a host port using :

docker run -p $HOST_PORT:$CONTAINER_PORT — name -t <image>
Enter fullscreen mode Exit fullscreen mode

e.g.

docker run -p $HOST_PORT:$CONTAINER_PORT — name infinite -t infinite
Enter fullscreen mode Exit fullscreen mode

7. Security

Guidelines for Building Secure Docker Images

  1. Prefer minimal base images
  2. Dedicated user on the image as the least privileged user
  3. Sign and verify images to mitigate MITM attacks
  4. Find, fix and monitor for open source vulnerabilities
  5. Don’t leak sensitive information to docker images
  6. Use fixed tags for immutability
  7. Use COPY instead of ADD
  8. Use labels for metadata
  9. Use multi-stage builds for small secure images
  10. Use a linter

8. Cleaning Docker

Removing a Running Container

docker container rm nginx
Enter fullscreen mode Exit fullscreen mode

Removing a Container and its Volume

docker container rm -v nginx
Enter fullscreen mode Exit fullscreen mode

Removing all Exited Containers

docker container rm $(docker container ls -a -f status=exited -q)
Enter fullscreen mode Exit fullscreen mode

Removing All Stopped Containers

docker container rm `docker container ls -a -q`
Enter fullscreen mode Exit fullscreen mode

Removing a Docker Image

docker image rm nginx
Enter fullscreen mode Exit fullscreen mode

Removing Dangling Images

docker image rm $(docker image ls -f dangling=true -q)
Enter fullscreen mode Exit fullscreen mode

Removing all Images

docker image rm $(docker image ls -a -q)
Enter fullscreen mode Exit fullscreen mode

Removing all Untagged Images

docker image rm -f $(docker image ls | grep “^” | awk “{print $3}”)
Enter fullscreen mode Exit fullscreen mode

Stopping & Removing all Containers

docker container stop $(docker container ls -a -q) && docker container rm $(docker container ls -a -q)
Enter fullscreen mode Exit fullscreen mode

Removing Dangling Volumes

docker volume rm $(docker volume ls -f dangling=true -q)
Enter fullscreen mode Exit fullscreen mode

Removing all unused (containers, images, networks and volumes)

docker system prune -f
Enter fullscreen mode Exit fullscreen mode

Clean all

docker system prune -a
Enter fullscreen mode Exit fullscreen mode

Let’s connect and chat! Open to anything under the sun 🏖️🍹

🐦 Twitter : devangtomar7

🔗 LinkedIn : devangtomar

📚 Stackoverflow : devangtomar

🖼️ Instagram : be_ayushmann

Ⓜ️ Medium : Devang Tomar

Hashnode : devangtomar

🧑‍💻 Dev.to : devangtomar

Top comments (1)

Collapse
 
devangtomar profile image
Devang Tomar

URL of a Hashnode post (with table of content for ease to locate stuff) :

TOP Docker commands (with examples) that you should know 💻

Docker is an open platform for app development, shipping, and running. Docker allows you to decouple your apps from your infrastructure, allowing you to release software more quickly. Docker allows you to package and run an application in a container...

favicon devangtomar.hashnode.dev