Docker Basic Command

Rakib Hasan
3 min readOct 23, 2021

In this article we will discuss some basic commands of docker. If we need some basic introduction we can check the previous article (Docker: Introduction) which will be helpful to start quickly.

docker run

The docker run command is used to run a container from an image.

docker run nginx
docker run -it nginx
# creating an interactive bash shell in the container
docker run -p 127.0.0.1:80:8080/tcp ubuntu bash
#This binds port 8080 of the container to TCP port 80 on 127.0.0.1 of the host machine.
docker run --expose 80 ubuntu bash
#This exposes port 80 of the container without publishing the port to the host system’s interfaces.
docker run -v /doesnt/exist:/foo -w /foo -i -t ubuntu bash
#The -v flag mounts the current working directory into the container. When the host directory of a bind-mounted volume doesn’t exist, Docker will automatically create this directory on the host for you. In the example above, Docker will create the /doesnt/exist folder before starting your container.

This will run an instance of the nginx application on the docker host. if the image is not present on the host it will go out to docker hub and pull that image down. But this is only done the first time for the subsequent executions. The same image will be reused for the next time.

docker ps

Shows the list of all running containers

docker ps

To see all containers, use the -a (or --all) flag:

docker ps -a

docker stop

Stop one or more running containers

docker stop [OPTIONS] CONTAINER [CONTAINER...]

in options: --time , -t : Seconds to wait for stop before killing it

Here we need to pass the containers id or container name

Example:

docker stop my_container

docker rm

Remove one or more containers

docker rm [OPTIONS] CONTAINER [CONTAINER...]

Example:

docker rm redis

List the most recently created images

docker images

List images by name and tag

docker images java

docker rmi

Remove one or more images

docker rmi [OPTIONS] IMAGE [IMAGE...]

Example:

docker rmi fd484f19954f
docker rmi test1:latest

docker pull

Pull an image or a repository from a registry

docker pull [OPTIONS] NAME[:TAG|@DIGEST]
docker pull debian
docker pull debian:jessie
docker pull ubuntu:20.04

docker exec

The docker exec command runs a new command in a running container.

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Example:
docker exec -it ubuntu_bash bash

docker attach & detach

Attach local standard input, output, and error streams to a running container. Use docker attach to attach your terminal’s standard input, output, and error (or any combination of the three) to a running container using the container’s ID or name.

docker attach [OPTIONS] CONTAINER
Example:
docker run kodekloud/simple-webapp
This is a sample web application that displays a colored background. * Serving Flask app "app" (lazy loading) * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
Detach
docker run –d kodekloud/simple-webapp
a043d40f85fefa414254e4775f9336ea59e19e5cf597af5c554e0a35a1631118
Attach again
docker attach a043d

Run Docker Container & gone to bash

docker run -it centos bash

docker tag

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE. By default without tag docker will pull the latest version of the image.

docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
#To tag a local image with ID “0e5574283393” into the “fedora” #repository with “version1.0”:
docker tag 0e5574283393 fedora/httpd:version1.0

docker inspect

Return low-level information on Docker objects. Docker inspect provides detailed information on constructs controlled by Docker. By default, docker inspect will render results in a JSON array.

docker inspect [OPTIONS] NAME|ID [NAME|ID...]
docker inspect nginx

docker logs

Fetch the logs of a container. The docker logs command batch-retrieves logs present at the time of execution.

docker logs [OPTIONS] CONTAINERdocker logs nginx
docker logs -f --until=2s test

Run Jenkins Docker with volume mapping

docker run -d -p 8585:8080 -p 50000:50000 -v C:/rakib/docker_jenkins_data:/var/jenkins_home jenkins/jenkins

Reference:

--

--