0% found this document useful (0 votes)
21 views

2docker Notes

This document provides information on various Docker commands used to manage containers. It summarizes key Docker container commands like docker run to create and start a container, docker create to only create a container, docker start to start an existing container, docker stop to stop a container gracefully, docker kill to stop a container immediately, and docker rm to remove a container. It also discusses commands for viewing container logs, setting resource limits, mounting volumes, and more.

Uploaded by

Test
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

2docker Notes

This document provides information on various Docker commands used to manage containers. It summarizes key Docker container commands like docker run to create and start a container, docker create to only create a container, docker start to start an existing container, docker stop to stop a container gracefully, docker kill to stop a container immediately, and docker rm to remove a container. It also discusses commands for viewing container logs, setting resource limits, mounting volumes, and more.

Uploaded by

Test
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

$ docker run image_name new start up command

$ docker run busy-box echo hi


$ docker run busy-box ls
$ docker run busy-box ping www.google.com

docker run imageName = docker create imageName + docker start -a containerId

$ docker create image name


$ docker start -a container Id → to start the existing container which is in stopped state

$ docker system prune → this command will remove


→ all the stopped containers
→ all networks not used by at least one containers
→ all dangling images
→ all build cache

Docker Container commands


List containers $ docker ps → list only running
containers

$ docker ps -a → list running


and stopped containers

$ docker ps -q → list only


running container Ids
Create and Run docker run --name mycontainer
imageToUse

this command pulls the image


from the remote repository if the
host system does not have the
image locally and then create a
container

kubectl run podName --image


imageToUse
Create and Run in detached docker run -d imageName docker run -d redis
mode
Create and Run in interactive docker run -it imageName docker run -it redis sh
mode with command( helpful for commandToRun
dry run and to validate what is
inside an image. Not used in
prod as this will override
command defined in the actual
image)
Create and Run with override Docker run image java -jar
command to run app.jar

Override entrypoint and


command

Docker run --entrypoint java


imageToUse -jar app.jar

Create but do not run. docker create imageName

this command will create a new


container but will not start it.
Execute an additional command docker exec -it <container id> docker exec -it
in an already existing container command myredisContainerID redis-cli
(without disturbing primary
command/process) exec – run another command docker exec -it
-i allow us to provide input to the myredisContainerID sh
Useful to debug live container container (Keep STDIN open)
in prod -t tty docker exec -it
myredisContainerID bin/sh

kubectl exec -it <POD_NAME> kubectl exec -it frontendpod


<COMMAND> bash

kubectl exec -it <POD_NAME> above command is deprecated.


-- <COMMAND> Use below instead ( just add --
infront of the command)

(OR)

kubectl exec -it frontendpod --


bash
Port forwarding from local port $ docker run -p $ docker run -p 8080:6379 redis
to container port portInLocalSystem:portInCon
tainer myImage
$ docker run -p 6379:6379 redis

Pass environment values using -e docker run -d docker run --env-file=env_file_name


flag to the container. -e alpine env
SSO_HOST_URL=http://someth
ing
-e SSO_PASS=A$B
my-image

if you declare environment


variable name but do not provide
value in run command then it
will be substituted from host
systems environment variable
value.

$docker run -d
-e SSO_HOST_URL
-e SSO_PASS
my-image

d run --env-file=my_env.properties -it


ubuntu bash

my_env.properties content
my_env_property=BLAH...

will be added to the environment

Run existing non running Docker start -a containerID


container
-a --> attached mode
See log Docker logs containerID
Follow the logs Docker logs -f containerId
--follow or -f

--since Docker logs --since 42m


Show logs since timestamp (e.g. containerid
2013-01-02T13:23:37Z) or
relative (e.g. 42m for 42
minutes)
$docker logs --tail 1000 -f
containerid
--tail , -nallNumber
of lines to
show from the end of the logs
Stop container gracefully Docker stop containerId If could not stop in 10 secs then
it will fallback to kill
command(refer next one)

--time ( OR -t) to change default 10 secs to


something else.

Stop container immediately Docker kill containerID


Restart container Docker restart containerId Seconds to wait for stop before
Restart one or more containers killing the container

--time ( OR -t) to change default 10 secs to


something else.

Show process that are running Docker top containerId


inside a container. Shows both
primary process (defined in the
Docker file and as well as
secondary process like sh in
docker exec -it containerId sh)
docker stats [OPTIONS]
[CONTAINER...]
Display a live stream of
container(s) resource usage $ docker stats
statistics

shows how much resource is


used by each containers like
cpu, memory, PIDS and network
etc

The PIDS column contains the


number of processes and kernel
threads created by that
container.

We can even create an image $ docker commit -c “CMD


[echo hi]”
from the running container runningContainerId
though it is not used in real
after -c we can provide
project as we should use override command to be
Dockerfile to make changes and used for new image. Here I
have added echo hi
recreate images and version the
changes.

But for quick testing or


validating purpose we can create
images from running container

Pause – we can pause running $ docker pause my_container


processes in side a container as
well.

The docker pause command


suspends all processes in the
specified containers. On Linux,
this uses the freezer cgroup.
Traditionally, when suspending a
process the SIGSTOP signal is
used, which is observable by the
process being suspended.

Probably useful when a long


running process executing and
wanted to make other system
changes in between so that
process can pick new changes
without killing and restarting
new container.

Unpause - to resume paused $ docker unpause my_container


processes in

Set conainer memory while docker run -m 512m nginx


starting a container
$ docker run -m 512m --memory-
-m or –-memory defines max reservation=256m nginx
memory limit

--memory-reservation defines min


memory

Defines CPU limit for a $ docker run --cpus=2 nginx


containers
$ docker run --cpus=2 --cpu-
--cpus = defines max cpu shares=2000 nginx

--cpu-shares = defines priority

Mounting a volume with docker run


-p3000:3000
container -v hostDir:/containerDir
-d
imageId

docker run
-p3000:3000
-v /containerDir/otherDir
-v hostDir:/containerDir
-d
imageId

when container Dir is


mapped to host system and
changes to the files in
host system will be
reflected in container as
well.

If containerDir not exists


then it will be created
inside the container

Note
/containerDir/otherDir
will not be mapped to host
system because that
volume does not have
mapping for it. Which
means that dir will be
untouched and have same
content as when the image
got created.

Stop running container $ docker stop


containerid(s)

$docker stop $(docker ps


-q) → stop all running
containers

Delete stopped containers $ docker rm containerId(s)

OR

$docker container prune →


to delete all the stopped
containers
Restart automatically Read in the blow sections docker run -dit --restart
[restart-policy-value]
[container_name]

You might also like